SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Must declare the scalar variable - help required


Must declare the scalar variable - help required

Author
Message
isaac.a
isaac.a
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 7
begin
DECLARE @deldate varchar(10)='01/01/1960',
@tblname varchar(50) = 'CACCOUNTSTMT',
@ssql varchar(200)
set @ssql = 'set @deldate = (select max(Convert(varchar(10) ,RowCreateDate,103)) from ' +@tblname +')'
print @ssql
--set @deldate = @ssql
exec(@ssql)
print 'deledate'
print @deldate
end

gives the following error
Must declare the scalar variable "@deldate".

can any one help on this?
David Burrows
David Burrows
SSCrazy Eights
SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)

Group: General Forum Members
Points: 9505 Visits: 9740
try
exec sp_executesql @ssql,N'@deldate varchar(10) OUTPUT',@deldate OUTPUT
instead of
exec(@ssql)


Far away is close at hand in the images of elsewhere.

Anon.


WayneS
WayneS
SSCrazy Eights
SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)

Group: General Forum Members
Points: 9723 Visits: 10569
isaac.a (1/24/2011)
begin
DECLARE @deldate varchar(10)='01/01/1960',
@tblname varchar(50) = 'CACCOUNTSTMT',
@ssql varchar(200)
set @ssql = 'set @deldate = (select max(Convert(varchar(10) ,RowCreateDate,103)) from ' +@tblname +')'
print @ssql
--set @deldate = @ssql
exec(@ssql)
print 'deledate'
print @deldate
end

gives the following error
Must declare the scalar variable "@deldate".

can any one help on this?


In the dynamic sql, you are setting a variable that doesn't exist in the context of the dynamic sql connection.

Use the solution David supplied to pass it in and get the results back out.

Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings

isaac.a
isaac.a
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 7
Thank u so much. It works.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search