22,209
社区成员
发帖
与我相关
我的任务
分享
if OBJECT_ID('ds') is not null
drop proc ds
GO
create 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部分
*/
if OBJECT_ID('ds') is not null
drop proc ds
GO
create 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
GO
create proc ds
@type int
as
if @type=1
print ('11111111111111111')
else
if @type=2
print ('22222222222222222')
else
print ('333333333333333333')
go
exec ds 1
11111111111111111