简体   繁体   中英

SQL Query Inner Join with Temp Table

I'm trying to set values in a temp table using the examples shown here: SQL output: Is it possible to create a temporary output column?

I have created my initial table, where I'm using the column names to base the population of a new column of values "operdesc".

Here is my working table query where I get the COLUMN_NAME value I need. I want to add a temp column "OPERAND":

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

However, I want to take it a step further and simplify it using a temp join. I followed this example: SQL output: Is it possible to create a temporary output column? but am hung up on my script. This is what I have but it's not working, hopefully I'm close:

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;

The 'Clients' table is a view.

Much thanks in advance!

You had it almost ok but you had T1 alias used in column alias instead of column and most importantly you had WHERE before JOIN . This works:

WITH XOperLU (xopername, xoperdesc)
 AS
 (
  SELECT xopername, CAST(xoperdesc AS VARCHAR(20))
    FROM (
          VALUES ('Street', 'SS'), 
                 ('Town', 'TW')                    
         ) AS XOperLU (xopername, xoperdesc)
 ) SELECT T1.COLUMN_NAME as COLUMN_NAME, T1.DATA_TYPE as DATA_TYPE, S1.xoperdesc AS Description FROM INFORMATION_SCHEMA.COLUMNS  AS T1 
   INNER JOIN XOperLU AS S1
      ON S1.xopername = T1.COLUMN_NAME
      WHERE    (TABLE_SCHEMA = 'dbo') AND (TABLE_NAME = 'Clients')

If you want to see all other columns that don't have specified description to be set to something, eg 'XX', you can use left join:

WITH XOperLU (xopername, xoperdesc)
 AS
 (
  SELECT xopername, CAST(xoperdesc AS VARCHAR(20))
    FROM (
          VALUES ('Street', 'SS'), 
                 ('Town', 'TW')                    
         ) AS XOperLU (xopername, xoperdesc)
 ) SELECT T1.COLUMN_NAME as COLUMN_NAME, T1.DATA_TYPE as DATA_TYPE, ISNULL(S1.xoperdesc,'XX') AS Description FROM INFORMATION_SCHEMA.COLUMNS  AS T1 
   LEFT JOIN XOperLU AS S1
      ON S1.xopername = T1.COLUMN_NAME
      WHERE    (TABLE_SCHEMA = 'dbo') AND (TABLE_NAME = 'Clients')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM