Thursday, March 8, 2012

Convert SQL query to SP

I have the following sql query

select A,B,sum(C),D

from Y inner join X on..inner join Z on...

where A like ..., B between ...

group by A,B,D

as a result of this select I get list of A,B,sum(C),D values

Ho do I create a SP that updates some table W with list of selected A,B,sum(C),D values ?

Should I use cursor for this or not?

Thank you!!!!!

Enclose your update statement with Create Proc...

No need to have cursor to update your values (use Update...From syntax)

here the sample,

Code Snippet

Create proc YourSpname

as

Update sometable

set

sometablecol = sumofc

From

sometable main

Join

(

select A,B,D,sum(c) as sumofc

from Y inner join X on..inner join Z on...

where A like ..., B between ...

group by A,B,D

) as data

On data.a = main.a and data.b=main.b and data.d=main.d

No comments:

Post a Comment