SQL Server use SCOPE_IDENTITY() for insert into with select command - sql

Hello I have one table whose name is
PERSO_DATA(Id,refPerso,,Col1,Col2,Col3,Col4,Col5)
and I have
PERSO_DATA_QUEUE(Id,refPersoData,refVideo)
Firstly I will insert to datas to PERSO_DATA then I will take Id of inserted data and isnsert to another table as refPersoData.
INSERT INTO PERSO_DATA(refPerso,Col1,Col2,Col3,Col4,Col5)
SELECT #refPerso,pa.Col1,pa.Col2,pa.Col3,pa.Col4,pa.Col5 FROM #PERSODATA pa
SET #refPersoData=SCOPE_IDENTITY()
When I write like that i insert all datas but can't take the Id for each data
INSERT INTO PERSO_DATA_QUEUE(refVideoQueue,refPersoData)
SELECT #refVideoQueue,#refPersoData
so code above it add only one.But I want if I have 10 data to insert PERSO_DATA I need to also insert PERSO_DATA_QUEUE table 10 data with inserted Ids.
How can I do this? I couldn't find a solution.
Thanks in advance

Try this code:
CREATE TABLE #temp ([Id] [bigint] NOT NULL);
INSERT INTO PERSO_DATA(refPerso,Col1,Col2,Col3,Col4,Col5)
OUTPUT INSERTED.Id
INTO #temp (Id)
SELECT #refPerso,pa.Col1,pa.Col2,pa.Col3,pa.Col4,pa.Col5
FROM #PERSODATA pa;
INSERT INTO PERSO_DATA_QUEUE(refVideoQueue,refPersoData)
SELECT #refVideoQueue, Id
FROM #temp;

The solution might be combining either cursor or loop and any iterator - like ROW_NUMBER() in your temp table.
Then - use SCOPE_IDENTITY() in it to get created IDENTITY value.

Related

TSQL Copy Entry to Same Table as well as Create

is it possible to copy a single entry to two tables in one query?
Yes. You can create an insert trigger on one of the tables where you insert the data into the other. You can do the same for updates.
You can use the OUTPUT INTO clause to copy the inserted data into a second table:
CREATE TABLE #tblsrc(id int NULL);
CREATE TABLE #tbldest1(id int NULL);
CREATE TABLE #tbldest2(id int NULL);
INSERT INTO #tblsrc VALUES (99);
INSERT INTO #tbldest1
OUTPUT INSERTED.id INTO #tbldest2
SELECT id FROM #tblsrc;
SELECT * FROM #tbldest1;
SELECT * FROM #tbldest2;

How to get the value after updating the table using triggers in sql

