Solved

Dynamic SQL does not recognize parameter of stored proc

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

911 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

16 Experts available now in Live!

Get 1:1 Help Now