更新语句未使用别名进行编译

时间:2013-05-16 13:19:16

标签: sql sql-server tsql sql-server-2012

这是我的更新声明,未编译:

Update Documents d
        Set 
            DocumentDate = 1,
            LastStatusChangedDateTime = (
                Select Top 1 DATEADD(ss,1,StatusChangedDateTime)
                    From [dbo].[DocumentStatusesHistory] dsh
                        Where dsh.DocumentID = d.DocumentID
            )
            Where DocumentID In
            (
                Select Data From dbo.Split(@DocumentIDs,',')
            )

我无法将d用作别名。我该如何解决这个问题?

3 个答案:

答案 0 :(得分:3)

这个怎么样?

    Update Documents 
        Set 
            DocumentDate = 1,
            LastStatusChangedDateTime = (
                Select Top 1 DATEADD(ss,1,StatusChangedDateTime)
                    From [dbo].[DocumentStatusesHistory] dsh
                        Where dsh.DocumentID = d.DocumentID
            )
        From Documents d
            Where DocumentID In
            (
                Select Data From dbo.Split(@DocumentIDs,',')
            )

答案 1 :(得分:0)

添加FROM子句:

Update d
Set 
DocumentDate = 1,
LastStatusChangedDateTime = (
                Select Top 1 DATEADD(ss,1,StatusChangedDateTime)
                    From [dbo].[DocumentStatusesHistory] dsh
                        Where dsh.DocumentID = d.DocumentID
)
FROM Documents AS d
...

答案 2 :(得分:0)

试试这个:

UPDATE  d
SET     d.DocumentDate = 1 ,
        d.LastStatusChangedDateTime = ( SELECT TOP 1
                                            DATEADD(ss, 1,
                                                    StatusChangedDateTime)
                                        FROM    [dbo].[DocumentStatusesHistory] dsh
                                        WHERE   dsh.DocumentID = d.DocumentID
                                  )

FROM Documents d    在哪里d.DocumentID IN(选择数据                           来自dbo.Split(@DocumentIDs,','))