Sqlserver
 sql >> Datenbank >  >> RDS >> Sqlserver

Variable in der FROM-Anweisung verwenden

Sie müssen dynamisches SQL verwenden:

declare @numrows int = (select count(*) from sys.servers)
declare @i int = 1
declare @Sql(1000)
declare @servername varchar(max)

while @i <= @numrows
BEGIN
select @servername = servernaam 
from #servers where rij = @i

set @Sql = 'select * from '[email protected]+'.DATABASE.DBO.TABLE'
exec(@Sql)

set @i = @i+1

END

hier Weitere Informationen zu exec.