I have a stored procedure called R_Proc
with a parameter called id
. Inside I am creating temp table ##Insert_Data
for inserting data:
set @Sql_Create_Table = 'CREATE TABLE ##Insert_Data...'
exec sp_executesql @Sql_Create_Table
Then I have created a cursor for selecting data based on the id
:
declare cur cursor for
select name from test where id = @id
open cur
fetch next from cur into @name
while @@FETCH_STATUS = 0
begin
set @Sql_Search_Table = 'select id from ##Insert_Data where ....'
exec sp_executesql @Sql_Search_Table
--I want to get **id from above exec** and pass to another statement below
set @Sql_Table = 'insert into ##Insert_Data
select name from tbl where id=<id from above>'
end
Last attempt was :
set @Sql_Search_Table ='select '+@id+'=id from ##Insert_Data where ....'
exec sp_executesql @Sql_Search_Table
set @temp = @id
--I want to get **id from above exec** and pass to another statement below
set @Sql_Table = 'insert into ##Insert_Data
select name from tbl where id='+@temp+'
When I print @temp
variable the value is null.
I am stuck. How can I do something like this?