Solved

EXEC statement - how to put quotes around a string variable

Posted on 2007-03-24
2
478 Views
Last Modified: 2008-02-01
Running SQL SERVER 2000 on SBS 2003 Premium

My question:

I am trying to construct an EXEC statement.

Lets say there are 2 sproc's:  spA and spB.

spA:
DECLARE @itemID    varchar(30)
DECLARE @intActive    tinyint
DECLARE @callEXEC     varchar(200)

@intActive = 1
SELECT @itemID = MAX(tblStores.itemID)
FROM  tblStores
WHERE tblStores.Active = @intActive

SET @callEXEC = 'spB  ' + ''' + @itemID + ''' + CAST(@intActive AS varchar(1))

EXEC @callEXEC

spB
DECLARE @itemID2
DECLARE @strActive

My question:  I do not seem to know how to put quotes around @itemID.  When I execute this SQL tells me:
>>  Cannot find spB VVZ 1

Please note:  VVZ is one of the itemID's.

Suggestions?
0
Comment
Question by:donpick
2 Comments
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 18787430


SET @callEXEC = 'exec spB  ' + ''' + @itemID + '',' + CAST(@intActive AS varchar(1))


EXEC @callEXEC

0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 18787828
corrections:
* you need brackets when calling SQL text with EXEC
* you need to put the single quotes doubled

SET @callEXEC = 'exec spB  ''' + @itemID + ''',' + CAST(@intActive AS varchar(1))

EXEC (@callEXEC)
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

760 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

20 Experts available now in Live!

Get 1:1 Help Now