Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.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
使用WHERE选择SQL更新_Sql_Sql Server_Select_Where - Fatal编程技术网

使用WHERE选择SQL更新

使用WHERE选择SQL更新,sql,sql-server,select,where,Sql,Sql Server,Select,Where,我想更新表中的一列,但在每一行中都必须有另一个值,该值依赖于另一行的WHERE 这是桌子的样子 BusinessUnitGUID | ClassName | DefaultGUID 结果应该是这样的 BusinessUnitGUID | ClassName | DefaultGUID 但这个显示的查询不起作用,因为它返回许多行,因此不精确 update cSC_BusinessUnit set defaultguid = ( select defaultguid from

我想
更新表中的一列,但在每一行中都必须有另一个值,该值依赖于另一行的
WHERE

这是桌子的样子

BusinessUnitGUID | ClassName | DefaultGUID 结果应该是这样的

BusinessUnitGUID | ClassName | DefaultGUID 但这个显示的查询不起作用,因为它返回许多行,因此不精确

update cSC_BusinessUnit
set defaultguid =
    (
    select defaultguid
    from cSC_BusinessUnit 
    where BusinessUnitGUID = 5
    )
where BusinessUnitGUID = 7

这能解决你的问题吗

update cSC_BusinessUnit t1
set defaultguid =
    (
    select defaultguid
    from cSC_BusinessUnit t2
    where t2.BusinessUnitGUID = 5
    and t1.classname = t2.classname
    )
where BusinessUnitGUID = 7

您还需要检查类名:

update b1
set b1.defaultguid =
    (
    select b2.defaultguid
    from cSC_BusinessUnit b2
    where b2.BusinessUnitGUID = 5
        AND b2.ClassName = b1.ClassName
    )
from cSC_BusinessUnit b1
where b1.BusinessUnitGUID = 7

您还可以像这样动态设置所有空列,只要只有一个ClassName NOT NULL出现


你可以试试看

update cSC_BusinessUnit a
set a.defaultguid =
    (
    select defaultguid
    from cSC_BusinessUnit b
    where b.BusinessUnitGUID = 5 and b.ClassName = a.ClassName
    )
where a.BusinessUnitGUID = 7

也可以使用下面的简短版本

UPDATE  b1
SET     b1.defaultguid = b2.defaultguid
FROM    cSC_BusinessUnit b1
        JOIN
        cSC_BusinessUnit b2
            ON b1.ClassName = b2.ClassName
                And b2.BusinessUnitGUID = 5
WHERE   b1.BusinessUnitGUID = 7

他使用的是SQL Server not ORACLE。如果出现更多的类名NOTNULL,则可能会导致错误。事实上,此查询仅在只有一个类名出现具有DEFAULT GUID的上下文中有效。not NULLOP正在使用SQL Server/T-SQL,因此
update cSC\u BusinessUnit a
是错误的。关于T-SQL的正确语法,请参见公认的答案。我使用pl/SQL来实现这一点,但我认为我的想法将用于此问题。谢谢你
update A
set DefaultGUID =
    (
        select B.DefaultGUID
        from cSC_BusinessUnit B
        where B.ClassName = A.ClassName
        And B.DefaultGUID IS NOT NULL
    )
from cSC_BusinessUnit A
update cSC_BusinessUnit a
set a.defaultguid =
    (
    select defaultguid
    from cSC_BusinessUnit b
    where b.BusinessUnitGUID = 5 and b.ClassName = a.ClassName
    )
where a.BusinessUnitGUID = 7
UPDATE  b1
SET     b1.defaultguid = b2.defaultguid
FROM    cSC_BusinessUnit b1
        JOIN
        cSC_BusinessUnit b2
            ON b1.ClassName = b2.ClassName
                And b2.BusinessUnitGUID = 5
WHERE   b1.BusinessUnitGUID = 7
update b1
set b1.defaultguid =
    (
    select b2.defaultguid
    from cSC_BusinessUnit b2
    where b2.BusinessUnitGUID = 5
        AND b2.ClassName = b1.ClassName
    )
from cSC_BusinessUnit b1
where b1.BusinessUnitGUID = 7