sql数据库中有一段,执行存储过程时:当字段的值为1时修改成0当字段的值为0时修改成1该如何来写存储过程?

解决方案 »

  1.   

    用 case when ... else ... end
      

  2.   

    用 case when ... else ... end
      

  3.   

    用 case when ... else ... end
      

  4.   


    declare @a int
    if @a =0
    begin
       print('0')
    end
    if @a =1
    begin
       print('1')
    end
      

  5.   

    case when  列=0 then 1 else 1 end
      

  6.   

    UPDATE Test SET i=CASE WHEN i=1 THEN 0 ELSE 1 end
      

  7.   


    update DS_Brand set Passed=case Passed when 1 then 0 else 1 end where id=49
    --或
    declare @passed int
    set @passed = (select passed from DS_Brand where id=49)
    if(@passed=1)
    update DS_Brand set Passed=0 where id=49
    else
    update DS_Brand set Passed=1 where id=49
      

  8.   

    UPDATE Test SET i=CASE WHEN i=1 THEN 0 ELSE 1 end
      

  9.   


    update DS_Brand set Passed=case Passed when 1 then 0 else 1 end where id=49
    --或
    declare @passed int
    set @passed = (select passed from DS_Brand where id=49)
    if(@passed=1)
    update DS_Brand set Passed=0 where id=49
    else
    update DS_Brand set Passed=1 where id=49
      

  10.   


    update DS_Brand set Passed=case Passed when 1 then 0 else 1 end where id=49
    --或
    declare @passed int
    set @passed = (select passed from DS_Brand where id=49)
    if(@passed=1)
    update DS_Brand set Passed=0 where id=49
    else
    update DS_Brand set Passed=1 where id=49
      

  11.   

    CREATE PROCEDURE "PROC_ID" (
        IN "ID"    VARCHAR(100)
    begindeclare C_count int;
    set C_count = (select "字段" from "表" where "ID"=CLID); if C_count = '1' then
    update "表" 
    set "字段" = '0'
    where "ID"=CLID
    end if;if C_count = '0' then
    update "表" 
    set "字段" = '1'
    where "ID"=CLID
    end if;go
      

  12.   

    select * into NewTable
    from (
    select 1 as id,
    1 as  isUsed
    union
    select 2,0
    union
    select 3,1) t
    goselect * from NewTable
    go
    update NewTable set isUsed = case
    when (isUsed=1) then 0
    else 1 end
    go
    select * from NewTable
      

  13.   

    set C_count = (select "字段" from "表" where "ID"=ID); 
      

  14.   

    CLID改成你的入参ID 第一次写错