Solved

Dynamic SQL does not recognize parameter of stored proc

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

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

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

758 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

17 Experts available now in Live!

Get 1:1 Help Now