我想在一個臨時表中設置值的利用這裏顯示的例子:SQL output: Is it possible to create a temporary output column?SQL查詢內部聯接與臨時表
我創建了我最初的表,其中我使用的列名的基礎一列新的「operdesc」值。
這裏是我的工作表查詢,我得到我需要的COLUMN_NAME值。我想補充一個臨時列「操作數」:
SELECT COLUMN_NAME, DATA_TYPE, 'OPERAND' AS TempField,
CASE WHEN COLUMN_NAME = 'Street' THEN '=' WHEN COLUMN_NAME = 'Town' THEN 'CW' END AS OPERAND FROM INFORMATION_SCHEMA.COLUMNS WHERE (TABLE_SCHEMA = 'dbo') AND (TABLE_NAME = 'Customers') ORDER BY COLUMN_NAME
不過,我希望把它更進一步,使用一個臨時加入簡化它。我遵循這個例子:SQL output: Is it possible to create a temporary output column?但我掛在我的腳本。這是我有,但它不工作,希望我很接近:
WITH XOperLU (xopername, xoperdesc)
AS
(
SELECT xopername, CAST(xoperdesc AS VARCHAR(20))
FROM (
VALUES ('Street', 'SS'),
('Town', 'TW')
) AS XOperLU (xopername, xoperdesc)
) SELECT COLUMN_NAME as T1.COLUMN_NAME, DATA_TYPE as T1.DATA_TYPE, S1.xoperdesc AS Description FROM INFORMATION_SCHEMA.COLUMNS AS T1 WHERE (TABLE_SCHEMA = 'dbo') AND (TABLE_NAME = 'Clients')
INNER JOIN XOperLU AS S1
ON S1.xopername = T1.COLUMN_NAME;
'客戶'表是一個視圖。
非常感謝提前!
SQL Server 2008 R2 – ripsin