I have a column in snowflake of type TIMESTAMP_TZ(9) and I have set its default value as
CURRENT_TIMESTAMP().
But still in all rows, I am getting the value of this column as null.
Why is the default value not being reflected and its giving null values?
Are you sure you're not inserting NULL manually?
Here's what works for me:
create or replace table testX(id integer autoincrement, text string, time timestamp_tz(9) default current_timestamp());
insert into testX (text) values ('test message');
I get:
Then I insert a NULL value:
insert into testX (time) values (NULL);
Now I see:
Table definition looks exactly like yours:
describe table testX;
I see:
Related
Working a project where we're considering adding a rowversion column to a bunch of tables. Seems like it wouldn't be a big deal because SQL Server assigns the value, so we wouldn't need to modify existing stored procedures, of which there are many. Not that easy, though, because the INSERT requires either a column list or a null value. So if a rowversion column were added to a table, any stored procedure written without the column list would fail.
Below is sample code from the Microsoft docs with some modifications to show what I mean:
CREATE TABLE MyTest
(
myKey int PRIMARY KEY,
myValue int,
RV rowversion
);
GO
--Works fine
INSERT INTO MyTest (myKey, myValue) VALUES (1, 0);
GO
--Fails with error: Column name or number of supplied values does not match
--table definition
INSERT INTO MyTest VALUES (2, 0);
--Works fine. Weird
INSERT INTO MyTest VALUES (2, 0, NULL);
GO
I don't understand why it requires a NULL value if a column list isn't supplied. Since SQL Server calculates the value, I don't see why it would throw off the mapping to the table definition, and it makes the code confusing because it looks as if you're giving the field a null value when that's not what happens.
Does anybody know the reason for this peculiar behavior?
The column-order is only predefined, when you provide all values. Otherwise you could also provide the Parameters in the wrong order. So if you want the to skip Parameters, you have to provide a Columnlist, to give a hint, how the values should be interpreted, wether there is a computed value involved, or not.
INSERT INTO Locality(versionNo,localityName, localityType, constiCode, deleteFlag,
createdOn, lastUpdate,updatedBy)
SELECT 1, localityName, Localitytype, constituencyCode + '' + regionCode, 1,
CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, CURRENT_USER
FROM dbo.MasterTable.
Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'localityCode', table 'GovMaster.dbo.Locality';
column does not allow nulls. INSERT fails.
I want to insert but the LocalityCode is primary key and should not be null. But I get that error above. The localityCode is primary key and it can not be null. And i want the localityCode to be auto generated, it should do an autoincrement on insert.
You're not inserting a value for localityCode, it's missing from your field list:
INSERT INTO Locality(versionNo,localityName, localityType, constiCode, deleteFlag,
createdOn, lastUpdate,updatedBy)
When you leave a field off the INSERT list, it gets a default NULL value. If it were an auto-incrementing ID field, it would be correct to exclude it, but it is not in your case. You either need to supply a value, or change the schema to allow NULL values for that field, or make it an auto-incrementing field.
Just cutting to the chase, the reason you are getting that error is because:
Problem:
You have a column name localityCode defined as a NOT NULL in your Locality table and it does not appear to be an auto-incrementing field.
Secondly, you are not passing any value in your insert statement below, hence it's passing a null value by default to your localityCode column.
Solution:
Ideally, you would want to pass some non-null values to your localityCode explicitly in your insert statement.
The other option would be change your null property for your localityCode column to accept NULL values.
Alternatively, if you want to set the localityCode column as an auto-increment field, you may want to look into this: Adding an identity to an existing column
Hope this helps!
Thank you guys.
I dropped the existing column and recreated it with localityCode INT IDENTITY(1,1) and alter the column to primary key after that.
:-)
I have an SSMS table that has one column that is a pk and set to auto increment. In my code I need to create a row and then pull the value of the ID.
I don't want to set the id, it needs to auto generate. Is there a way to do this without adding a second column to add data into?
Just use the phrase "DEFAULT VALUES" in place of a field list and values specification:
INSERT INTO [TableName] DEFAULT VALUES;
Test with:
CREATE TABLE #Test
(
ID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY
);
INSERT INTO #Test DEFAULT VALUES;
SELECT SCOPE_IDENTITY();
For more info on this, check out the MSDN page for INSERT and do a "find" (i.e. Control-F) in your browser for "default values".
We have a tool and this tool create some insert scripts to add rows in our plsql table.
this scripts is not modifiable and we can't see this scripts. so,
when data row comes (with insert script that we don't know structure), we should give a primary key. we can use trigger but we don't want do this for reasons of performance.
Below code doesn't work.
CREATE TABLE qname
(
qname_id integer NOT NULL default qname_id_seq.nextval PRIMARY KEY,
);
Any idea?
Thanks..
.nextval cannot be used in Default Value of table, This is achieved by Trigger and Sequence when you want serialized number that anyone can easily read/remember/understand. But if you don't want to manage ID Column (like qname_id) by this way, and value of this column is not much considerable, you can use SYS_GUID() at Table Creation to get Auto Increment like this.
CREATE TABLE qname
(qname_id RAW(16) DEFAULT SYS_GUID() PRIMARY KEY,
name VARCHAR2(30));
(or by Modifying Column)
Now your qname_id column will accept "globally unique identifier value".
you can insert value in table by ignoring emp_id column like this.
INSERT INTO qname (name) VALUES ('name value');
So, it will insert unique value to your qname_id Column.
I have come across the following piece of T-SQL. Can somebody explain what it does.
INSERT #numbers default VALUES
The temporary table #numbers was created a few lines before with the following:
CREATE TABLE #numbers (num int identity primary key)
What does the "default values" bit do?
While I will never understand the need for the #numbers table, with just one column defined as identity, there is no other way to insert data into that table
INSERT #numbers default VALUES
will insert one row of data with the next IDENTITY value. The only alternate would be to use IDENTITY INSERT ON, which would be cumbersome in this case
The DEFAULT constraint is used to insert a default value into a column.
The default value will be added to all new records, if no other value is specified.
In your case, you just have an identity column which does not really correspond to a column with a specified default value.