Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Come ottenere il conteggio delle righe da EXEC() in un TSQL SPROC?

usa sp_executesql e un output parameter

esempio

DECLARE @sqlBody VARCHAR(500),@TableCount INT, @SQL NVARCHAR(1000)

SELECT @sqlBody = 'from sysobjects'
SELECT @SQL = N'SELECT @TableCount = COUNT(*) ' + @sqlBody

EXEC sp_executesql @SQL, N'@TableCount INT OUTPUT', @TableCount OUTPUT

SELECT @TableCount
GO