Home All Groups Group Topic Archive Search About

passing in a value to use as a column 'as name' in a stored proc

Author
10 Jun 2006 11:46 PM
Aussie Rules
Hi,

I want to hand into a store procude the column name to use in the returned
result set...

create proc sample
@colName as nvarcher(20)

as

select col1 as @colname, col2 from table1..

But this produces an error... saying incorrect syntax near @colname

is there a way to do want i am trying to do here?

Thanks

Author
11 Jun 2006 1:25 AM
Martin C K Poon
The curse and blessings of dynamic SQL
http://www.sommarskog.se/dynamic_sql.html

--
Martin C K Poon
Senior Analyst Programmer
====================================
Show quote
"Aussie Rules" <AussieRules@nospam.nospam> ¦b¶l¥ó
news:uvQ1pgOjGHA.3572@TK2MSFTNGP04.phx.gbl ¤¤¼¶¼g...
> Hi,
>
> I want to hand into a store procude the column name to use in the returned
> result set...
>
> create proc sample
> @colName as nvarcher(20)
>
> as
>
> select col1 as @colname, col2 from table1..
>
> But this produces an error... saying incorrect syntax near @colname
>
> is there a way to do want i am trying to do here?
>
> Thanks
>
>
>
>
>
Author
12 Jun 2006 3:05 AM
Steven Cheng[MSFT]
Thanks for Martin's informative inputs.

Hi Aussie,

I agree with Martin that you would need to consider using the dynamic SQL
execution. And in SQL Server the "exec" or "execute" keyword to execute
dynamic generated T-SQL statements:

#EXECUTE
http://msdn.microsoft.com/library/en-us/tsqlref/ts_ea-ez_05ro.asp?frame=true

BTW, dynamic sql will have additional performance overhead comparing to
static T-SQL execution. Also, when we use string concatenate to generate
dynamic dynamic T-SQL statement, we would also take care of SQL injection
issue:

#SQL Injection 
http://msdn2.microsoft.com/en-us/library/ms161953.aspx

Hope this also helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Author
14 Jun 2006 2:50 PM
Steven Cheng[MSFT]
Hi Aussie,

Have you got any progress or new ideas on this issue or does our replies
help you some? If there is still anything we can help, please feel free to
post here.

Regards,

Steven Cheng
Microsoft MSDN Online Support Lead


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

AddThis Social Bookmark Button