使用内联更新查询

时间:2015-06-16 20:00:12

标签: sql-server-2008 sql-update

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

使用表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

1 个答案:

答案 0 :(得分: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]将是用于执行连接操作的列/属性