从select中更新sql server

时间:2011-06-06 19:11:27

标签: sql sql-server-2000

根据this post的答案,我有类似的内容:

update MyTable
set column1 = otherTable.SomeColumn,
    column2 = otherTable.SomeOtherColumn
from MyTable
inner join
(select *some complex query here*) as otherTable
on MyTable.key_field = otherTable.key_field;

但是,我一直收到这个错误:

  

列前缀'otherTable'可以   与表名或别名不匹配   查询中使用的名称。

我不确定是什么问题。我不能从这样的选择查询中做这样的更新吗? 任何帮助将不胜感激。

(我正在使用* blush * sql server 2000。)

编辑:

这是实际查询

update pdx_projects set pr_rpc_slr_amount_year_to_date = summary.SumSLR, pr_rpc_hours_year_to_date = summary.SumHours
    from pdx_projects pr join (
select  pr.pr_pk pr_pk, sum(tc.stc_slr_amount)  SumSLR, sum(tc.stc_worked_hours)  SumHours from pdx_time_and_cost_from_rpc tc 
    join pdx_rpc_projects sp on tc.stc_rpc_project_id = sp.sol_rpc_number
    join pdx_rpc_links sl on sl.sol_fk = sp.sol_pk
    join pdx_projects pr on pr_pk = sl.pr_fk
    where tc.stc_time_card_year = year(getdate())
    group by pr_pk
) as summary

on pr.pr_pk = summary.pr_pk

并且实际的错误消息是

  

服务器:Msg 107,Level 16,State 2,   第1行列前缀'summary'   与表名称或   查询中使用的别名。

3 个答案:

答案 0 :(得分:1)

我向您提交此更改后的查询:

update x
set x.pr_rpc_slr_amount_year_to_date = summary.sumSLR, 
    x.pr_rpc_hours_year_to_date = summary.sumHours
from pdx_projects x
join (
    select  pr.pr_pk as pr_pk, 
            sum(tc.stc_slr_amount) as SumSLR, 
            sum(tc.stc_worked_hours) as SumHours 
    from pdx_time_and_cost_from_rpc tc 
    join pdx_rpc_projects sp on tc.stc_rpc_project_id = sp.sol_rpc_number
    join pdx_rpc_links sl on sp.sol_pk = sl.sol_fk
    join pdx_projects pr on sl.pr_fk = pr.pr_pk
    where tc.stc_time_card_year = year(getdate())
    group by pr.pr_pk
) as summary
on x.pr_pk = summary.pr_pk

这里明显不同:我不会在复杂查询的内部和外部重用别名pr。我按照我喜欢的方式重新排序了连接(先前引用过表格),并在2个地方明确标注了pr_pk。我还将更新语法更改为使用update <alias>

答案 1 :(得分:0)

也许不是您正在寻找的答案,但我通常默认将一些复杂的查询插入到表变量中,而不是生成非常复杂的查询。然后,您可以通过连接到表变量对MyTable进行简单更新。它可能效率不高,但更容易维护。

答案 2 :(得分:0)

我无法在80兼容级别使用SQL 2008复制您的错误。虽然此选项并不能保证我会得到与您相同的结果,但似乎没有任何不合适的地方。

create table pdx_projects
(
    pr_rpc_slr_amount_year_to_date varchar(max)
    , pr_rpc_hours_year_to_date varchar(max)
    , pr_pk varchar(max)

)

create table pdx_time_and_cost_from_rpc
(
    stc_slr_amount decimal
    , stc_worked_hours decimal
    , stc_rpc_project_id varchar(max)
    , stc_time_card_year varchar(max)
)

create table  pdx_rpc_projects
(
    sol_rpc_number varchar(max)
    , sol_pk varchar(max)
)

create table pdx_rpc_links
(
    sol_fk varchar(max)
    , pr_fk varchar(max)
)


update pdx_projects
set
    pr_rpc_slr_amount_year_to_date = summary.SumSLR
    , pr_rpc_hours_year_to_date = summary.SumHours
from
    pdx_projects pr
join (
    select  pr.pr_pk pr_pk
        , sum(tc.stc_slr_amount)  SumSLR
        , sum(tc.stc_worked_hours)  SumHours
    from pdx_time_and_cost_from_rpc tc 
    join pdx_rpc_projects sp on tc.stc_rpc_project_id = sp.sol_rpc_number
    join pdx_rpc_links sl on sl.sol_fk = sp.sol_pk
    join pdx_projects pr on pr_pk = sl.pr_fk
    where tc.stc_time_card_year = year(getdate())
    group by pr_pk
    ) as summary
    on pr.pr_pk = summary.pr_pk