3

我有一个存储过程来将数据从一个表加载到另一个表。

我需要根据 select 语句的两个值设置目标表的列值,如下例所示。

insert into table table_name
( value1, value 2,value 3)

select (value 1,value2 ,
case value3 

when value1 = 'somevalue' &&* value2 = 'somevalue'

then 'x' 

else 'y'
End

from table_name.

谁能帮我找出如何根据同一个选择查询中的前两个列值更新a列?

我尝试使用以下示例示例来理解,但无法解析。

INSERT INTO HumanResources.departmentcopy  
( DepartmentID,GroupName,Name,temp)

SELECT DepartmentID,GroupName,Name,
CASE temp
WHEN  DepartmentID = 1 && Name = 'Engineering and Research'
THEN 'sucessful'
ELSE 'unsucessful'
END
FROM HumanResources.department

帮我解决这个问题!!

谢谢,文卡特

4

3 回答 3

7

You were very close:

INSERT INTO HumanResources.departmentcopy(DepartmentID, GroupName, Name, temp)
SELECT  DepartmentID,
        GroupName,
        Name,
        CASE WHEN DepartmentID = 1 AND Name = 'Engineering and Research'
        THEN 'sucessful' ELSE 'unsucessful' END
FROM HumanResources.department
于 2013-09-16T18:32:08.007 回答
1

&& is not valid in SQL. Use AND to append a condition.

INSERT INTO HumanResources.departmentcopy( DepartmentID,GroupName,Name,temp)
    SELECT  DepartmentID,
            GroupName,
            Name, 
            CASE
                WHEN DepartmentID = 1 AND Name = 'Engineering and Research' THEN 'sucessful' 
                ELSE 'unsucessful' 
                END 
            FROM HumanResources.department
于 2013-09-16T18:31:02.897 回答
0
INSERT INTO HumanResources.department(DepartmentID, GroupName, Name, temp)

SELECT  DepartmentID,
        GroupName,
        Name,

CASE WHEN DepartmentID = 1 AND Name = 'Engineering and Research'

THEN 'sucessful' ELSE 'unsucessful' END

FROM HumanResources.department
于 2014-10-09T10:40:24.143 回答