Solved

Using variable in MS Sql Query

Posted on 2009-05-05
1
207 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql help 8 55
SQL NULL vs Blank 26 36
SQL Server Count where two id types exist in column 8 27
Need help constructing a conditional update query 16 43
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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