470,614 Members | 1,387 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,614 developers. It's quick & easy.

concatenate where clause

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

Jul 23 '05 #1
6 4216

"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
Jul 23 '05 #2
SQL is a compiled language. not some version of BASIC.

SELECT * FROM Categories WHERE parent_id = 2;

Jul 23 '05 #3
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


Jul 23 '05 #4
In that case, you might be able to use views and/or functions to
simplify your queries - you can create views for your most common joins
and queries, then select from the views in your proc code. That would
help you simplify your code while avoiding the dynamic SQL solution.

Simon

Jul 23 '05 #5
Simon, first of all thanks for you prompt answers. I liked the function
idea is nice.

Rod

Simon Hayes wrote:
In that case, you might be able to use views and/or functions to
simplify your queries - you can create views for your most common joins and queries, then select from the views in your proc code. That would
help you simplify your code while avoiding the dynamic SQL solution.

Simon


Jul 23 '05 #6
>> I am building a stored procedure which contains a lot of repetitive
sql statements specially in the where clause.<<

Statements change things in the schema (UPDATE, INSERT, DELETE) so you
cannot put them int he WHERE clause; did you mean search conditions?

Build VIEWs and not functions or dynamic SQL for the repetitive search
conditions. SQL is a declarative langauge, not a procedural language.
It is also a compiled language. so dynamic SQL is a kludge.

Also, if this is one procedure as you said, post the code. Assuming
that it has the proper cohesion and coupling, there is a good chance
that we can improve it and remove the repetition. Since we got the
CASE expression, a lot of things that programmers used to write with
procedural or dynamic code can be done with compiled code.

Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Aaron Ackerman | last post: by
6 posts views Thread by Sheldon | last post: by
14 posts views Thread by metamorphiq | last post: by
10 posts views Thread by Aaron Hoffman | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.