Identity column in Snowflake - snowflake-cloud-data-platform

A simple question in Snowflake. I created a table with an identity column.
create table etl_test
(id int identity,
name string);
And tried to insert data into it. In my SQL Server world, I just need to provide the value for the name field.
insert into etl_test values('test');
I tried the same in Snowflake but I am getting an error
SQL compilation error: Insert value list does not match column list expecting 2 but got 1
How can I make sure that the value 'test' is inserted with id = 1?

You need to include the column name(s) you are using if you aren't inserting into all the columns:
insert into etl_test (name) values('test');

"Blind inserts" are antipattern regardless of RDBMS.
In your scenario, you need to specify column list and either remove id from column list or provide a DEFAULT for it.
INSERT INTO etl_test(id, name) VALUES (DEFAULT, 'test');

Related

Determine UniqueIdentifier id of newly created record in remote table

When I create a new record in a table that has a primary key of type INT and I want to retrieve the value of its ID, I use something like this:
INSERT INTO MyTable (Fields)
VALUES (MyValues);
SELECT SCOPE_IDENTITY();
However, this does not work with primary keys of type UNIQUEIDENTIFIER. So the solution is:
INSERT INTO MyTable (Fields)
OUTPUT Inserted.MyPrimaryField
VALUES (MyValues);
Unfortunetly, this does not work with remote tables from linked servers, since I get an error reading:
A remote table cannot be used as a DML target in a statement which includes an OUTPUT clause or a nested DML statement.
Are there any other options?

SQL Insert Into cannot insert NULL

I have set some script that inserts data from an XML file into a SQL database. I am getting the following error.
Cannot insert the value NULL into column 'fighterID', table 'MMA Database.dbo.FIGHTERStemp'; column does not allow nulls. INSERT fails.
I have fighterID set as the primary key and will not allow NULLS. My intention is to have it number each row as they are inserted. I found one answer that advises to modify the column properties to be the identifier. However it will not let me adjust the columns properties without dropping and adding the table again.
I can do that - but what is the SQL syntax to set the identity specification settings? Am I going about this the right way?
It's pretty simple, just set the field with datatype INT (integer) and follow it with keyword IDENTITY. You can include the seed and increment values; e.g. start at 1, increment by 1, or just use the keyword IDENTITY for a 1,1 default.
CREATE TABLE MMA (FighterID INT IDENTITY (1,1), FighterInfo VARCHAR(MAX))
While inserting data into Primary key you can check the previous max id value and then increment it to next value before you insert a new row.
In SQL, you need to drop table before altering its specification. You can do this by taking backup into temp table then drop your main table and then re insert data from temp table.

How to create a SQL Server table with a column and its values to be generated automatically as a GUID

I need to design a table in SQL Server having some columns, one of these columns (ID column and use sequential uniqueidentifier) should automatically populate its data when inserting other column data.
The values of the ID column should be generated automatically when insertion happens.
Please help me to do this, any help is appreciated.
NB: I am new to this step by step approach will be more helpful
Just create a table with a column ID of datatype uniqueidentifier and set it's default value to newsequentialid():
Then, when you go insert rows into that table, just omit the ID column from the list of columns you're inserted values into:
INSERT INTO dbo.YourTable(ColA, ColB, ....., ColX)
VALUES(.., .. ,. ...)
If you don't explicitly insert a value into ID, the default specification (newsequentialid()) will be used .
As per Marc_s's comment, you should use NEWSEQUENTIALID()
CREATE TABLE myTable (ColumnA uniqueidentifier DEFAULT NEWSEQUENTIALID());
See NEWSEQUENTIALID (Transact-SQL)

Add a column in MS SQL Server table

I wanted to add a column in MS SQL Server. Will it affect any existing stored procedures or triggers or dbms jobs etc? AFAIK, in Oracle once you add a new column to an existing table, you need to recompile any invalid objects. How bout in MS SQL Server?
Adding a column to an existing table, in theory and best practices world is easy, but in reality it is not always as easy as ALTER TABLE ADD COLUMN.
The risk is that any procedure, view or application code that uses a “Select *” or an “Insert Values <…>” (without column names explicitly stated) may not function, or may not function correctly, with an additional column added to the table.
Also note there could be temp tables, table variables and table value parameters built off the table that an additional column affects as well. Best practices and my database development guidelines says not to use the “Select *” or the “Insert Values <…>”.
For example, if you have a table with ID, Name, and Status columns in your table
DECLARE #Table TABLE (id INT, name VARCHAR(20), Status INT)
INSERT INTO #Table VALUES (1,'Name')
It will throw error Column name or number of supplied values does not match table definition. Because in this query, inserting only first two column values and Status is missing. If you add Status column value will not throw any error.
So you need to always mention column names in your INSERT query wherever used
DECLARE #Table TABLE (id INT, name VARCHAR(20), Status INT)
INSERT INTO #Table (id, name) VALUES (1,'Name')
If you didn't specify the column names in INSERT query and add new column to the table will affect that query. Please make sure whether you specified the column names.

How to use default primary key as a sequence value when creating table for oracle database columns

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.

Resources