2013-07-02 117 views
0

我有這些表。我想寫更新查詢sql查詢2表來更新表

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表

+2

到目前爲止您試過了什麼? –

回答

-1

試試這個...

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

您應distinct關鍵字來高效,快捷,誤差不工作..........

0

使用下面的查詢記錄「封閉」

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

嘗試這個

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

它最好有JOIN語句,而不是IN。它會更快。 您可以在下面試試

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