'How to declare variables in T-SQL from select statements
declare @Week nvarchar(16) = quotename(concat('Week',@CID,@SID));
declare @Week2 nvarchar(256) = concat('SELECT TOP 1 [Year],[Week] FROM',@Week,'GROUP BY [Year],[Week] ORDER BY [Year] DESC,[Week] DESC');
exec sp_executesql @Week2
how can I declare @Year and @Week respectively from the result of exec sp_executesql @Week2? (declare as in assign the result values into @year and @week)
Thanks
declare @latestyear NVARCHAR(50), @latestweek NVARCHAR(50) declare @Week2 nvarchar(256) = 'SELECT TOP 1 @latestyear=[Year],@latestweekweek=[Week] FROM [Week45] GROUP BY [Year],[Week] ORDER BY [Year] DESC,[Week] DESC'; exec sp_executesql @Week2 PRINT @latestyear PRINT @latestweek
The above doesn't seem to work for me
Also trying to do this in 1 select statement if possible
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
