Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.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 server 在SQL Server 2008中从另一个表更新表_Sql Server_Sql Server 2008_Tsql - Fatal编程技术网

Sql server 在SQL Server 2008中从另一个表更新表

Sql server 在SQL Server 2008中从另一个表更新表,sql-server,sql-server-2008,tsql,Sql Server,Sql Server 2008,Tsql,我有一个带有一组列的临时表变量: Declare @GearTemp table ( ItemNumber varchar(20), VendorNumber varchar(6), ItemStatus varchar(20), Style varchar(20), ItemName varchar(100), ItemDescription varchar(1000),

我有一个带有一组列的临时表变量:

 Declare @GearTemp table
    (
        ItemNumber varchar(20),
        VendorNumber varchar(6),
        ItemStatus varchar(20),
        Style varchar(20),
        ItemName varchar(100),
        ItemDescription varchar(1000),
        Color varchar(50),
        [Size] varchar(50),
        ItemCost decimal(9,4),
        IsQuickShipFl bit,
        IsEmbroiderable bit,
        IsBackOrderable bit,
        LoadDate smalldatetime
    )
它通过insert语句从另一个表中填充数据,我想获取该数据并更新我的Products表。如果可能的话,我想这样做:

Update Products blah blah blah  all columns where itemnumbers match up
SELECT * FROM @GearTemp FT 
WHERE EXISTS (SELECT P.ItemNumber FROM Products P WHERE FT.ItemNumber = P.ItemNumber)
UPDATE p SET X = gt.X, Y = gt.Y -- etc... (not sure whether your column names match up)
FROM Products p
    INNER JOIN @GearTemp gt ON p.ItemNumber = gt.ItemNumber

这可能吗?如果不是,请给我指出正确的方向

如果我理解正确,您可以使用以下内容:

Update Products blah blah blah  all columns where itemnumbers match up
SELECT * FROM @GearTemp FT 
WHERE EXISTS (SELECT P.ItemNumber FROM Products P WHERE FT.ItemNumber = P.ItemNumber)
UPDATE p SET X = gt.X, Y = gt.Y -- etc... (not sure whether your column names match up)
FROM Products p
    INNER JOIN @GearTemp gt ON p.ItemNumber = gt.ItemNumber

请注意,如您在上面的评论中所述,只有在
@GearTemp
中每个
项目编号只有一个条目时,这才有效。如果我理解正确,您可以使用以下内容:

Update Products blah blah blah  all columns where itemnumbers match up
SELECT * FROM @GearTemp FT 
WHERE EXISTS (SELECT P.ItemNumber FROM Products P WHERE FT.ItemNumber = P.ItemNumber)
UPDATE p SET X = gt.X, Y = gt.Y -- etc... (not sure whether your column names match up)
FROM Products p
    INNER JOIN @GearTemp gt ON p.ItemNumber = gt.ItemNumber

请注意,正如您在上面的评论中所述,只有在
@GearTemp
中每个
ItemNumber

只有一个条目的情况下,这才有效。因为您使用的是SQL Server 2008,所以您可以使用MERGE语句:

-- Target Table

DECLARE @tgt TABLE (OrdID INT, ItemID INT, Qty INT, Price MONEY);
INSERT INTO @tgt (OrdID, ItemID, Qty, Price)
    SELECT 1, 100, 10, 10.00 UNION ALL
    SELECT 1, 101, 10, 12.00


OrdID       ItemID      Qty         Price
----------- ----------- ----------- ---------------------
1           100         10          10.00
1           101         10          12.00

-- Source Table 
DECLARE @src TABLE (OrdID INT, ItemID INT, Qty INT, Price MONEY);
INSERT INTO @src (OrdID, ItemID, Qty, Price)
    SELECT 1, 100, 12, 10.00 UNION ALL
    SELECT 1, 102, 10, 12.00 UNION ALL
    SELECT 1, 103, 5, 7.00


OrdID       ItemID      Qty         Price
----------- ----------- ----------- ---------------------
1           100         12          13.00
1           102         10          12.00
1           103         5           7.00

MERGE @tgt AS t
USING @src AS s
ON t.OrdID = s.OrdID AND t.ItemID = s.ItemID
WHEN MATCHED THEN
  UPDATE SET 
    t.Qty = s.Qty,
    t.Price = s.Price;



Content of the target table after the MERGE operation:
OrdID       ItemID      Qty         Price
----------- ----------- ----------- ---------------------
1           100         12          13.00

由于您使用的是SQL Server 2008,因此可以使用MERGE语句:

-- Target Table

DECLARE @tgt TABLE (OrdID INT, ItemID INT, Qty INT, Price MONEY);
INSERT INTO @tgt (OrdID, ItemID, Qty, Price)
    SELECT 1, 100, 10, 10.00 UNION ALL
    SELECT 1, 101, 10, 12.00


OrdID       ItemID      Qty         Price
----------- ----------- ----------- ---------------------
1           100         10          10.00
1           101         10          12.00

-- Source Table 
DECLARE @src TABLE (OrdID INT, ItemID INT, Qty INT, Price MONEY);
INSERT INTO @src (OrdID, ItemID, Qty, Price)
    SELECT 1, 100, 12, 10.00 UNION ALL
    SELECT 1, 102, 10, 12.00 UNION ALL
    SELECT 1, 103, 5, 7.00


OrdID       ItemID      Qty         Price
----------- ----------- ----------- ---------------------
1           100         12          13.00
1           102         10          12.00
1           103         5           7.00

MERGE @tgt AS t
USING @src AS s
ON t.OrdID = s.OrdID AND t.ItemID = s.ItemID
WHEN MATCHED THEN
  UPDATE SET 
    t.Qty = s.Qty,
    t.Price = s.Price;



Content of the target table after the MERGE operation:
OrdID       ItemID      Qty         Price
----------- ----------- ----------- ---------------------
1           100         12          13.00

对于任何给定的产品记录,@GearTemp中是否可以有多个记录?在这种情况下,您希望发生什么?Itemnumber是主键,因此数字将是唯一的。这就是你要问的吗?ItemNumber是产品和@GearTemp的主键?我想知道对于给定的ItemNumber,@GearTemp中是否可以有多个条目。如果是这样,您希望如何处理?不,对于给定的itemnumber,只有一条记录。@Karl:我不太清楚为什么您会感到困惑。我在问题中明确表示,我希望更新一个名为“产品”的表格。数据来自temp表变量。对于任何给定的产品记录,@GearTemp中是否可以有多条记录?在这种情况下,您希望发生什么?Itemnumber是主键,因此数字将是唯一的。这就是你要问的吗?ItemNumber是产品和@GearTemp的主键?我想知道对于给定的ItemNumber,@GearTemp中是否可以有多个条目。如果是这样,您希望如何处理?不,对于给定的itemnumber,只有一条记录。@Karl:我不太清楚为什么您会感到困惑。我在问题中明确表示,我希望更新一个名为“产品”的表格。数据来自一个临时表变量。您的回答为我指明了正确的方向,但语法应该是这样的:更新P SET P.X=GT.X,P.Y=GT.Y from Products P internal JOIN@GearTemp AS GT ON P.X=GT.XAh,对不起,在我发布之前没有在SSMS中进行语法检查。我会相应地编辑它。不过,很高兴它帮助了你!你的回答为我指明了正确的方向,但语法应该是这样的:从产品P internal JOIN@GearTemp AS GT ON P.X=GT.XAh更新P SET P.X=GT.X,P.Y=GT.Y,抱歉,在我发布之前没有在SSMS中进行语法检查。我会相应地编辑它。不过,很高兴它帮助了你!