sql中的select查询中的case语句

时间:2021-03-22 09:11:25

I have a stored procedure to load the data from one table to another table.

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

i need to set the column value of the destination table based on the two values of the select statement, some thing like the below example.

我需要根据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.

can any one help me to find out how to update the a column in based on the two previous column values in the same select query?

有谁能帮我找到如何根据同一个select查询中的前两个列值更新a列吗?

i have tried with the below sample example to understand but it was failed to parse.

我试图理解下面的示例,但它未能进行解析。

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

Help me on this!!

帮我在这! !

thanks, Venkat

谢谢,引导

3 个解决方案

#1


3  

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

#2


1  

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

&&在SQL中无效。使用并附加一个条件。

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

#3


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

#1


3  

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

#2


1  

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

&&在SQL中无效。使用并附加一个条件。

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

#3


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