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
 automatic feeding parameters to an sp

Author  Topic 

allan8964
Posting Yak Master

249 Posts

Posted - 2012-11-16 : 23:18:05
Hi there,

I am not sure if this is possible that automatically let a table feed a stored procedure with parameters:

tableParam has columns:
ID | Year | Month | Week |

A stored procedure run as
exec sp_ClculateValue year, month, week

here year, month and week are the parameters coming from table tableParam with year, month, week order. How can i let the table feed the sp with all parameters in the order of
exec sp_ClculateValue 2010, Jan, week1
exec sp_ClculateValue 2010 Feb, week2
...
exec sp_ClculateValue 2010 Dec week1
...
exec sp_ClculateValue 2011, Jan, week1 ...
exec sp_ClculateValue 2011, Feb, week1
exec sp_ClculateValue 2011, Dec, week1

till all values in tableParam run out?
thanks in advance.


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-11-16 : 23:22:35
you've to use a cursor based logic in t-sql for this

Is this requirement to do it native sql or one to call from ssis package?

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

Go to Top of Page

allan8964
Posting Yak Master

249 Posts

Posted - 2012-11-16 : 23:31:13
thanks visakh16 for quick reply. it's goona be called from an SSIS package.
could you plz be little more specific on how to use cursor for this? If it takes much time any link will help too. thanks again.
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-11-17 : 10:06:06
If you are using SSIS, you can use a variable to hold the recordset with a foreach loop container to execute the stored procedure for each row in the variable. There is an example here: http://redmondmag.com/articles/2009/11/02/loop-thru-rows.aspx
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-11-19 : 04:23:39
Another method to do is

declare @sql varchar(max)
set @sql=''
select @sql=@sql+' exec sp_ClculateValue '+cast(year as varchar(4))+','+cast(month as varchar(2))+','+cast(week as varchar(4)) from table
order by id
select @sql

If the select gives all you want , add exec(@sql) at the end

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-11-20 : 01:42:15
quote:
Originally posted by allan8964

thanks visakh16 for quick reply. it's goona be called from an SSIS package.
could you plz be little more specific on how to use cursor for this? If it takes much time any link will help too. thanks again.



if its called from an ssis package you can do the following

1. Create a variable of type object
2. Use an execute sql task to retrieve values from tableparam and populate the object variable
3. Use a For each loop with ADO .NET enumerator and map to above object variable
4. Create three other variables of type int to hold the Year,Month,Week values from loop during each iteration and map inside for each loop
5. Add another execute sql task inside for each loop to call the sp

make the statement as

exec sp_ClculateValue ?, ?, ?

and map to variables in parameter mapping tab as
variable type direction parametername
User::Year long Input 0
User::Month long Input 1
User::Week long Input 2

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

Go to Top of Page
   

- Advertisement -