Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql Update语句适用于临时表,但不适用于表值变量_Sql_Sql Server_Tsql_Correlated Subquery_Rdl - Fatal编程技术网

Sql Update语句适用于临时表,但不适用于表值变量

Sql Update语句适用于临时表,但不适用于表值变量,sql,sql-server,tsql,correlated-subquery,rdl,Sql,Sql Server,Tsql,Correlated Subquery,Rdl,我定义了这个表: CREATE TABLE #stagingtable ( id int identity(1,1), typeflag int default 0, resourcetype varchar(25), resource varchar(40), est int, planned int, actual int ) And then I am looking for places where the resourcetyp

我定义了这个表:

CREATE TABLE #stagingtable
(
    id int identity(1,1),
    typeflag int default 0,
    resourcetype varchar(25),
    resource varchar(40),
    est int,
    planned int,
    actual int
)

And then I am looking for places where the resourcetype is not the same as the resourcetype in the previous row, so I wrote the following UPDATE:

UPDATE #stagingtable
SET typeflag = 1 
WHERE id = (
    SELECT min(id)
    FROM #stagingtable
)
OR resourcetype <> (
    SELECT resourcetype
    FROM #stagingtable rt2
    WHERE rt2.id = (
        SELECT MAX(id) 
        FROM #stagingtable rt3
        WHERE rt3.id < #stagingtable.id
    )
)
这很有效。但是,我所在的环境不允许我使用临时表RDL!。因此,我将表更改为表值变量:

DECLARE @stagingtable TABLE
(
    id int identity(1,1),
    typeflag int default 0,
    resourcetype varchar(25),
    resource varchar(40),
    est int,
    planned int,
    actual int
)

But the following code doesn't work. 

UPDATE @stagingtable
SET typeflag = 1 
WHERE id = (
    SELECT min(id)
    FROM @stagingtable
)
OR resourcetype <> (
    SELECT resourcetype
    FROM @stagingtable rt2
    WHERE rt2.id = (
        SELECT MAX(id) 
        FROM @stagingtable rt3
        WHERE rt3.id < @stagingtable.id
    )
)
我得到的信息是:

Msg 137,级别16,状态1,第431行必须声明标量变量 @分期付款


有没有办法更改update语句以使其正常工作?

您的查询几乎没有问题。在最后一行中,您只需使用您提供的别名引用表变量:

其中rt3.id<@stagingtable.id


你的问题很好。在最后一行中,您只需使用您提供的别名引用表变量:

其中rt3.id<@stagingtable.id


我设法想出了正确的语法,即在变量名周围添加方括号:

UPDATE @stagingtable
SET typeflag = 1 
WHERE id = (
    SELECT min(id)
    FROM @stagingtable
)
OR resourcetype <> (
    SELECT resourcetype
    FROM @stagingtable rt2
    WHERE rt2.id = (
        SELECT MAX(id) 
        FROM @stagingtable rt3
        WHERE rt3.id < [@stagingtable].id
    )
)

我设法想出了正确的语法,即在变量名周围添加方括号:

UPDATE @stagingtable
SET typeflag = 1 
WHERE id = (
    SELECT min(id)
    FROM @stagingtable
)
OR resourcetype <> (
    SELECT resourcetype
    FROM @stagingtable rt2
    WHERE rt2.id = (
        SELECT MAX(id) 
        FROM @stagingtable rt3
        WHERE rt3.id < [@stagingtable].id
    )
)

@变量是批处理的局部变量。我假设您在某个地方有一个GO命令,或者正在其他可以访问的地方运行这个命令。您可以尝试@variable,但如果您试图随时保留此变量,则不应该这样做。为什么不使用临时变量呢?@变量是批处理的局部变量。我假设您在某个地方有一个GO命令,或者正在其他可以访问的地方运行这个命令。您可以尝试@variable,但如果您试图随时保留此变量,则不应该这样做。为什么不直接使用temp呢?很好地抓住了别名我认为这不起作用,因为rt2.id与stagingtable不同。id@GeoffSnowman你需要在那里做什么?你不能像这样作为一个简单变量访问主表,它仍然是一个表,你需要一个连接来从中获取行…见下面我的答案。我只是通过在别名上运行查询和rt3.idnice catch来检查这一点。我认为这不起作用,因为rt2.id与stagingtable不同。id@GeoffSnowman你需要在那里做什么?主表不能作为一个简单变量访问,它仍然是一个表,您需要一个连接才能从中获取行…请参阅下面的答案。我只是通过运行查询和rt3.idUse更新来检查这一点。。。从中可以为最外层的表添加别名。和现在一样,很难读懂这篇文章并说出到底发生了什么,以后你会把别人搞糊涂。使用更新。。。从中可以为最外层的表添加别名。和现在一样,很难读懂这篇文章并说出到底发生了什么,以后你会把别人搞糊涂。