sql查询2个表来更新表

时间:2013-07-02 04:44:57

标签: sql sql-server

我有这些表格。我想写更新查询

   CREATE TABLE [dbo].[Feasibility](
[FeasibilityID] [int] IDENTITY(1,1) NOT NULL,
[orderstatus] [varchar](20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL CONSTRAINT [DF_Feasibility_orderstatus]  DEFAULT ('Not Placed')
     CONSTRAINT [PK_Feasibility] PRIMARY KEY CLUSTERED 
     (
[FeasibilityID] ASC
     )WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
     ) ON [PRIMARY]



     CREATE TABLE [dbo].[OrderMaster](
[orderid] [int] IDENTITY(1,1) NOT NULL,
[feasibilityid] [int] NULL,

     CONSTRAINT [PK_OrderMaster] PRIMARY KEY CLUSTERED 
    (
[orderid] ASC
     )WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
     ) ON [PRIMARY]

我想将可行性表中的orderstatus更新为ordermaster表中存在的所有记录的“已关闭”

4 个答案:

答案 0 :(得分:1)

最好使用JOIN语句而不是IN。它会快得多。 您可以尝试以下

UPDATE F
SET    F.OrderStatus = 'closed'
FROM   Feasibility  AS F
JOIN   OrderMaster AS O
ON     F.FeasibilityID = O.FeasibilityID 

答案 1 :(得分:0)

使用以下查询

update Feasibility set orderstatus='closed' where 
FeasibilityID in (select feasibilityid from OrderMaster)

答案 2 :(得分:-1)

试试这个

update Feasibility set orderstatus='closed' where FeasibilityID in (select FeasibilityID from OrderMaster)

答案 3 :(得分:-1)

试试这个......

  update feasibility set orderstatus='Closed' where feasiablityID in(select distinct          
  feasiablityID from ordermaster)

你应该区分关键字以提高效率,快速和错误工作..........