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
 automatic feeding parameters to an sp
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

allan8964
Posting Yak Master

247 Posts

Posted - 11/16/2012 :  23:18:05  Show Profile  Reply with Quote
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

India
52309 Posts

Posted - 11/16/2012 :  23:22:35  Show Profile  Reply with Quote
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

247 Posts

Posted - 11/16/2012 :  23:31:13  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/17/2012 :  10:06:06  Show Profile  Reply with Quote
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

India
22744 Posts

Posted - 11/19/2012 :  04:23:39  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
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

India
52309 Posts

Posted - 11/20/2012 :  01:42:15  Show Profile  Reply with Quote
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
  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.05 seconds. Powered By: Snitz Forums 2000