if OBJECT_ID('ds') is not null 
drop proc ds
GOcreate proc ds
@type int
as
if @type=1
print ('11111111111111111')if @type=2
print ('22222222222222222')else 
print ('333333333333333333')
我创建了一条存储过程,由于是练习的也没太在意。但是当我执行 exec ds 1 的时候我踌躇了。他会打印出 11111111111111
33333333333333   当我执行exec ds 2 的时候  会执行 222222222222    
我自己也想不通了!!!!!

解决方案 »

  1.   

    怎么想不通if OBJECT_ID('ds') is not null  
    drop proc ds
    GOcreate proc ds
    @type int
    as
    if @type=1
    print ('11111111111111111')if @type=2
    print ('22222222222222222')else  
    print ('333333333333333333')
    go
    exec ds 1
    11111111111111111
    333333333333333333
    原因在于你的代码中的那几个if第一个if是判断是不是=1  
    第二个是判断是不是=2不是就打印出33333333333
    执行的时候两个if都会执行的 在第二个if前面加上else就可以了
    if OBJECT_ID('ds') is not null  
    drop proc ds
    GOcreate proc ds
    @type int
    as
    if @type=1
    print ('11111111111111111')
    else
    if @type=2
    print ('22222222222222222')else  
    print ('333333333333333333')
    go
    exec ds 111111111111111111
      

  2.   

    if @type=1
    begin
       print ('11111111111111111')
    endif @type=2
    print ('22222222222222222')else  
    print ('333333333333333333')是两个不同if 语句 所以 1时 打印111111,和333333.
      

  3.   

    注意一下判断条件 就很容易理解 第一次传入参数为1 满足 第一个if和第二个else条件 传入参数为2 至满足第二个else条件 所以就输出啊
      

  4.   

    小李子解释的很清楚了,楼主其实自己也发现问题了,就是想当然了。第二个if条件前面加else
      

  5.   

    if OBJECT_ID('ds') is not null  
    drop proc ds
    GOcreate proc ds
    @type int
    as
    if @type=1
    print ('11111111111111111')
    else if @type=2
    print ('22222222222222222')
    else  
    print ('333333333333333333')
    /*--楼主的代码
    create proc ds
    @type int
    as
    --第一部分
    if @type=1
    print ('11111111111111111')
    --
    --第二部分
    if @type=2
    print ('22222222222222222')
    else  
    print ('333333333333333333')
    --部分--1.执行时,第一部分的执行,不会决定第二部分是否执行
    --2.执行第二部分时,进入了if就不会执行else了,反之就会执行else部分
    */