如何根据另一列中的值更新一列中的值

时间:2014-02-28 20:27:01

标签: sql sql-server

我是SQL新手,仍在学习DBMS的内外。

我有一个名为ApprovalID的列的表,其类型为int,值为1-5。 (这些值由网站上运行的代码更新。 我有另一个名为Approval的列,我从ApprovalID列值复制,但它是varchar类型并显示字符串(Hatchery为1,Purchase for 2,Rearing为3,Source Unknown为4和Not Approved for 5)。

现在,Approval列不会自行更新,如果ApprovalID列中的相应值已更改,我必须手动运行查询以更改其中的值。

如果ApprovalID中的值发生了变化,我需要一个自动更新Approval值的解决方案,但我想显示上面提到的字符串而不是1,2,3,4,5。

请帮忙。

2 个答案:

答案 0 :(得分: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

这是一个开始。你还需要学习很多东西,比如主键和索引......

答案 1 :(得分: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