Wednesday, 21 February 2018

Using a temporary table in dynamic sql query in a stored procedure

Description : in this post how to use temporary table in dynamic sql query first add city table data in #tmpCity and use in dynamic sql insert query

Declare @Sql Nvarchar(max),@Sql1 Nvarchar(max)

CREATE TABLE #tmpCity (CityIDP BigInt, CityName Varchar(50))

SET @Sql = 'INSERT INTO #tmpCity SELECT CityIDP, CityName  FROM mCity'
EXEC(@Sql)
SELECT @@ROWCOUNT
SET @Sql1 = 'SELECT * FROM #tmpCity'
EXEC(@Sql1)
GO
DROP TABLE #tmpCity

No comments:

Post a Comment