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
- FROM VS_' + @dmsCompanyId + '_VehicleLogFile
- WHERE StockbookNumber = ' + CAST(@stocknum AS varchar(20)) + ' AND NewAccountsStatus = ''''X''''
- ORDER BY TransactionDate'')'
EXECUTE sp_executesql @sql, N'@deldate datetime OUTPUT', @deldate OUTPUT
PRINT @sql
PRINT ISDATE(@deldate)
IF @deldate IS NULL
- PRINT isNULL(cast(@deldate AS varchar(20)),'@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