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

Ergebnis von dynamischem SQL in eine Variable für sql-server bekommen

DECLARE @sqlCommand nvarchar(1000)
DECLARE @city varchar(75)
declare @counts int
SET @city = 'New York'
SET @sqlCommand = 'SELECT @cnt=COUNT(*) FROM customers WHERE City = @city'
EXECUTE sp_executesql @sqlCommand, N'@city nvarchar(75),@cnt int OUTPUT', @city = @city, @[email protected] OUTPUT
select @counts as Counts