Hello.. I've used a prior post's instructions to attempt passing a report's parameter - @From_Date and am getting a 'Must declare the scalar variable @From_Date' error message -- I don't know where to go from here! Any ideas.. Thanks in advance! DECLARE @SQLAI nvarchar(max) SET @SQLAI = 'DECLARE @FROM datetime' SET @SQLAI = @SQLAI + ' SET @FROM = @From_Date ' SET @SQLAI = @SQLAI + ' SELECT @FROM' EXEC (@SQLAI)
When you EXEC() something, the variables that you are working with aren't visible to the query that you are EXEC()ing. They are not in the same scope. Instead of building the string with `+ ' SET @FROM = @From_Date '` try something like `+ ' SET @FROM = ' + CONVERT(varchar, @From_Date, 121) + ' '` This will build your dynamic query with the value that is in the variable, rather than a reference to the variable. I am assuming here that @From_Date is a date/time.