【问题标题】:Update Query With Inline使用内联更新查询
【发布时间】:2015-06-16 20:00:11
【问题描述】:

是否可以进行这样的更新查询?这可能不是,只是我想可能解决我糟糕的数据结构困境的方法。我想要完成的是:

使用表 vet 中存在 entrytype 的计数来更新表 prodinformation

Set @location varchar(100), @entrydate datetime
Set @location = 'server01.database01.dbo.manhunt
Set @entrydate = GetDate()  

Update prodinformation
Set totalenteredtoday = abc.te
FROM prodinformation d
JOIN (SELECT Count(ID) 
      from @location 
      WHERE entrytype IN (
                          Select validentrytype 
                          from vet
                          where ltrim(rtrim(entrydate)) = @entrydate) As te

【问题讨论】:

  • 您缺少连接条件,不清楚为什么要连接到count 返回的单行结果?
  • @MartinSmith - 我想要完成的是使用表 vet 中存在 entrytype 的计数来更新表 prodinformation

标签: sql-server-2008 sql-update


【解决方案1】:
Update  d
Set     totalenteredtoday = te.IdCount
FROM    prodinformation As d
        JOIN 
        (
            Select   [someJoinAttribute]
                    ,Count(ID) As IdCount
            From    @location 
            Where   entrytype IN (  Select  validentrytype 
                                    From    vet With (Nolock)
                                    Where   ltrim(rtrim(entrydate)) = @entrydate
                                 )
            Group By [someJoinAttribute]
        ) As te On d.[someAttribute] = te.[someJoinAttribute]

这里的 [someJoinAttribute] 是用于执行连接操作的列/属性

【讨论】:

    猜你喜欢
    • 2015-08-01
    • 2015-06-15
    • 1970-01-01
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 2015-11-03
    • 2016-03-21
    • 1970-01-01
    相关资源
    最近更新 更多