Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Using variable in MS Sql Query

Posted on 2009-05-05
1
209 Views
Last Modified: 2012-05-06
I've build a stored proc where I loop and set up a part of the query but it shows up empty.

This will work
//////////////////////////////////
SELECT TOP 20 ItemCode, Descr FROM Items WHERE ItemCode like 'cf%'  AND ( brandid = 1 OR brandid = 2 OR brandid = 3 OR brandid = 4 OR brandid = 0)
//////////////////////////////////

But this won't
///////////////////////////////////
DECLARE @setBrand varchar(200)
SET @setBrand = ' AND ( brandid = 1 OR brandid = 2 OR brandid = 3 OR brandid = 4 OR brandid = 0)'

SELECT TOP 20 ItemCode, Descr FROM Items WHERE ItemCode like 'cf%' + @setBrand
///////////////////////////////////

I know this is probably not of a big deal, but I can't figured it out.

Thanks
0
Comment
Question by:maxleb
1 Comment
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 24308148
EXEC  (    'SELECT TOP 20 ItemCode, Descr FROM Items WHERE ItemCode like ''cf%''  ' + @setBrand )
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Character matching different date formats for dates between 6 59
Find results from sql within a time span 11 46
TSQL Challenge... 7 36
What is this datetime? 1 19
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question