I have this SQL Server query with a cursor:
DECLARE #ids TABLE(id varchar(50))
INSERT INTO #ids VALUES ('1098264', '1098859', '1098860', '1098267', '1098265')
But when I run the code, I get an error:
Insert Error: Column name or number of supplied values does not match table definition.
You have only one column in table i.e id and you are trying to insert 5 column values in table.
Try this
Create table :
DECLARE #ids TABLE(id varchar(50))
Insert values to table :
INSERT INTO #ids VALUES ('1098264')
INSERT INTO #ids VALUES ('1098859')
INSERT INTO #ids VALUES ('1098860')
INSERT INTO #ids VALUES ('1098267')
INSERT INTO #ids VALUES ('1098265')
You need to use a pair of bracket for each row like below:
INSERT INTO #ids VALUES ('1098264'),('1098859'),('1098860'),('1098267'),('1098265')
There are three ways of inserting multiple rows into a table (other than via a select statement)
INSERT INTO FOO (columna,columnb)
VALUES (1,a)
INSERT INTO FOO (columna,columnb)
VALUES (2,b)
INSERT INTO FOO (columna,columnb)
VALUES (3,c)
The second
INSERT INTO FOO (columna,columnb)
select 1,'a'
UNION ALL
select 2,'b'
UNION ALL
select 3,'c'
Thirdly (only works SQLServer 2008 and up)
INSERT INTO FOO (columna,columnb)
VALUES (1,'a'),(2,'b'),(3,'c')
In the example in your question, you were trying to insert multiple fields into one column, which is why you got the error.
You have only 1 column in the table but are trying to insert 5 values.
First declare 5 different columns in the table to hold 5 values
For Example:-
declare #IDS_1 varchar(50)
,#IDS_2 varchar(50)
,#IDS_3 varchar(50)
,#IDS_4 varchar(50)
,#IDS_5 varchar(50)
INSERT INTO #IDS VALUES ('1098264'
,'1098859'
,'1098860'
,'1098267'
,'1098265')
Related
I have one issue, I'm not too sure is it possible by anyway or not.
suppose-
I have a physical table with 4 columns but I have to insert data into it from temp tables with 1000's of records but with less column (i.e. 2 columns) Is it possible by anyway.
I have added a temporary script to describe my problem-
create table A (id int,sal int,name varchar(50),data varchar(50))
create table #B (id int,sal int)
insert into #B values(1,10)
insert into #B values(2,20)
insert into #B values(3,30)
insert into #B values(4,40)
---This will Not work
insert into A select * from #B
Is there any other way we can do this, I have added just a scenario but i have a lot of columns in my physical table
create table A (id int,sal int,name varchar(50),data varchar(50))
create table #B (id int,sal int)
insert into #B values(1,10)
insert into #B values(2,20)
insert into #B values(3,30)
insert into #B values(4,40)
-This will work
insert into A select *,null,null from #B
-- or
insert into A (id,sal) select * from #B
How about specifying the columns during insert i.e.
insert into A(id,sal) select * from #B
I want to know if it is possible to insert to a table from a specific column of result from a stored procedure?
Something like:
declare #temp as table(
id int
)
insert #temp
exec getlistofStudents --returns multiple columns
this is an example only, Thanks for the help..
You can take a 2 step approach. First INSERT INTO a #TempTable, then populate the #TempVariable with another INSERT INTO, selecting the single column.
DECLARE #temp AS TABLE
(
ID int
);
CREATE TABLE #tempTable1
(
Column1 int,
Column2 int
);
INSERT INTO #tempTable1
Exec getlistofStudents
INSERT INTO #temp
SELECT Column1 FROM #tempTable1
In my database all tables are using a common table for Sequence(ID_Table).
TABLE_ID has two fields (Common_ID, Table_Name).
If I insert any record in the table, I have to first insert a record in Table_ID(Auto-increment, Table_name) then use that Auto-increment value in my Other Table.
For example, I want to insert in Table_Products which has fields ID(Common_ID), Product_Name, Product_ID(Auto Increment)
I want to do something like this:
INSERT INTO TABLE_ID (Table_NAME), Values (Table_Products)
Get the Inserted ID and use it in Table_Products:
INSERT INTO Table_Products (ID, Product_Name, Product_ID(Auto Increment)
VALUES (ID from TABLE_ID, SomeProduct, Increment)
Try this one -
DECLARE #ID BIGINT
INSERT INTO dbo.TABLE_ID (Table_NAME)
SELECT 'Table_Products'
SELECT #ID = SCOPE_IDENTITY()
INSERT INTO dbo.Table_Products (ID, Product_Name)
SELECT #ID, 'SomeProduct'
You can use an insert statement with the output clause to generate a new Common_ID. Using insert ... select, you can specify that ID in an insert operation:
declare #Common_ID as table(ID int)
insert Table_ID
(Table_Name)
output inserted.Common_ID into #Common_ID
values ('Table_Products')
insert Table_Products
(ID, Product_Name)
select ID
, 'Some Product'
from #Common_ID
Use SCOPE_IDENTITY() after ur insert statementto get the last inserted id.
DECLARE #Product_Id int
INSERT INTO TABLE_ID (Table_NAME) VALUES (Table_Products);
SELECT #Product_Id=SCOPE_IDENTITY();
Insert INTO Table_Products (ID, Product_Name)
VALUES (ID from TABLE_ID, 'SomeProduct')
Dear friend you have to select id of last record inserted
and then pass it in another table so bellow code will help you very well
Insert INTO TABLE_ID (Table_NAME), Values (Table_Products)
DECLARE #ID int;
set #ID = SCOPE_IDENTITY();
Insert INTO Table_Products (ID, Product_Name)
Values (#ID, SomeProduct)
this code will solve your problem i define #ID for your last record id and then insert it in your other table
For MySql use select LAST_INSERT_ID();
All the other answers so far declare intermediary variables for SCOPE_IDENTITY(), but it could be simpler:
INSERT INTO dbo.TABLE_ID (Table_NAME) VALUES 'Table_Products';
INSERT INTO dbo.Table_Products (ID, Product_Name) VALUES (SCOPE_IDENTITY(),'SomeProduct');
You could be also more table-specific using IDENT_CURRENT()
Insert INTO TABLE_ID (Table_NAME), Values (Table_Products)
select #NewID=IDENT_CURRENT('TABLE_ID')
Insert INTO Table_Products (ID, Product_Name, Product_ID
Values (#NewID, SomeProduct, Increment)
I would like to INSERT multpile rows (using INSERT SELECT), and OUTPUT all the new and old IDs into a "mapping" table.
How can I get the original ID (or any source values) in the OUTPUT clause? I don't see a way to get any source values there.
Here is a minimal code example:
-- create some test data
declare #t table (id int identity, name nvarchar(max))
insert #t ([name]) values ('item 1')
insert #t ([name]) values ('another item')
-- duplicate items, storing a mapping from src ID => dest ID
declare #mapping table (srcid int, [newid] int)
insert #t ([name])
output ?????, inserted.id into #mapping-- I want to use source.ID but it's unavailable here.
select [name] from #t as source
-- show results
select * from #t
select * from #mapping
My actual scenario is more complex, so for example I cannot create a temp column on the data table in order to store a "original ID" temporarily, and I cannot uniquely identify items by anything other than the 'ID' column.
Interesting question. For your example, a possible cheat is to depend on the fact that you are doubling the number of rows. Assuming that rows are never deleted and the [id] column remains dense:
-- create some test data
declare #t table (id int identity, name nvarchar(max))
insert #t ([name]) values ('item 1')
insert #t ([name]) values ('another item')
-- duplicate items, storing a mapping from src ID => dest ID
declare #mapping table (srcid int, [newid] int)
declare #Rows as Int = ( select Count(42) from #t )
insert #t ([name])
output inserted.id - #Rows, inserted.id into #mapping
select [name] from #t as source order by source.id -- Note 'order by' clause.
-- show results
select * from #t
select * from #mapping
Just as a preface, I'm not very knowledgeable on T-SQL syntax.
I'd like to create a simple SQL script that will make 3 insert statements.
Insert A
Insert B
Insert C
Insert A statement's identity or "ID" would be needed in insert B statement.
And both the identities Inserts A & B would be needed in Insert C statement.
Pseudo code would look something like:
INSERT INTO tableA
VALUES ('blah', 'blah')
INSERT INTO tableB
VALUES (IDENTITY_FROM_A_INSERT, 'foo')
INSERT INTO tableC
VALUES (IDENTITY_FROM_A_INSERT, IDENTITY_FROM_B_INSERT)
How would I go about writing this script?
Use SCOPE_IDENTITY() after each insert in order to get the identity of the inserted row (in the current session).
I have used two variables to capture the two identities and then insert them into the third table:
DECLARE #Id1 INT
DECLARE #Id2 INT
INSERT INTO tableA VALUES ('blah', 'blah')
SET #Id1 = SELECT SCOPE_IDENTITY()
INSERT INTO tableB VALUES (IDENTITY_FROM_A_INSERT, 'foo')
SET #Id2 = SELECT SCOPE_IDENTITY()
INSERT INTO tableC VALUES (#Id1, #Id2)
scope_identity() is perfect for integer identifiers on single-record insertions (+1 to the other answer btw). However, if you find yourself using a guid/uniqueidentifier (newsequentialid(), newid(), etc) or inserting multiple records at once, you'll need something a little different:
declare #id uniqueidentifier;
-- Table variable for storing your newly inserted identifiers:
declare #NewlyInsertedIds table
(
[Id] uniqueidentifier
);
insert [MyTable]
(
[Blah1]
,[Blah2]
)
-- in the output clause you can access the inserted/deleted pseudo tables:
ouptut inserted.[Id]
into #NewlyInsertedIDs
(
[Id]
)
values
(
'Blah'
,'Blah'
);
select
#id = [Id]
from #NewlyInsertedIds;
Check out the OUTPUT Clause for more information.