Solved

Dynamic SQL does not recognize parameter of stored proc

Posted on 2008-10-29
3
411 Views
Last Modified: 2008-10-29
I am using t-sql - sql server 2005.
I have a stored proc sp_Activity which has a parameter @Include with a default value of 0.
I have some dynamic sql that executes another proc from the main proc, but it does not recognize the parameter @Include

I get an error " must define the variable @Include."

I need to pass the parameter from the main proc to the inner proc that is executed.


CREATE  PROCEDURE dbo.sp_Activity  
    @Area            varchar(2100)   = NULL   
,   @StartDate              datetime        = NULL    
,   @EndDate                datetime        = NULL   
,   @Include bit = 0
 
DECLARE @SQLText          nvarchar(4000)
 
SELECT @SQLText = '
DECLARE @SuccessFlag tinyint
SET @SuccessFlag = 0
 
SELECT TOP 0 * INTO #Trade2 FROM #Trade
 
INSERT INTO #TmpArea2
EXEC dbo.sp_UpdateActivityData    
    @ServerName = @@SERVERNAME,
    @UpdateFlag = 1,
    @IncludeFieldTaxes  = @Include,
    @SuccessFlag = @SuccessFlag OUTPUT
'

Open in new window

0
Comment
Question by:countrymeister
3 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 400 total points
ID: 22830862
sure it does not recognize it
you won't be able to do it the way you do it since EXEC does not support incoming parameters
change ti to this:

CREATE  PROCEDURE dbo.sp_Activity  
    @Area            varchar(2100)   = NULL  
,   @StartDate              datetime        = NULL    
,   @EndDate                datetime        = NULL  
,   @Include bit = 0
 
DECLARE @SQLText          nvarchar(4000)
 
SELECT @SQLText = '
DECLARE @SuccessFlag tinyint
SET @SuccessFlag = 0
 
SELECT TOP 0 * INTO #Trade2 FROM #Trade
 
INSERT INTO #TmpArea2
EXEC dbo.sp_UpdateActivityData    
    @ServerName = @@SERVERNAME,
    @UpdateFlag = 1,
    @IncludeFieldTaxes  = '+cast(@Include as char(1))+',
    @SuccessFlag = @SuccessFlag OUTPUT
'
0
 
LVL 60

Assisted Solution

by:chapmandew
chapmandew earned 50 total points
ID: 22830881
That won't work...you'll have to use sp_executesql and pass in the params

http://msdn.microsoft.com/en-us/library/ms188001.aspx
0
 
LVL 39

Assisted Solution

by:BrandonGalderisi
BrandonGalderisi earned 50 total points
ID: 22830895
momi is correct in that you can't pass input parameters into EXEC dynamic SQL.  

But you don't need to wrap this in a call like this at all.

Just call the procedure.
CREATE  PROCEDURE dbo.sp_Activity  
    @Area            varchar(2100)   = NULL   
,   @StartDate              datetime        = NULL    
,   @EndDate                datetime        = NULL   
,   @Include bit = 0
 
DECLARE @SQLText          nvarchar(4000)
 
 
DECLARE @SuccessFlag tinyint
SET @SuccessFlag = 0
 
SELECT TOP 0 * INTO #Trade2 FROM #Trade
 
INSERT INTO #TmpArea2
EXEC dbo.sp_UpdateActivityData    
    @ServerName = @@SERVERNAME,
    @UpdateFlag = 1,
    @IncludeFieldTaxes  = @Include
    @SuccessFlag = @SuccessFlag OUTPUT

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

822 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