Thanks guys. I appreciate your help. Let me explain what I want to do
and maybe there is a different solution for what I am trying to do.
I am building a stored procedure which contains a lot of repetitive sql
statements specially in the where clause. I was trying to clean it a
little bit by storing the recurring code so that it would be easier to
read. In C# or VB.net this is easy to do like I showed you. But it
seems that I will have to live with it unless I use dynamic SQL.
Rod
Simon Hayes wrote:
"Rodusa" <rc**********@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...I am beggining to learn stored procedures and I am trying to
concatenate the where clause below but I keep getting an error:
declare @sqlwhere varchar(15)
set @sqlwhere = 'parentid=2'
select * from categories where @sqlwhere
This is the error I am getting
Server: Msg 170, Level 15, State 1, Line 3
Line 3: Incorrect syntax near '@sqlwhere'.
What am I doing wrong?
Thanks
Rod
You can't use a variable in place of arbitrary parts of a query - you
would need to use dynamic SQL instead. However, that's often a bad idea and
there are lots of reasons why you shouldn't; check out this article for
examples and more details of the issues with using dynamic SQL:
http://www.sommarskog.se/dynamic_sql.html
Simon