473,473 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 4321

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Aaron Ackerman | last post by:
Is there any way to concatenate a display member in a bound combo (ex: FirstName & LastName) or do I merely have to have a field called FullName in my dataset from the get go and bring that over???
1
by: Kevin | last post by:
Hello, How do I concatenate two fields in the same table... For example, I have two tables, first one is called familynametable that has a familyID and familyname field. This table is connected...
0
by: anaik100 | last post by:
i just want to write a SQL query to concantenate two columns to form a part of where clause ex query SELECT * FROM tab1 a,tab2 b WHERE a.ID='b.col1+b.col2' i am trying to put it in DB2..but...
6
by: Sheldon | last post by:
Hi, I am trying to build a large array using concatenate function in python. So as I loop over the number of arrays, of which there are 12 (4 down and 3 across), I create 3 long arrays by...
14
by: metamorphiq | last post by:
Hello, I'm a Java programmer, so I'm probably asking a very simple question here, but I have trouble solving it :) I'd like to know how you concatenate multiple (4, in my case) char* in C++,...
2
by: exapplerep | last post by:
I've seen how to use VBA code to concatenate two fields into a third by using an expression in the "After Update" property in fields 1 & 2. field3 = field1 + field2 The above code would go...
12
by: parth | last post by:
Hi I want to achieve the following transformation of data using a stored procedure. Source col1 col2(varchar) -------------------------
12
by: derekedw | last post by:
Hi guys, This is my first post out here. I read the guidelines, did not find anything relevant via searching, so I would like some help here. I am trying to write an update statement on a...
10
by: Aaron Hoffman | last post by:
Hello, I'm hoping someone might be able to offer some guidance to my problem. I have one query in MS Access which consists of 2 tables joined by a SEQUENCE_ID. By joining the two tables I am...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.