(https://social.msdn.microsoft.com/Forums/sqlserver/en-US/144f0812-b3a2-4197-91bc-f1515e7de4b9/not-able-to-create-hash-table-inside-stored-proc-through-execute-spexecutesql-strquery?forum=sqldatabaseengine),
#Temp:
CREATE TABLE
, , . , , - .
, PIVOT. , . , SQL Server 1024 ( ) 8060 (http://msdn.microsoft.com/en-us/library/ms143432.aspx). , #Temp ( NULLable).
, CREATE TABLE ( int ):
CREATE TABLE #Temp(c1 int NULL, c2 int NULL, c3 int NULL, ..., c1024 int NULL);
, #Temp , @Cols. .
@Cols. - - , , @Cols , , . , #Temp. - :
@TempCols = N'c1, c2, c3, c4, c5';
@TempCols , @Cols. SQL ( INSERT INTO #Temp (@TempCols) ):
SET @Sql = N'INSERT INTO #Temp (' + @TempCols + N') SELECT ' + @Cols + N' FROM
(
SELECT ResourceKey, ResourceValue
FROM LocaleStringResources where StateId ='
+ LTRIM(RTRIM(@StateID)) + ' AND FormId =' + LTRIM(RTRIM(@FormID))
+ ' AND CultureCode =''' + LTRIM(RTRIM(@CultureCode)) + '''
) x
pivot
(
max(ResourceValue)
for ResourceKey IN (' + @Cols + ')
) p ;'
SQL:
EXEC (@Sql) OR sp_executesql @Sql
, #Temp temp c1, c2, c3, ...
MSDN :
, , , .
DROP #Temp, :
IF OBJECT_ID('tempdb..#Temp') IS NOT NULL
DROP TABLE
T-SQL (CREATE TABLE, EXEC,... ..., DROP TABLE), , .