sql - Set Variable with select query with dynamic schema name -
i trying set variable query result. problem below code runs in while loop & schemaname
different.
while (@i <= (select max(idx) @schema_table)) begin set @userid = (select aspnetusers.id schemaname.aspnetusers left join schemanameaspnetuserroles on aspnetuserroles.userid = aspnetusers.id left join schemaname.aspnetroles on aspnetroles.id = aspnetuserroles.roleid aspnetroles.name = 'superadmin') end
how set schemaname variable
can dynamic in while loop.
something help:
declare @sql nvarchar(4000), @schemaname nvarchar(200), @i int = 1, @vparams nvarchar(100) set @vparams = '@uid int output' while (@i <= (select max(idx) @schema_table)) begin select @schemaname = schemaname @schema_table idx = @i select @sql = ' select @userid = aspnetusers.id schemaname.aspnetusers left join schemanameaspnetuserroles on aspnetuserroles.userid = aspnetusers.id left join [' +@schemaname + '].aspnetroles on aspnetroles.id = aspnetuserroles.roleid aspnetroles.name = ''superadmin'';' exec sp_executesql @sql, @vparams, @userid=@uid output -- here hot @userid value need , can set @i = @i + 1 end
Comments
Post a Comment