Multiple row insert into two tables avoiding loops - sql

I have a set of value which have to be inserted into two tables.Input has say 5 row and I have to insert these 5 rows into table A first.Table A has a identity column.Next i have to insert these 5 rows into table B with an extra column which is the identity from table A.
How this can be done with out using any loops?
Any help will be highly helpful.
INSERT INTO TABLE_A(COL2,COL3)
SELECT COL2,COL3 FROM #TEMP_TAB
set #identityval=##identity
INSERT INTO TABLE_B(COLA,COLB,COLC)
SELECT #identityval,COL2,COL3,COL4 FROM #TEMP_TAB

You cannot insert into multiple tables using a single statment.
What you could do is create an insert trigger on Table A so that after the insert occurs this performs the new insert with the identity of the value inserted into Table A and insert it into Table B.

Here is one solution.
take max of identity column from table TABLE_A
insert new records in table TABLE_A
then insert records on TABLE_B from TABLE_A with Identity greater than last max identity.
Thanks,
Gopal

What you want to do is not possible.
You can get only the value from the last insert using the ##identity variable. This way its possible to add to multiple tables setting the right foreign key without selecting the just inserted row again using a cursor. This approach is not useful when inserting multiple rows at once.
From the documentation:
Use the ##identity global variable to retrieve the last value inserted into an IDENTITY column. The value of ##identity changes each time an insert or select into attempts to insert a row into a table.
Here is a procedure which inserts a single row and you can use the return value to create a reference to the inserted data in another table:
create procedure reset_id as
set identity_insert sales_daily on
insert into sales_daily (syb_identity, stor_id)
values (102, "1349")
select ##identity
select ##identity
execute reset_id

Related

How to insert row in sql sever table if table contains only two columns one is identity and other one is default constraint [duplicate]

(Came up with this question in the course of trying to answer this other one)
Consider the following MS-SQL table, called GroupTable:
GroupID
-------
1
2
3
where GroupID is the primary key and is an Identity column.
How do you insert a new row into the table (and hence generate a new ID) without using IDENTITY_INSERT ON?
Note that this:
INSERT INTO GroupTable() Values ()
... won't work.
edit: we're talking SQL 2005 or SQL 2008 here.
This should work:
INSERT INTO GroupTable DEFAULT VALUES
Here you go:
INSERT INTO GroupTable DEFAULT VALUES
It is possible to insert more than one row at a time.
For e.g., to insert 30 rows.
INSERT INTO GroupTable DEFAULT VALUES
GO 30
This will insert 30 rows by incrementing the identity column each time.
Can you try using a Sequence or something similar? Where you select from a Sequence and it will give you the next value in the sequence.
This will work actually--
insert into TABLE default values

When inserting a new row in table A, another row also gets inserted in table B

So I am working with these two tables, Dependency and DependencyList.
I am trying to write an SQL query that, when a new row gets created in table Dependencies, another one also gets created in DependencyList, having the DependencyId filled with the Id of the newly created Dependency.
The Id column of both tables is auto-incremented.
Would that be possible, in any way?
You would typically do this with a FOR INSERT trigger. You can access the previously generated id using the inserted pseudo-table.
CREATE TRIGGER myTrigger ON Dependencies FOR INSERT
AS INSERT INTO DependencyList (DependencyId) SELECT Id FROM inserted;
Demo on DB Fiddlde
You can also do this with two insert statements, using SCOPE_IDENTITY() to retrieve the last insert id:
INSERT INTO Dependencies(isResolved, AssignedToTaskId) VALUES(0, 0);
INSERT INTO DependencyList (DependencyId) SELECT SCOPE_IDENTITY();
Demo

Insert Multiple Rows into Table from a Table

I have a SQL Server 2008 database. The database has a stored procedure which receives two strings as parameters. One parameter is used to build a temp table which will usually only have 1 or 2 rows but theoretically could have more.
For each row in the temp table, I need to insert a row into a different table that consists of the other parameter and the contents of the temp table. Is there a way to do this without a cursor?
I've tried variations on the following:
Pseudo code:
procedure InsertLinks(#Key varchar(36), #LinkKey varchar(36)
tempLinks Table = getLinks(#LinkKey)
Insert into MyTable (Key, LinksTo) Values (#Key, Select LinksTo From tempLinks)
The VALUES clause is messed up - you have a single value comma a table. That's not valid.
The following should work just fine:
INSERT INTO MyTable (Key, LinksTo)
SELECT #Key, LinksTo
FROM tempLinks

SQL Server INSERT trigger how to get data of current row

I've never created a trigger before and I'm trying to read online but am a little confused.
I want to create a trigger on a table that on insert, it will grab some data from different columns and insert it into a few different other tables.
I'm not sure how to write the T-SQL to get the data from the columns..
insert into [othetTable]
values ([col1 from row that was inserted], [col5 from row that was inserted])
What would the syntax be to get those values?
thanks
Use the inserted virtual table that is available to triggers. Note that there could be multiple rows in this table - your trigger could be processing multiple inserts at once.
Therefore, you need to use something like the following syntax:
insert into othertable
select col1, col5
from inserted
This will insert a row into othertable for each inserted row.

how to compare the values inside a table in sql

how to
compare the values of same table(say for eg: Order table) each and every time the record get inserted ,
if the record with same values get inserted already in same table i should not insert the new record with same values. how to do that exactly in sql server 2008
If exists(select * from Order where key_column=#some_value)
print 'data already exists'
else
Insert into Order(columns) values (#some_value,...)
I'd suggest adding a unique index on the key columns...
ALTER TABLE mytable ADD UNIQUE INDEX myindex (keycolumn1, keycolumn2, ...);
That'd make it impossible to insert a duplicate by accident.