Solved

Dynamic SQL does not recognize parameter of stored proc

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

738 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