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

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -