Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a variable into a stored procedure as part of a column name

Status
Not open for further replies.

ljevans

Technical User
Apr 4, 2001
53
US
I would like to create a stored procedure for this query.
Can I do this?

My query is in a loop where month_num increments from 1 to 18 and #project_number# is passed from another query:

<set month_num = 1>

SELECT Sum(month#month_num#) AS total_month
FROM .manload.dbo.monthly_manload
WHERE dept IN (50,51,52,55)
and project_number = '#project_number#'

<set month_num = month_num + 1>

 
declare @month int, @sql varchar(128)

set @month=1

While @month<19
Begin

Select &quot;@sql=SELECT Sum(month&quot; + ltrim(str(@month)) + &quot;) AS total_month FROM .manload.dbo.monthly_manload WHERE dept IN (50,51,52,55) and project_number = '&quot; + @project_number + &quot;'&quot;

Exec(@sql)

Set @month=@month+1

End Terry

;-) The single biggest challenge to learning SQL programming is unlearning procedural programming. -Joe Celko

SQL Article links:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top