求大神帮忙啊

s250964630 2012-10-30 10:22:31
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')


我创建了一条存储过程,由于是练习的也没太在意。但是当我执行 exec ds 1 的时候我踌躇了。他会打印出 11111111111111
33333333333333 当我执行exec ds 2 的时候 会执行 222222222222
我自己也想不通了!!!!!
...全文
73 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ChangeMyself2012 2012-10-31
  • 打赏
  • 举报
回复


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部分
*/




开启时代 2012-10-30
  • 打赏
  • 举报
回复
if @type=1
begin
print ('11111111111111111')
end

if @type=2
print ('22222222222222222')

else
print ('333333333333333333')

是两个不同if 语句 所以 1时 打印111111,和333333.
  • 打赏
  • 举报
回复
怎么想不通

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

以学习为目的 2012-10-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

怎么想不通
SQL code

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 ('33333333……
[/Quote]小李子解释的很清楚了,楼主其实自己也发现问题了,就是想当然了。第二个if条件前面加else
clicsullivan 2012-10-30
  • 打赏
  • 举报
回复
注意一下判断条件 就很容易理解 第一次传入参数为1 满足 第一个if和第二个else条件 传入参数为2 至满足第二个else条件 所以就输出啊
six-years 2012-10-30
  • 打赏
  • 举报
回复
嘿嘿 楼主要淡定啊 当局者迷旁观者清 下次出现这问题 去厕所蹲蹲 回来再看 一眼就能看出来了 也不用浪费分了

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