日期:2014-05-17  浏览次数:20526 次

SQL CASE WHEN 插入问题
SQL code
    INSERT INTO @tabResult (ContractCode,SD_State_InOut,First_Map,ContractName,AlterMoney,SignDate,ContractState)
    CASE 
    WHEN @ContractType = '1' THEN
        SELECT a.ContractCode,a.SD_State_InOut,a.ContractName,a.AlterMoney,a.SignDate,a.ContractState,b.ConsumerName AS First_Map 
        FROM BS_Contract AS A left join OA_Consumer AS B ON A.First = B.ID
        WHERE A.ID = @ContractID
    WHEN @ContractType = '0' THEN
        SELECT a.ContractCode,a.SD_State_InOut,a.ContractName,a.AlterMoney,a.SignDate,a.ContractState,b.ConsumerName AS First_Map 
        FROM BS_Contract_AUDIT AS A left join OA_Consumer AS B ON A.First = B.ID
        WHERE A.ID = @ContractID
    ELSE
        SELECT '','','','',NULL,'',''
    END


相信大家看到这个SQL 应该明白我的意思:
就是当 @ContractType 不同时 插入的信息也不一样

------解决方案--------------------
sql不支持这样的语法
SQL code

if @ContractType = '1' 
begin
    INSERT INTO @tabResult (ContractCode,SD_State_InOut,First_Map,ContractName,AlterMoney,SignDate,ContractState)
     SELECT a.ContractCode,a.SD_State_InOut,a.ContractName,a.AlterMoney,a.SignDate,a.ContractState,b.ConsumerName AS First_Map 
        FROM BS_Contract AS A left join OA_Consumer AS B ON A.First = B.ID
        WHERE A.ID = @ContractID
end
else
begin
    if  @ContractType = '0'
    begin
        INSERT INTO @tabResult (ContractCode,SD_State_InOut,First_Map,ContractName,AlterMoney,SignDate,ContractState)
           SELECT a.ContractCode,a.SD_State_InOut,a.ContractName,a.AlterMoney,a.SignDate,a.ContractState,b.ConsumerName AS First_Map 
            FROM BS_Contract_AUDIT AS A left join OA_Consumer AS B ON A.First = B.ID
            WHERE A.ID = @ContractID
    end
    else
    begin
     INSERT INTO @tabResult (ContractCode,SD_State_InOut,First_Map,ContractName,AlterMoney,SignDate,ContractState)
     SELECT '','','','',NULL,'',''
    end
end

------解决方案--------------------
SQL code
if @ContractType = '1' 
insert into ...
if @ContractType = '0'
insert into ...

------解决方案--------------------
SQL code
IF ContractType = '1'
INSERT INTO @tabResult (ContractCode,SD_State_InOut,First_Map,ContractName,AlterMoney,SignDate,ContractState)
    SELECT a.ContractCode,a.SD_State_InOut,a.ContractName,a.AlterMoney,a.SignDate,a.ContractState,b.ConsumerName AS First_Map 
    FROM BS_Contract AS A left join OA_Consumer AS B ON A.First = B.ID
    WHERE A.ID = @ContractID
ELSE IF  @ContractType = '0' 
INSERT INTO @tabResult (ContractCode,SD_State_InOut,First_Map,ContractName,AlterMoney,SignDate,ContractState)
    SELECT a.ContractCode,a.SD_State_InOut,a.ContractName,a.AlterMoney,a.SignDate,a.ContractState,b.ConsumerName AS First_Map 
    FROM BS_Contract_AUDIT AS A left join OA_Consumer AS B ON A.First = B.ID
ELSE 
INSERT INTO @tabResult (ContractCode,SD_State_InOut,First_Map,ContractName,AlterMoney,SignDate,ContractState) 
    SELECT '','','','',NULL,'',''

------解决方案--------------------
不支持你这种写法,改为if语句
------解决方案--------------------
这种case when 是对的

case when 的结果只是一个值 不能是一个集合
------解决方案--------------------
1,用if else
2. 
insert into xxxx select xxxx from xxx where xxx and @contractType='1';
insert into xxxx select xxx from xxxx where xxx and @contractType='0';

------解决方案--------------------
楼上正解 用if判断
------解决方案--------------------