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
我自己也想不通了!!!!!
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
我自己也想不通了!!!!!
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
begin
print ('11111111111111111')
endif @type=2
print ('22222222222222222')else
print ('333333333333333333')是两个不同if 语句 所以 1时 打印111111,和333333.
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部分
*/