SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Stored Procedure to insert and update
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

taj
Starting Member

India
39 Posts

Posted - 06/15/2013 :  00:13:15  Show Profile  Reply with Quote
Hi all,

I have 2 tables,A and B with same columns.

Now I want to write a single stored procedure which insert data into table A and in the same time update with the same data into table B .

SwePeso
Patron Saint of Lost Yaks

Sweden
30282 Posts

Posted - 06/15/2013 :  03:05:06  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Use Composable DML. See http://weblogs.sqlteam.com/peterl/archive/2009/04/08/Composable-DML.aspx




N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

taj
Starting Member

India
39 Posts

Posted - 06/15/2013 :  07:50:10  Show Profile  Reply with Quote
quote:
Originally posted by SwePeso

Use Composable DML. See http://weblogs.sqlteam.com/peterl/archive/2009/04/08/Composable-DML.aspx




N 56°04'39.26"
E 12°55'05.63"




If you don't mind can u please explain me with an small example.

I request all forum peoples to workout on this and help me out in getting a fine solution for this requirement.
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3760 Posts

Posted - 06/15/2013 :  09:25:03  Show Profile  Reply with Quote
quote:
Originally posted by taj

quote:
Originally posted by SwePeso

Use Composable DML. See http://weblogs.sqlteam.com/peterl/archive/2009/04/08/Composable-DML.aspx




N 56°04'39.26"
E 12°55'05.63"




If you don't mind can u please explain me with an small example.

I request all forum peoples to workout on this and help me out in getting a fine solution for this requirement.

That article SwePeso pointed to has a simple example; the third piece of code that he is showing there is what you want to use (assuming you are on SQL 2008 or later).

If you need more specific help, post the DDL for your tables, so someone can write a query tailored to that. If you need help in posting the DDL, take a look at this article: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30282 Posts

Posted - 06/15/2013 :  09:31:46  Show Profile  Visit SwePeso's Homepage  Reply with Quote
UPDATE  dbo.TableB
SET     CustomerID = CustomerID,
        Value = 'My New Value',
        LastEdited = GETDATE()
OUTPUT  inserted.CustomerID
        inserted.Value,
        inserted.LastEdited
INTO    dbo.TableA
        (
            CustomerID,
            Value,
            LastEdited
        );



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000