Adding auto-incremented values to a table with one column - sql

I need to create a table that basically keeps a list of indices only. Therefore I've created a table with just one, auto-incremented column called 'id'. However, I can't seem to implicitly add auto-incremented values to this table.
I know that usually when you have such a column in a table (with more than just this column) you can do:
INSERT INTO TABLE (col1, col2 ...) VALUES (val1, val2 ...)
And if you don't specify the auto-incremented column, it would automatically get a value. However, things like:
INSERT INTO TABLE () VALUES ()
INSERT INTO TABLE
INSERT INTO TABLE ()
etc. all produce an error on my single-columned table. Can anyone offer a solution?
Thanks.
p.s. I'm using Sqlite, in case it matters.

Try this
INSERT INTO dbo.Table DEFAULT VALUES
See this answer:
Previous answer

Try the following:
INSERT INTO YOUR_TABLE(YOUR_ID) VALUES (NULL);

Related

Creating New GUID automatically while inserting a new row to an existing table Not Working

I have an existing table in MS SQL called myTab.
It has the following fields
empno(PK) nchar(10),
age int
Now, i want to add a myGUID column and fill it up with a GUID whenever i insert a new row as well as Updating existing rows.
So i added the statement
ALTER TABLE myTab ADD myGUID uniqueidentifier DEFAULT NewId() NOT NULL;
Updating existing rows works correctly.
But, when i try to insert values,
INSERT INTO myTab VALUES ( 1000, 22 );
It fails, and gives the following message
**Column name or number of supplied values does not match table definition.**
When i do
insert into sourav_test2 values (20055711,23,NEWID());
The above statement works.
I want a GUID to be filled without changing the insert statement. Is it possible via a Trigger or a Function?
Always list the columns you are inserting!
INSERT INTO myTab (empno, age)
VALUES ('1000', 22);
Also use correct types for the values. Unmentioned columns will be assigned their default values, or NULL if there is no explicit default.
Your table has three columns, so if you leave out the column list, then the insert expects three values. You can still set a default, if you want by using the DEFAULT keyword in the VALUES clause:
INSERT INTO myTab (empno, age, myGUID)
VALUES ('1000', 22, DEFAULT);
Sourav's question about triggers got me thinking, so I tried a little test. Why?
Imagine a scenario where an application has already been written with thousands of INSERT statements that leave off the column list. In this case, if you could write an INSTEAD OF INSERT trigger that provides the column list, you could hopefully save yourself from correcting thousands of INSERT statements due to a newly added column.
Off the top of my head, I admittedly did not know if this could work.
So I wrote this little test:
CREATE TABLE tt (ColA varchar(1));
INSERT INTO tt VALUES ('a');
ALTER TABLE tt
ADD ColB uniqueidentifier DEFAULT NEWID();
GO
CREATE TRIGGER tr_tt
ON tt
INSTEAD OF INSERT
AS
INSERT INTO tt (ColA)
SELECT ColA FROM inserted;
GO
INSERT INTO tt VALUES ('a');
SELECT * FROM tt;
DROP TABLE tt;
I also tried a variation of the TRIGGER with the following INSERT just to be thorough:
INSERT INTO tt (ColA, ColB)
SELECT ColA, NEWID() FROM inserted;
The result was the same in both cases: The same error as reported in the question. So to answer the question:
Can't we use a trigger here which can do it?
The answer is NO. Even if you put an INSTEAD OF INSERT TRIGGER on the table, the parser will still not let you write an INSERT..VALUES() statement unless the number and order of VALUES exactly matches the definition of the table. A TRIGGER cannot be used to get around it.
Sooner or later, lazy coding exacts its price.

Inserting Values Into Table with Identity Column via Databricks

