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.
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 asexec sp_ClculateValue year, month, weekhere 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, week1exec sp_ClculateValue 2010 Feb, week2...exec sp_ClculateValue 2010 Dec week1...exec sp_ClculateValue 2011, Jan, week1 ...exec sp_ClculateValue 2011, Feb, week1exec sp_ClculateValue 2011, Dec, week1till 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 thisIs this requirement to do it native sql or one to call from ssis package?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
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. |
|
|
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 |
|
|
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 tableorder by id select @sqlIf the select gives all you want , add exec(@sql) at the endMadhivananFailing to plan is Planning to fail |
|
|
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 following1. Create a variable of type object2. Use an execute sql task to retrieve values from tableparam and populate the object variable3. Use a For each loop with ADO .NET enumerator and map to above object variable4. Create three other variables of type int to hold the Year,Month,Week values from loop during each iteration and map inside for each loop5. Add another execute sql task inside for each loop to call the spmake the statement asexec sp_ClculateValue ?, ?, ?and map to variables in parameter mapping tab asvariable type direction parameternameUser::Year long Input 0User::Month long Input 1User::Week long Input 2------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|
|
|