How to get the value after updating the table using triggers in sql
Having a scenario like this:
CREATE TABLE #tmp (id INT, txt VARCHAR(10))
INSERT INTO #tmp ( id, txt )
VALUES ( 1, 'abc')
You can return the values using OUTPUT (https://msdn.microsoft.com/en-us/library/ms177564.aspx) during an UPDATE like this:
UPDATE #tmp SET
txt = 'xyz'
OUTPUT INSERTED.id, INSERTED.txt
You can return any column from your table - INSERTED will contain new data while DELETED will contain previous data.
In the Update SQL it self you can use OUTPUT.
In the triggers on MS SQL servers there are to temp tabels called INSERED and DELETED see her for more info: https://msdn.microsoft.com/en-us/library/ms191300.aspx
Hope it helps.

I need insert 2 records in differents tables,

I need to insert 2 records into 2 different tables. The problem is that the two records will be with the same Id.
For example :
I have my Mannto table, with its IdMan and oters fields. I also have my Service table, with its IdServ.
What can I do to make this one equal? I am using Postgre. The Id of the Mannto table is serial and I need to use that one as a Foreign key in the Service table
I tried the following, but it does not work:
Insert into Mannto ( idMan, field 1 field2 ...etc)
values ( default, 'f1', 'f2'...etc)
Insert into Service ( idServ, fkMannto, field1...etc)
values (default, (in this part I need the same ManntoId called idMan), 'f1')
Thank you for any help you can provide!
INSERT INTO Mannto ( field1, field2 ...etc) VALUES ( 'f1', 'f2'...etc)
RETURNING idMan;
http://www.postgresql.org/docs/current/static/sql-insert.html
When field idMan uses a sequence to create a new value, you can refer to this value in the next INSERT using CURRVAL():
BEGIN; -- start transaction
INSERT INTO Mannto ( idMan, field 1 field2 ...etc)
VALUES ( default, 'f1', 'f2'...etc);
INSERT INTO Service ( idServ, fkMannto, field1...etc)
VALUES (default, currval('name_of_the_sequence') , 'f1');
COMMIT; -- commit both inserts
Maybe it's not the best solution but you could use a trigger.
CREATE TRIGGER MannToTrigger
AFTER INSERT OR UPDATE OR DELETE ON MannTo
FOR EACH ROW EXECUTE PROCEDURE insertService();
Fetch your MannTo inserted last code and throw the procedure after each insert.

Insert into Table select result set from stored procedure but column count is not same

I need something like that which is of course not working.
insert into Table1
(
Id,
Value
)
select Id, value from
(
exec MySPReturning10Columns
)
I wanted to populate Table1 from result set returned by MySPReturning10Columns. Here the SP is returning 10 columns and the table has just 2 columns.
The following way works as long as table and result set from SP have same number of columns but in my case they are not same.
INSERT INTO TableWith2Columns
EXEC usp_MySPReturning2Columns;
Also, I want to avoid adding "." as linked server just to make openquery and openrowset work anyhow.
Is there a way not to have define table strucutre in temp table (all columns with datatypes and lenght)? Something like CTE.
You could use a temporary table as a go-between:
insert into #TempTable exec MySP
insert into Table1 (id, value) select id, value from #TempTable
You could solve the problem in two steps by doing the insert from the stored procedure into a temporary table, then do the insert selecting just the columns you want from the temporary table.
Information on temporary tables: http://www.sqlteam.com/article/temporary-tables
-- Well, declare a temp table or a table var, depending on the number of rows expected
-- from the SP. This table will be basically the result set of your SP.
DECLARE #spResult AS TABLE
(
ID INT,
VALUE FLOAT,
....
);
-- Get the result set of the SP into the temp table.
INSERT #spResult EXEC STORED_PROC;
-- Now you can query the SP's result set for ID and Value;
INSERT Table1 (ID, VALUE)
SELECT ID, VALUE FROM #spResult;
You dont need to create a temporary table, you can do it with single query by creating temporary view like this
with tempView as EXEC MySPReturning10Columns insert into Table1 select id, value from tempView
The temporary view disappears as soon as the statement finishes execution

Getting the identity of the row I just inserted

I've got two tables that are linked with foreign keys. I need to do a few inserts on one table and then use the identity column values are part of my next batch of insert statements. This all has to be done through SQL.
I've been reading about SCOPE_IDENTITY() but all the examples I'm seeing are using ASP/PHP/Whatever to do the substitution in the next batch of inserts, I dont have this option.
Any pointers appreciated.
Use
SCOPE_IDENTITY() - Function
instead of
##IDENTITY Variable -
otherwise you get the result of the last inserted identity - if there is a trigger on the table than does inserting somewhere you get the wrong result back (the value inserted by the trigger and not by your statement). Scope_Identity returns the identity of your statement and that is what you normally want.
IN SQl Server 2008 you can also use the OUTPUT clause.
DECLARE #output TABLE (myid INT)
INSERT mytable (field1, field2)
OUTPUT inserted.myid INTO #output
VALUES ('test', 'test')
SELECT * FROM #output
What makes this espcially valuable is if you use it to insert a bunch of records instead of one you can get all the identities, You can also return any of the other fields you might need.
something like:
DECLARE #output TABLE (myid INT, field1 datetime)
INSERT mytable (field1, field2)
OUTPUT inserted.myid, inserted.field1 INTO #output
Select '20100101', field3 from mytable2
SELECT * FROM #output
You can do the same with SQL or TSQL. Just assign the identify column as soon as you do the insert.
-- Variable to hold new Id
Declare #NewId int
--Insert some values in to create a new ID
Insert Into dbo.MyTable1 (Col1)
Values (#NewValue)
-- Grab the new Id and store it in the variable
Select #NewId = Scope_Identity()
--Insert the new Id in to another table
Insert Into dbo.AnotherTable (Col1)
Values (#NewId)
You can use
SELECT SCOPE_IDENTITY() [Iden]
After the insert statement in the same block. This should work.