Multiple insert identity get - sql-server-2005

My query in Stored procedure looks like this.
insert into a(name) select name from b.
I want to get all the new identity value from a.
How can i do this?
Thanks in advance.

insert into a(name)
OUTPUT INSERTED.IDColumn --this line for >1 row
select name from b.
The OUTPUT Clause

Have a look at OUTPUT Clause (Transact-SQL)
Something like
DECLARE #a TABLE(
ID INT IDENTITY (1,1),
Name VARCHAR(250)
)
DECLARE #b TABLE(
Name VARCHAR(250)
)
INSERT INTO #b SELECT 'a'
INSERT INTO #b SELECT 'b'
INSERT INTO #b SELECT 'c'
INSERT INTO #b SELECT 'd'
INSERT INTO #a(name)
OUTPUT INSERTED.ID
SELECT name FROM #b

Related

Inserting into a Table the result between a variable and a table parameter

Having the following procedure:
CREATE PROCEDURE [dbo].[Gest_Doc_SampleProc]
#Nome nvarchar(255),
#Descritivo nvarchar(255),
#SampleTable AS dbo.IDList READONLY
AS
DECLARE #foo int;
SELECT #foo=a.bar FROM TableA a WHERE a.Nome=#Nome
IF NOT EXISTS (SELECT a.bar FROM TableA a WHERE a.Nome=#Nome)
BEGIN
INSERT INTO TableA VALUES (#Nome,#Descritivo)
INSERT INTO TableB VALUES (scope_identity(),#SampleTable)
END
I am trying, as shown, inserting into TableB all the values of SampleTable, together with the scope_identity.
SampleTable is as:
CREATE TYPE dbo.SampleTable
AS TABLE
(
ID INT
);
GO
How can I correctly achieve this?
The right way to do this type of work is the OUTPUT clause. Although technically not needed for a single row insert, you might as well learn how to do it correctly. And even what looks like a single row insert can have an insert trigger that does unexpected things.
PROCEDURE [dbo].[Gest_Doc_SampleProc] (
#Nome nvarchar(255),
#Descritivo nvarchar(255),
#SampleTable AS dbo.IDList
) READONLY AS
BEGIN
DECLARE #ids TABLE (id int);
DECLARE #foo int;
SELECT #foo = a.bar
FROM TableA a
WHERE a.Nome = #Nome;
IF NOT EXISTS (SELECT 1 FROM TableA a WHERE a.Nome = #Nome)
BEGIN
INSERT INTO TableA (Nome, Descritive)
OUTPUT Inserted.id -- or whatever the id is called
INTO #ids;
VALUES (#Nome,#Descritivo)
INSERT INTO TableB (id, sampletable)
SELECT id, #SampleTable
FROM #ids;
END;
END; -- Gest_Doc_SampleProc
In addition to using OUTPUT, this code also adds column lists to the INSERTs. That is another best practice.

store result of select query into array variable

I want to store the result of this sql query in variable #a. The result contains 17 rows. How to edit this code in order to store the rows in #a?
declare #a uniqueidentifier
select EnrollmentID into #a from Enrollment
You cannot store 17 values inside a scalar variable. You can use a table variable instead.
This is how you can declare it:
DECLARE #a TABLE (id uniqueidentifier)
and how you can populate it with values from Enrollment table:
INSERT INTO #a
SELECT EnrollmentID FROM Enrollment
You should declare #a as a Table Variable with one column of type unique identifier as follows:
DECLARE #a TABLE (uniqueId uniqueidentifier);
INSERT INTO #a
SELECT EnrollmentID
FROM Enrollment;

Get Scope identity for multiple inserts

For table1 Inserted 3 records
It should get those three identities and it should insert 3 records in table3 (but it’s not happening- it inserts 3 records with same identity ie.last scope identity)
create table table1(ID INT identity(1,1),Name varchar(50))
insert into table1 values('Ram'),('Sitha'),('Laxman')
create table table1(ID INT identity(1,1),Name varchar(50))
create table table3(ID INT ,Name varchar(50))
insert into table2(Name)
select Name from table1
declare #id int;
set #id= (select scope_Identity())
begin
insert into table3(ID,Name)
select #id,Name from table2
end
select * from table2
select * from table3
How can get all identities to insert do I need to write a loop (or) do I need to Create a trigger.
Please give me a solution I am strugguling from past 4 hours.
Thanks in anvance
Use the OUTPUT clause to handle multi-row inserts:
INSERT INTO dbo.table2(Name)
OUTPUT inserted.ID, inserted.Name INTO table3
SELECT Name FROM dbo.table1;
You can use the OUTPUT clause to get the identity from any number of inserts.
create table table1(ID INT identity(1,1),Name varchar(50))
DECLARE #T1 Table (ID int, name varchar(50))
insert into table1
OUTPUT inserted.ID, Inserted.Name INTO #T1
values('Ram'),('Sitha'),('Laxman')
DECLARE #IdentityId INT,#Count INT=1
DECLARE #temp AS TABLE (Id INT IDENTITY ,Name NVARCHAR(100))
INSERT INTO #temp(Name)
SELECT Name FROM table1
WHILE #Count <=(SELECT COUNT(SId) FROM #temp)
BEGIN
INSERT INTO table2(Name)
SELECT Name FROM #temp
WHERE Id=#Count
SET #IdentityId = SCOPE_IDENTITY()
INSERT INTO tabel3(#IdentityId,Name)
SELECT 3, #IdentityId,1,GETDATE()
SET #Count=#Count+1
END

INSERT ONLY SPECIFIC COLUMN FROM A STORED PROCEDURE RESULT

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

T-SQL - table variable select

I just wondering how to select values from table B according to table A col values; The idea is quite simple but I have confused a little
code like a
DECLARE #A TABLE
(
id INT NOT NULL,
name VARCHAR(50)
);
INSERT #A SELECT id,name FROM table1 WHERE id>10
DECLARE #B TABLE
(
address VARCHAR(255),
city VARCHAR(128)
);
INSERT #b SELECT address,city FROM table2
WHERE id=(SELECT id FROM #A)
Change "id =" to "id IN"
WHERE id=(SELECT id FROM #A)
to
WHERE id IN (SELECT id FROM #A)