Solved

Using variable in MS Sql Query

Posted on 2009-05-05
1
216 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
[X]
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
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

631 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