Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am passing datatable as input parameter to a stored procedure. I have created custom type for it. Here is my stored procedure:
SQL
INSERT INTO Employee
                ([Name],[Lname],[Code])
    SELECT [Name],[Lname],@Code
    FROM   @tblEmp A
    WHERE  NOT EXISTS (SELECT 1
                       FROM   Employee B
                       inner join Contactdetail c
                       on cid = B.cid
                       WHERE  B.[Name] = A.[Name]
                              AND B.[Lname] = A.[Lname]
                              AND C.[mobno] = A.[mobno]) 

Here I fetching record from datatable and inserting into Employee table. Datatable contain Name,Lname and mobileno. I want to check combination of Name,Lname and mobileno.If combination of it present in Employee table,pls don't insert record([Name], [Lname], @Code ) in Employee.Else Insert.After inserting record from Employee,I want id of all inserted record.Scope_identity give last identity of table.I want all id,which are newly inserted into table because in same sp,I want to do further processing.

So I modified my code.Now I am using like this
SQL
DECLARE @InsertedIDs table(EmployeeID int);

INSERT INTO Employee
                ([Name],[Lname],[Code])
             --
             OUTPUT INSERTED.ID
             INTO @InsertedIDs 
             --
    SELECT [Name],[Lname],@Code
    FROM   @tblEmp A
    WHERE  NOT EXISTS (SELECT 1
                       FROM   Employee B
                       inner join Contactdetail c
                       on cid = B.cid
                       WHERE  B.[Name] = A.[Name]
                              AND B.[Lname] = A.[Lname]
                              AND C.[mobno] = A.[mobno]) 

It store value of id into another table.I want to add id to that datatable or another table with all record from datatable.How to do that?
Posted
Updated 20-Apr-15 0:13am
v2

Read - OUTPUT Clause (Transact-SQL)[^].

Go to "Examples". A. Using OUTPUT INTO with a simple INSERT statement.

You can do like INSERTED.ColumnName. Just insert all these inserted column values to a new Table or something.
 
Share this answer
 
before insert, get the last id of which is present destination table and assign into a variable. then insert your records..after inserting, fetch all records from destination table which is greater then that variable your assigned.

and for this that '.If combination of it present in Employee table,pls don't insert record([Name], [Lname], @Code ) in Employee.Else Insert' i would prefer to use Merge Statement

check this.
https://msdn.microsoft.com/en-us/library/bb510625.aspx
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900