﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / SQL Server 2008 - General  / Must declare the scalar variable - help required / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Fri, 24 May 2013 14:36:04 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Must declare the scalar variable - help required</title><link>http://www.sqlservercentral.com/Forums/Topic1052323-391-1.aspx</link><description>Thank u so much. It works.</description><pubDate>Mon, 24 Jan 2011 21:26:04 GMT</pubDate><dc:creator>isaac.a</dc:creator></item><item><title>RE: Must declare the scalar variable - help required</title><link>http://www.sqlservercentral.com/Forums/Topic1052323-391-1.aspx</link><description>[quote][b]isaac.a (1/24/2011)[/b][hr]beginDECLARE @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 @deldateendgives the following errorMust declare the scalar variable "@deldate".can any one help on this?[/quote]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.</description><pubDate>Mon, 24 Jan 2011 09:07:42 GMT</pubDate><dc:creator>WayneS</dc:creator></item><item><title>RE: Must declare the scalar variable - help required</title><link>http://www.sqlservercentral.com/Forums/Topic1052323-391-1.aspx</link><description>try exec sp_executesql @ssql,N'@deldate varchar(10) OUTPUT',@deldate OUTPUT instead of exec(@ssql) </description><pubDate>Mon, 24 Jan 2011 06:44:25 GMT</pubDate><dc:creator>David Burrows</dc:creator></item><item><title>Must declare the scalar variable - help required</title><link>http://www.sqlservercentral.com/Forums/Topic1052323-391-1.aspx</link><description>beginDECLARE @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 @deldateendgives the following errorMust declare the scalar variable "@deldate".can any one help on this?</description><pubDate>Mon, 24 Jan 2011 06:39:21 GMT</pubDate><dc:creator>isaac.a</dc:creator></item></channel></rss>