Solved

Using variable in MS Sql Query

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

896 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now