Q:如下情况:
create proc save_stopname
(@bus_id int,@stop_name varchar(50),@req int)
as
begin
declare @temp int
set @temp=(select stop_id from Chengdu_stop where stop_name=@stop_name)
@temp=?--返回值是什么?
用if ...else来判断怎么写?是if @temp==0 begin...end else begin ... end吗?
create proc save_stopname
(@bus_id int,@stop_name varchar(50),@req int)
as
begin
declare @temp int
set @temp=(select stop_id from Chengdu_stop where stop_name=@stop_name)
@temp=?--返回值是什么?
用if ...else来判断怎么写?是if @temp==0 begin...end else begin ... end吗?
解决方案 »
- 更新语句。高手进!!
- 求一个sql语句
- sql2008安装问题
- sql基础问题
- java连接sqlserver2008问题
- 如是建立这样的视图?
- 如何用SQL server2000存储过程的方法根据本地IP 限制新闻列表的输出,如果本地ip不在某条新闻的IP允许范围内 那么新闻列表就不输出
- MS SQL 2000 无法备份, 目的在于想把本地得数据库 导到网上
- 我的存储过程又错了,请大家帮我改一下,谢谢!
- SOS!!!!!!!!!!!!!!!!!!如何在SQL SERVER中实现大小写的区分???????????????????
- 大家帮忙看一下这个触发器,有错误!
- 求 sql 2005 中文 开发版 下载地址
select @sql=(select cols from 表A where id=condition)
if((@sql is null) or @sql='')
begin
print ' null'
end
else
begin
print 'not null '
end
@bus_id int,
@stop_name varchar(50),
@req int
as
begin
declare @temp int
select @temp =stop_id from Chengdu_stop where stop_name=@stop_name
if @temp>0
select '存在' as aaa
else
select '不存在' as aaa
end但是我不明白,为什么要传 @bus_id,@req 这两个参数,存储过程里面没有用到的地方啊
2楼:楼主只给出部分,不能用0来判断应用 is null
如果不用变量接收应用if exists select stop_id from Chengdu_stop .......
(@bus_id int,@stop_name varchar(50),@req int)
as
begin
declare @temp int
set @temp=(select stop_id from Chengdu_stop where stop_name=@stop_name)
--@temp=?--返回值是什么?
if @temp=1
begin
--...
end
else
begin
--...
end
set @temp=(select stop_id from Chengdu_stop where stop_name=@stop_name)
这种写法如果查询结果不止一个,就会出错.
(@bus_id int,@stop_name varchar(50),@req int)
as
begin
declare @temp int
select @temp=stop_id from Chengdu_stop where stop_name=@stop_nameif @temp is not null
--有值
else
--无值
(@bus_id int,@stop_name varchar(50),@req int)
as
begin
declare @temp int
set @temp=(select stop_id from Chengdu_stop where stop_name=@stop_name) if(@temp is null)
begin
end
else
begin
end
set @temp=(select stop_id from Chengdu_stop where stop_name=@stop_name)
如果存在多条会出现这个错误
服务器: 消息 512,级别 16,状态 1,行 2
子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。为保险起见,还是加top 1
set @temp=(select top 1 stop_id from Chengdu_stop where stop_name=@stop_name)另外,stop_id是否有空值??如果没有
就通过判断@temp is null就可以知道了。如果有空值,那么就用isnull(stop_id,'000')处理下,在通过判断@temp is null就可以知道了
(@bus_id int,@stop_name varchar(50),@req int)
as
beginselect stop_id from Chengdu_stop where stop_name=@stop_nameif stop_id is not null
--有值
else
--无值
--有值
else
--无值 没试过!
if(@temp is null)
begin
end
else
begin
end