August 28, 2007 at 5:19 am
Hi, I'm having trouble the assiging a value to an output variable in a dynamic SQL query:
DECLARE @deldate datetime
SET @sql = 'SELECT TransactionDate FROM OPENQUERY(' + @dmsDb + ', ''SELECT TOP 1 TransactionDate
EXECUTE sp_executesql @sql, N'@deldate datetime OUTPUT', @deldate OUTPUT
PRINT @sql
PRINT ISDATE(@deldate)
IF @deldate IS NULL
If I execute the printed @sql statement, I get a result set containing a date, but my attempts to print the variable @deldate suggest that it is null. What's going on here? Btw @dmsDb, dmsCompanyId and @stocknum are declared earlier in the code (not shown here) and printing of the @sql statement shows that these are fine.
Cheers, Mundo
August 28, 2007 at 6:46 am
Oops! Newbie error! Changing the SET @sql bit to the following fixed this:
SET @sql = 'SELECT @deldate = TransactionDate FROM OPENQUERY(' + @dmsDb + ', ''SELECT TOP 1 TransactionDate
+ @dmsCompanyId + '_VehicleLogFile
+ CAST(@stocknum AS varchar(20)) + ' AND NewAccountsStatus = ''''X''''
Note the extra '@deldate = ' bit at the beginning. Going away from my desk for lunch seemed to give me the fresh view of this to see the error.
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply