选择并更新相同的存储过程并使用select的结果更新某个值

时间:2013-09-01 08:42:28

标签: c# asp.net sql-server stored-procedures

是否有类似

的内容
select Column_n from table1;

update table2 set column_m where column_a=Column_n ;

可以用“相同存储”程序

编写

3 个答案:

答案 0 :(得分:1)

UPDATE JOIN可以直接这样:

UPDATE t2
SET t2.column_m = ...
FROM table2 AS t2
INNER JOIN table1 AS t1 ON t1.column_n = t2.column_a;

您可以将其放在存储过程中。

答案 1 :(得分:0)

SQL Server有很多选项可以使用其他表中的数据更新一个表。你能找到一些解决方案:

CREATE TABLE TableX
(
    ID INT NOT NULL,
    Col2 VARCHAR(10)
);
INSERT TableX (ID)
SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3;

CREATE TABLE TableY
(
    ID INT NOT NULL,
    Col2 VARCHAR(10)
);
INSERT TableY (ID,Col2)
SELECT 1,'A' UNION ALL SELECT 2,'B' UNION ALL SELECT 4,'D';

解决方案:

-- Solution  #1
UPDATE TableX SET Col2 = NULL;
UPDATE  x
SET     Col2 = y.Col2
FROM    TableX x INNER JOIN TableY y ON x.ID = y.ID;
SELECT  * FROM TableX;
/*
ID          Col2
----------- ----
1           A
2           B
3           NULL
*/

-- Solution  #2
UPDATE TableX SET Col2 = NULL;
UPDATE  TableX 
SET     Col2 = 
        (
            SELECT  y.Col2
            FROM    TableY y 
            WHERE   TableX.ID = y.ID
        )
SELECT  * FROM TableX;
/*
ID          Col2
----------- ----
1           A
2           B
3           NULL
*/

-- Solution  #3
UPDATE TableX SET Col2 = NULL;
WITH UpdateTableX
AS
(
    SELECT  x.Col2 AS TargetCol2,
            y.Col2 AS SourceCol2
    FROM    TableX x INNER JOIN TableY y ON x.ID = y.ID
)
UPDATE  UpdateTableX
SET     TargetCol2 = SourceCol2
SELECT  * FROM TableX;
/*
ID          Col2
----------- ----
1           A
2           B
3           NULL
*/

-- Solution  #4 (SQL2008+)
UPDATE TableX SET Col2 = NULL;
MERGE INTO TableX x
USING TableY y ON x.ID = y.ID
WHEN MATCHED THEN
    UPDATE SET Col2 = y.Col2;
SELECT  * FROM TableX;  
/*
ID          Col2
----------- ----
1           A
2           B
3           NULL
*/

现在,如果对于目标表,一行可以匹配源表中的许多行 那么你可能会遇到问题,因为有些解决方案可以成为UNSAFE(解决方案#1和#3)

示例:我在源表(TableY)中插入另一行

INSERT TableY (ID,Col2)
SELECT 1,'AA'

现在,对于源表中ID = 1的一行,有2行ID = 1({1,'A'}和{1,'AA'})。在这种情况下,即使这些UPDATE不安全,解决方案#1或#3也会成功执行:

UPDATE TableX SET Col2 = NULL;
UPDATE  x
SET     Col2 = y.Col2
FROM    TableX x INNER JOIN TableY y ON x.ID = y.ID;
SELECT  * FROM TableX;
/*
ID          Col2
----------- ----------
1           A <-- In my test, SQL Server selected "first" row with ID=1 from source table (TableY) which has 2 rows with ID=1 ({1,'A'} and {1,'AA'})
2           B
3           NULL
*/

但是如果我更改源表TableY中的行顺序,那么相同的UPDATE语句的结果将会有所不同:

TRUNCATE TABLE TableY;
INSERT TableY (ID,Col2)
SELECT 1,'AA' UNION ALL SELECT 2,'B' UNION ALL SELECT 4,'D';
INSERT TableY (ID,Col2)
SELECT 1,'A';

UPDATE TableX SET Col2 = NULL;
UPDATE  x
SET     Col2 = y.Col2
FROM    TableX x INNER JOIN TableY y ON x.ID = y.ID;
SELECT  * FROM TableX;
/*
ID          Col2
----------- ----------
1           AA <-- The same UPDATE gives different results: 'AA' instead of 'A'
2           B
3           NULL
*/

如果我执行解决方案#2和#4,那么我将收到一个例外(这很好)

Solution #2:
Msg 512, Level 16, State 1, Line 17
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.

Solution #4:
Msg 8672, Level 16, State 1, Line 55
The MERGE statement attempted to UPDATE or DELETE the same row more than once. This happens when a target row matches more than one source row. A MERGE statement cannot UPDATE/DELETE the same row of the target table multiple times. Refine the ON clause to ensure a target row matches at most one source row, or use the GROUP BY clause to group the source rows.

因为我知道我必须重写那些陈述

-- Solution  #2
UPDATE TableX SET Col2 = NULL;
UPDATE  TableX 
SET     Col2 = 
        (
            SELECT  MAX(y.Col2) -- or MIN or something else
            FROM    TableY y 
            WHERE   TableX.ID = y.ID
        )

-- Solution  #4 (SQL2008+)
UPDATE TableX SET Col2 = NULL;
MERGE INTO TableX x
USING 
(
    SELECT  a.ID, MAX(a.Col2) AS Col2 -- or MIN or something else
    FROM    TableY a
    GROUP BY a.ID
) y ON x.ID = y.ID
WHEN MATCHED THEN
    UPDATE SET Col2 = y.Col2;

答案 2 :(得分:0)

你可以这样做

声明@Var = varchar(50)

从table1中选择@Var = Column_n;

update table2 set column_m = newValue where column_a = @Var;