2014-02-28 87 views
1

我是SQL新手,仍然在學習DBMS。如何根據另一列中的值更新一列中的值

我有一個名爲ApprovalID的列的表,其類型爲int,值爲1-5。 (這些值是通過網站上運行的代碼更新的 我有另一個名爲批准的列,我從ApprovalID列值中複製,但它是varchar類型並顯示字符串(1的孵化器,2的購買,3的飼養,來源未知4和未被批准5)

現在,Approval列並未自行更新,如果ApprovalID列中的相應值已更改,我必須手動運行查詢以更改其中的值。

我需要一個解決方案,以便在ApprovalID中的值發生變化時自動更新Approval中的值,而不是1,2,3,4,5我想顯示上面提到的字符串

請幫忙。而不是

+1

你真的應該有一個存儲ApprovalIDs和他們的名字一個表,然後針對它 – Lamak

+1

執行'JOIN'你會以錯誤的方式去做。您應該將'approval'列提取到另一個具有兩列的靜態表,即'approval_id'和'approval',並且在想要顯示「批准」字符串時對原始表使用'join'。這個過程被稱爲[規範化](http://en.wikipedia.org/wiki/Database_normalization)。 – Mureinik

+0

計算列在這裏是正確的選擇,因爲「批准」是「ApprovalID」的直接翻譯。您可以創建UDF來轉換值並將該udf用作列「批准」定義。 –

回答

1

: -

select approvalid, approval, othercolumn, anothercolumn 
from tablea 

做一次: -

create tableb (
    approvalid int, 
    approval varchar(50) 
) 

insert into tableb values (1, 'Hatchery') 
insert into tableb values (2, 'Purchase') 
insert into tableb values (3, 'Rearing') 
insert into tableb values (4, 'Source Unknown') 
insert into tableb values (5, 'Not Approved') 

從TableA的下降批准列,然後做到這一點,當你需要的描述: -

select a.approvalid, b.approval, a.othercolumn, a.anothercolumn 
from tablea a 
join tableb b on b.approvalid=a.approvalid 

這是一個開始。有很多事情你也需要了解與主鍵和索引...

0

您可以使用多種approuches做到這一點:

計算列

CREATE TABLE t3 
(
    ID INT IDENTITY(1,1), 
    ApprovalID INT, 
    Approval AS (CASE 
        WHEN ApprovalID = 1 THEN 'Hatchery' 
        WHEN ApprovalID = 2 THEN 'Purchase' 
        WHEN ApprovalID = 3 THEN 'Rearing' 
        WHEN ApprovalID = 4 THEN 'Source Unknown' 
        WHEN ApprovalID = 5 THEN 'Not Approved' 
        ELSE '<Unknown approval state>' 
       END 
     ) PERSISTED 
) 

INSERT INTO t3(ApprovalID) VALUES(1) 
INSERT INTO t3(ApprovalID) VALUES(2) 
INSERT INTO t3(ApprovalID) VALUES(3) 
INSERT INTO t3(ApprovalID) VALUES(4) 
INSERT INTO t3(ApprovalID) VALUES(5) 
UPDATE t3 
    set ApprovalID = 2 
    where ApprovalID = 1 
SELECT * FROM t3 

觸發器

DROP TABLE t3 
CREATE TABLE t3 
(
    ID INT IDENTITY(1,1), 
    ApprovalID INT, 
    Name NVARCHAR(256), 
) 
GO 
CREATE TRIGGER t3_trigger 
    ON t3 
    AFTER INSERT, UPDATE 
    AS 
    UPDATE t3 
     SET Name = CASE 
       WHEN i.ApprovalID = 1 THEN 'Hatchery' 
       WHEN i.ApprovalID = 2 THEN 'Purchase' 
       WHEN i.ApprovalID = 3 THEN 'Rearing' 
       WHEN i.ApprovalID = 4 THEN 'Source Unknown' 
       WHEN i.ApprovalID = 5 THEN 'Not Approved' 
       ELSE '<Unknown approval state>' 
      END 
    FROM t3 INNER JOIN inserted i ON t3.ID = i.ID 
GO 
INSERT INTO t3(ApprovalID) VALUES(1) 
INSERT INTO t3(ApprovalID) VALUES(2) 
INSERT INTO t3(ApprovalID) VALUES(3) 
INSERT INTO t3(ApprovalID) VALUES(4) 
INSERT INTO t3(ApprovalID) VALUES(5) 

UPDATE t3 set ApprovalID = 2 WHERE ApprovalID = 1 

SELECT t3.*, Approval.Name FROM t3 LEFT JOIN Approval ON t3.ApprovalID = Approval.ApprovalID 

但最好的方法是分割數據在兩個表

CREATE TABLE t3 
(
    ID INT IDENTITY(1,1), 
    ApprovalID INT, 
) 

CREATE TABLE Approval 
(
    ApprovalID INT, 
    Name NVARCHAR(256) 
) 

INSERT INTO Approval(ApprovalID, Name) VALUES(1, 'Hatchery') 
INSERT INTO Approval(ApprovalID, Name) VALUES(2 , 'Purchase') 
INSERT INTO Approval(ApprovalID, Name) VALUES(3, 'Rearing') 
INSERT INTO Approval(ApprovalID, Name) VALUES(4, 'Source Unknown') 
INSERT INTO Approval(ApprovalID, Name) VALUES(5, 'Not Approved') 

INSERT INTO t3(ApprovalID) VALUES(1) 
INSERT INTO t3(ApprovalID) VALUES(2) 
INSERT INTO t3(ApprovalID) VALUES(3) 
INSERT INTO t3(ApprovalID) VALUES(4) 
INSERT INTO t3(ApprovalID) VALUES(5) 

並使用選擇數據查詢象下面這樣:

SELECT t3.*, Approval.Name FROM t3 LEFT JOIN Approval ON t3.ApprovalID = Approval.ApprovalID 
相關問題