我已经得到了这个动态创建的混乱,基本上占用了表中的所有字段并将两个记录相互比较:
DECLARE @ID1 AS VarChar(3)
DECLARE @ID2 AS VarChar(3)
Set @ID1 = '42'
Set @ID2 = '600'
-- Where clause params
DECLARE @whereClauseParam VARCHAR(MAX) = '['+@ID1+'] <> ['+@ID2+']'
--***************************************--
--******** tblSQLAdminInventory ********--
--***************************************--
--Get the Fields required for the initial pivot
DECLARE @AIFields VARCHAR(MAX)= '';
DECLARE @AIFields2 VARCHAR(MAX)= '';
SELECT @AIFields+=QUOTENAME(t.name)+', '
FROM sys.columns AS t
WHERE t.object_id = OBJECT_ID('tblSQLAdminInventory')
AND t.name <> 'TransID'
--AND t.system_type_id = '56';
SELECT @AIFields2+='Convert(VarChar(250), '+QUOTENAME(t.name)+') AS '+ QUOTENAME(t.name) +', '
FROM sys.columns AS t
WHERE t.object_id = OBJECT_ID('tblSQLAdminInventory')
AND t.name <> 'TransID'
--AND t.system_type_id = '56';
--56 (Int)
--61 (DateTime)
--104 (Bit)
--167 (VarChar)
--231 (NVarChar)
-- Get the KeyId's with alias added
DECLARE @AIkeyIDs VARCHAR(MAX),
@AIkeyIDs1 VARCHAR(MAX);
SELECT @AIkeyIDs = COALESCE(@AIkeyIDs + ',','') + QUOTENAME(t.TransID) + ' AS [KeyID_' + CAST(t.TransID AS VARCHAR(10)) + ']',
@AIkeyIDs1 = COALESCE(@AIkeyIDs1 + ',','') + QUOTENAME(t.TransID)
FROM tblSQLAdminInventory AS t
WHERE TransID IN (@ID1, @ID2);
--Generate Dynamic SQL
DECLARE @AISQL2 VARCHAR(MAX)= 'SELECT Value AS FieldName, ';
SELECT @AISQL2+=@AIkeyIDs+'
FROM
(SELECT TransID, Value, FieldName
FROM
(SELECT TransID, '+SUBSTRING(@AIFields2, 1, LEN(@AIFields2)-1)+'
FROM tblSQLAdminInventory) p
UNPIVOT
(FieldName FOR Value IN
('+SUBSTRING(@AIFields, 1, LEN(@AIFields)-1)+')
)AS unpvt) AS SourceTable
PIVOT
(
MAX(FieldName)
FOR TransID IN ('+@AIkeyIDs1+')
) AS PivotTable
WHERE '+@whereClauseParam
EXECUTE(@AISQL2);
问题是,它似乎不让我把结果放在临时表中。我尝试使用此代码,但它一直告诉我#Temp1对象不存在:
SELECT @AISQL2+=@AIkeyIDs+'
INTO #Temp1
FROM
(SELECT TransID, Value, FieldName
FROM
(SELECT TransID, '+SUBSTRING(@AIFields2, 1, LEN(@AIFields2)-1)+'
FROM tblSQLAdminInventory) p
UNPIVOT
(FieldName FOR Value IN
('+SUBSTRING(@AIFields, 1, LEN(@AIFields)-1)+')
)AS unpvt) AS SourceTable
PIVOT
(
MAX(FieldName)
FOR TransID IN ('+@AIkeyIDs1+')
) AS PivotTable
WHERE '+@whereClauseParam
我做错了什么?
答案 0 :(得分:2)
您正在使用动态SQL。 public static void Main(string[] args)
{
// Display the name of the current thread culture.
Console.WriteLine("CurrentCulture is {0}.", CultureInfo.CurrentCulture.Name);
// Change the current culture to th-TH.
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("th-TH", false);
Console.WriteLine("CurrentCulture is now {0}.", CultureInfo.CurrentCulture.Name);
// Display the name of the current UI culture.
Console.WriteLine("CurrentUICulture is {0}.", CultureInfo.CurrentUICulture.Name);
// Change the current UI culture to ja-JP.
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("ja-JP", false);
Console.WriteLine("CurrentUICulture is now {0}.", CultureInfo.CurrentUICulture.Name);
}
语句启动了一个全新的范围,并且该范围内的临时表不可用。
有几种解决方法,例如使用您清理或使用全局临时表的永久表,但它们都有自己的陷阱。