如何更新SQL Server中的ALIAS列

时间:2013-08-12 11:22:13

标签: sql-server

我有如下查询:

select <few column names>, 
       0.0 as 'first alias column name', 
       0.0 as 'second alias column name' 
into ##tempTable 
from (one query UNION second query) 
where <some conditions>

现在稍后我必须更新这个##tempTables这两个ALIAS列。

任何想法,怎么做?

2 个答案:

答案 0 :(得分:1)

我希望你的名字中没有空格的别名!但基本上,您将使用UPDATE语句来更新该临时表 - 就像任何其他表一样:

UPDATE #tempTable
SET AliasColumn1 = somevalue,
    AliasColumn2 = someOthervalue
WHERE (some condition)

更新:问题是这样的:使用SELECT ... INTO ##tempTable并提供0.0作为值方法,您的临时表会获得两列NUMERIC类型的列精度= 1且刻度= 1 - 1位数,其中小数点后1位。基本上这是一个不可用的数值,因为你不能提供适合的任何值.....

基本上,在使用之前,你应该创建你的临时表:

CREATE TABLE ##tempTable 
       (list of columns, 
        AliasColumn1 DECIMAL(12,2), 
        AliasColumn2 DECIMAL(12,2)
       )

现在您可以在该表中插入值

INSERT INTO ##tempTable(columns......)
   SELECT  ........

然后您也可以轻松更新该表格,因为您现在拥有可用数字列

答案 1 :(得分:0)

我不得不在这里和那里进行查询。

案例1: -

Select <few column names>, 
           0.0 as 'firstAliasColumnName', 
           0.0 as 'secondAliasColumnName' 
    into ##tempTable from (one query UNION second query) where <some conditions>

执行此操作后,如果我们运行

select 'firstAliasColumnName' from ##tempTable.

我正在......

--------------------

<ListOfValues>

为了更新,我不得不写

update ##tempTable set firstAliasColumnName=.4 where <some condition>

它会正确更新我的表格。

案例2: -

   Select <few column names>, 
                   1.00 as 'firstAliasColumnName', 
                   1.00 as 'secondAliasColumnName' 
            into ##tempTable from (one query UNION second query) 
             where <some conditions>

以下更新将有效。

update ##tempTable set firstAliasColumnName=1.42 where <some condition>

等等......

但是,ALIAS列名称之间不应该存在任何空格

由于