Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Converting code into a function

Author  Topic 

SergioM
Posting Yak Master

170 Posts

Posted - 2012-05-14 : 20:12:19
This is a truncated version of a MERGE command I have. This merges the contents of one table to another using the UPC as the key. However, I need to do this with several tables, so I would like to reuse the code. Can I just make it into a function? I just need 'Dist1' to be a variable wherever it appears & to call the function 5-10 times.

MERGE INTO [MyDBName].[dbo].[FinalList] AS targetDB
USING [MyDBName].[dbo].[Dist1] AS sourceDB
ON sourceDB.UPC = targetDB.UPC

WHEN MATCHED THEN
UPDATE SET
Dist1_Price = sourceDB.Price,
Dist1_Qty = sourceDB.Quantity;

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-14 : 22:14:03
you need to make into dynamic string if you want this to reused for several objects. But question is why to do that as your columns will also be different from objects. Or are you talking objects which are all similar in structure?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -