SQL Server: How to set column alias from another table during select?

后端 未结 1 1317
我在风中等你
我在风中等你 2021-01-22 03:35

I have 2 tables

Table 1:

DB1, DB2, DB3, DB4, DB5, Some other identifiers

Table 2:

EnumText, EnumValue, Some other ident         


        
1条回答
  •  长情又很酷
    2021-01-22 04:29

    You can't have dynamic aliases without dynamic SQL. If you did change the resultset column names, how do you access them via name? (I wouldn't use index because it will break)

    So, you can add a fixed column to describe it. And read that

    Select 
        DB1, foo.EnumText AS DB1EnumText, 
        DB2, 
        DB3 
    from 
        Table1
        CROSS JOIN
        (Select EnumText from Table2 where EnumValue='DB1') foo
    

    0 讨论(0)
提交回复
热议问题