0

Wanting to know how to get the output of the following dynamic query into a variable that can be used in while loop in SQL. This gives me the result I am looking for in the results window. Just need to get it into a usable variable once in a stored procedure.

declare @DynQry varchar(max)
declare @rotpos int = 2
declare @rotID int = 3

set @DynQry = '

select ShiftID'+CAST(@rotpos as nvarchar(10))+' from dbo.Rotations where RotationID = '+CAST(@rotID as nvarchar(10))+';

'
EXEC (@DynQry);
 
Cody
  • 17
  • 3
  • Does this answer your question? [Getting result of dynamic SQL into a variable for sql-server](https://stackoverflow.com/questions/3840730/getting-result-of-dynamic-sql-into-a-variable-for-sql-server) – RoMEoMusTDiE Nov 05 '21 at 02:09
  • https://stackoverflow.com/questions/3840730/getting-result-of-dynamic-sql-into-a-variable-for-sql-server – RoMEoMusTDiE Nov 05 '21 at 02:09
  • No it doesnt, I am not using the count function – Cody Nov 05 '21 at 02:12
  • I have just given you a relevant article that will help you. obviously there is work to do from your end! – RoMEoMusTDiE Nov 05 '21 at 03:41

0 Answers0