Solved

Dynamic SQL does not recognize parameter of stored proc

Posted on 2008-10-29
3
415 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

828 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