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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Help in Query

Author  Topic 

shammad
Starting Member

3 Posts

Posted - 2011-04-20 : 09:38:20
Hi,

is there a way to use dynamic columns name in a Stored Procedure, I mean some thing like :

ALTER PROCEDURE [SearchApplication]
@Name nvarchar(50),
@Prop nvarchar(50)
AS
BEGIN
SELECT * FROM Application as app WHERE @Prop Like '%'+@Name+'%'
END

where @Prop will change depends on the parameter.

please advice,
Thanks in advance

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-04-20 : 09:45:14
ALTER PROCEDURE [SearchApplication]
@Name nvarchar(50),
@Prop nvarchar(50)
AS
BEGIN
declare @sql varchar(1000)
select @sql = 'SELECT * FROM Application as app WHERE [' + @Prop + '] Like ''%'+@Name+'%'''
exec (@sql)
END

see
http://www.nigelrivett.net/SQLTsql/TableNameAsVariable.html


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

shammad
Starting Member

3 Posts

Posted - 2011-04-20 : 09:54:48
Great, it is working thank you :)
Go to Top of Page
   

- Advertisement -