Skip to content Skip to sidebar Skip to footer

Insert Or Update Datatable Values Into A Database Table Using Stored Procedure

I have a table Cars as follows: This table will be read in grid of a C# application. The application can either edit OR delete OR add a new row. The updated grid will be take in a

Solution 1:

Use Merge statement to update,insert and delete the data http://technet.microsoft.com/en-in/library/bb522522(v=sql.105).aspx


Solution 2:

MERGE Cars AS T
USING (SELECT id, Model, Company from @dtTypeCars) AS S
ON T.id = S.id
WHEN MATCHED THEN
    UPDATE SET T.Model = S.Model,T.Company = S.Company
WHEN NOT MATCHED BY TARGET THEN
    INSERT (id,Model,Company) VALUES (S.id, S.Model, S.Company)
WHEN NOT MATCHED BY SOURCE THEN
    DELETE;

Post a Comment for "Insert Or Update Datatable Values Into A Database Table Using Stored Procedure"