I've created a table in Databricks that is mapped to a table hosted in an Azure SQL DB. I'm trying to do a very simple insert statement on a small table, but an identity column is giving me issues. This table has the aforementioned identity column and three additional columns.
I first tried something similar to below:
%sql
INSERT INTO tableName (col2, col3, col4)
VALUES (1, 'Test Value', '2018-11-16')
That was giving me a syntax error, so I did some searching and learned that Hive SQL doesn't allow you to specify columns for an INSERT statement. So then I tried something like below as a test:
%sql
INSERT INTO tableName
VALUES (100, 1, 'Test Value', '2018-11-16')
That gives me an error message that I can't insert explicit values into an identity column, but that's what I expected to happen.
If I can't specify the columns for my INSERT statement, how do I avoid issues when I have an identity column? I just want to insert values for the non-identity columns, and I want the ID column to continue incrementing like normal. The above example is extremely watered-down. I will need to do much larger insertions based on SELECT statements eventually, so any solution involving toggling on IDENTITY_INSERT probably isn't feasible.
Below is how we can create a table with an identity column -
CREATE TABLE table_name
(column_name1 data_type GENERATED ALWAYS AS IDENTITY,
column_name2......)
Below are the two ways how we can insert the data into the table with the Identity column -
First way -
INSERT INTO T2 (CHARCOL2)
SELECT CHARCOL1 FROM T1;
Second way -
INSERT INTO T2 (CHARCOL2,IDENTCOL2) OVERRIDING USER VALUE
SELECT * FROM T1;
Links for reference-
Create table - https://docs.databricks.com/sql/language-manual/sql-ref-syntax-ddl-create-table-using.html
Insert into table - https://www.ibm.com/docs/en/db2-for-zos/11?topic=statement-rules-inserting-data-into-identity-column

sql insert into a table with computed columns

I have a table with 4 columns, say COLA,COLB,COLC,COLD. COLC and COLD are computed columns.
Suppose I want to insert a row into table, should the query be something like
insert into Table (COLA,COLB,COLC,COLD) values (1,2,'','')?
I know I can't insert into computed columns. But how can I add a row and keep the default computed columns as they are?
Thanks for any advice!
try this
INSERT INTO TABLE (COLA,COLB) values (1,2);
you dont need to provide the values even blanks for computed column. They get calculated automatically
Just specify the columns you want to insert values into:
insert into Table (COLA,COLB) values (1,2)
Just don't specify the calculated columns:
insert into Table (COLA,COLB) values (1,2)
Thank you all! I just got that out too! Previously I got errors that number of col don't match, because I missed a a column.

Can't INSERT INTO SELECT into a table with identity column

In SQL server, I'm using a table variable and when done manipulating it I want to insert its values into a real table that has an identity column which is also the PK.
The table variable I'm making has two columns; the physical table has four, the first of which is the identity column, an integer IK. The data types for the columns I want to insert are the same as the target columns' data types.
INSERT INTO [dbo].[Message] ([Name], [Type])
SELECT DISTINCT [Code],[MessageType]
FROM #TempTableVariable
This fails with:
Cannot insert duplicate key row in object 'dbo.Message' with unique index
'IX_Message_Id'. The duplicate key value is (ApplicationSelection).
But when trying to insert just Values (...) it works ok.
How do I get it right?
It appears that the data "ApplicationSelection" is already in the database. YOu need to write the select to exclude records that are already in the database. YOu can do that with a where not exists clause or a left join. LOok up teh index to see what field is unique besides the identity. That will tell you what feild you need to check to see if teh record currently exists.

vbscript/sql adding a column to an insert statement

I am trying to add an additional column and value to an existing insert query - both integers, and running into trouble.
Anything to look out for?
you don't give much to go in in your question:
I am trying to add an additional
column and value to an existing insert
query - both integers, and running
into trouble.
Anything to look out for?
it is best practice to list all columns you intend to include values for in the list of columns, so make sure you add them there, as well as the VALUES list:
insert into YourTable (col1, col2,..., newCol1, newCol2)
VALUES (1,2,...,new1, new2)
make sure the you get the column names spelled correct and that the table actually has those new columns in it.
make sure the column name sequence is the same as your insert data sequence.
Example
INSERT INTO TABLENAME
(ColumnName1,ColumnName2) VALUES (1,'data')
Becomes
INSERT INTO TABLENAME
(ColumnName1,ColumnName2,ColumnNameNEW) VALUES (1,'data','newcolumndata')
Notice both the new column name and the new data are in the third position in the sequence.