关于SQL中的"null",以及一个报错的存储过程

sdo23 2010-08-11 10:05:27
问题1:

declare @cid
select @cid=id from a_company where com_id=1113
if @cid<=0

如果查询不到指定,@cid返回什么值?null ? 0 ? 什么情况下返回Null值
----------------------------------------------

问题2:
下面的代码报错

/***********************************
添加公司IP的存储过程
************************************/
if exists (select * from sysobjects where name='Proc_Add_CID')
drop procedure Proc_Add_CID
go
create procedure Proc_Add_CID
@cid int,--公司的ID,与a_company匹配
@result int output
as
/*
@a_com_id:存放a_company的id
@v_cid存放Vote的CID,用于判断是否存在该id,如果存在就不用添加
*/
declare @a_com_id int,@v_cid int

--先查询a_company表,输入的公司ID是否存在于该表,不存在就不能添加
select @a_com_id=id from a_company where id=@cid
if @a_com_id<=0
set @result=-1
return @result

--先判断是否存在该公司ID,存在就不用重复添加
select @v_cid=id from Vote where Com_id=@cid
if @v_cid>0
set @result=0
return @result

insert into Vote(Com_id) values(@cid)
set @result=1
return @result

--执行存储过程
declare @resu int
exec Proc_Add_CID 35,@resu output
--print convert(varchar,@resu)
--select top 10 * from a_company
if @resu<=0
print '添加失败'
return
print '添加成功'

错误信息:Proc_Add_CID过程试图返回状态 NULL,这是不允许的。将返回状态 0。
...全文
192 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
luoxj_win 2010-08-13
  • 打赏
  • 举报
回复
第一个:返回的是Null


添加公司IP的存储
************************************/
if exists (select * from sysobjects where name='Proc_Add_CID')
drop procedure Proc_Add_CID
go
create procedure Proc_Add_CID
@cid int,--公司的ID,与a_company匹配
@result int output
as
begin
/*
@a_com_id:存放a_company的id
@v_cid存放Vote的CID,用于判断是否存在该id,如果存在就不用添加
*/

--先查询输入的公司ID是否存在于a_company表,不存在就不能添加
if exists(select 1 from a_company where id=@cid)
begin
--在判断vote表是否存在该ID,存在就不能重复添加
if exists(select 1 from Vote where Com_id=@cid)
begin
set @result=0
end
else
begin--Vote表中没该CID
insert into Vote(Com_id) values(@cid)
set @result=1
end
end
else
begin
set @result=-1
end
end--最靠外的begin end语句
claro 2010-08-11
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 hao1hao2hao3 的回复:]
这条语句是没有查到任何记录的。

下面打印的结果是

"结果不为0"

说明返回的结果不是null。

这样的推理不知够不够严谨,请大家指正。谢谢!
[/Quote]的确跟你想的一样,理解错误。
比如
create table #a (com_id int,id int)
go
insert into #a
select 1 com_id,11 id
go
declare @cid int
select @cid=id from #a where com_id=1113
select @cid
select ISNULL(@cid,0)
--result:
-----------
NULL

(1 行受影响)


-----------
0

(1 行受影响)

hao1hao2hao3 2010-08-11
  • 打赏
  • 举报
回复
第一个应该不是返回null吧


declare @cid varchar(20)
--declare @sql varchar(200)
select basedataname from sysbasedata where basedataid =0
select @cid=isnull(basedataname,'0') from sysbasedata where basedataid = 0
if @cid ='0'
print '结果为0'
else
print '结果不为0'


本地的测试情况如下:

select basedataname from sysbasedata where basedataid =0

这条语句是没有查到任何记录的。

下面打印的结果是

"结果不为0"

说明返回的结果不是null。

这样的推理不知够不够严谨,请大家指正。谢谢!

claro 2010-08-11
  • 打赏
  • 举报
回复
--try
if ISNULL(@a_com_id,0)<=0
if ISNULL(@v_cid,0)>0
billpu 2010-08-11
  • 打赏
  • 举报
回复
又看了一下,错了
当return用于存储过程时 RETURN不能返回空值。如果过程试图返回空值,将生成警告信息并返回 0 值。所有系统存储过程返回 0 值表示成功,返回非零值则表示失败。
budong0000 2010-08-11
  • 打赏
  • 举报
回复
null在sql server中比较特殊,它就是:什么都不是。
null和任何操作结果都为null,再where null 就得不到结果集。


MERGE Production.ProductInventory AS target
USING (SELECT ProductID, SUM(OrderQty) FROM Sales.SalesOrderDetail AS sod
JOIN Sales.SalesOrderHeader AS soh
ON sod.SalesOrderID = soh.SalesOrderID
AND soh.OrderDate = @OrderDate
GROUP BY ProductID) AS source (ProductID, OrderQty)
ON (target.ProductID = source.ProductID)
WHEN MATCHED AND target.Quantity - source.OrderQty <= 0
THEN DELETE
WHEN MATCHED
THEN UPDATE SET target.Quantity = target.Quantity - source.OrderQty,
target.ModifiedDate = GETDATE()
OUTPUT $action, Inserted.ProductID, Inserted.Quantity, Inserted.ModifiedDate, Deleted.ProductID,
Deleted.Quantity, Deleted.ModifiedDate;

hao1hao2hao3 2010-08-11
  • 打赏
  • 举报
回复
第一个肯定是null的。

我想通过

declare @cid varchar(20)
select @cid=isnull(id,'0') from a_company where com_id = 3
print @cid


来测试,但是好像得不到效果,哪位大牛给看看?
华夏小卒 2010-08-11
  • 打赏
  • 举报
回复
select @a_com_id=id from a_company where id=@cid


这种写法不好

反正都是等于id

if exists(select 1 from a_company where id=@cid) and @cid>0
set @result=0
else
set @result= 改成你想要的值 --当然如果不存在,肯定返回null ,这个你自己改成想要的值

return @result
billpu 2010-08-11
  • 打赏
  • 举报
回复
问题1:null
问题2 你设置的@result int output 为整型不允许为null所以只能0
华夏小卒 2010-08-11
  • 打赏
  • 举报
回复
应该返回的是null
sdo23 2010-08-11
  • 打赏
  • 举报
回复
补充:declare @cid 少了个int类型
sdo23 2010-08-11
  • 打赏
  • 举报
回复
看来,各有各的见解!
希望有更多的前辈来看看!

22,298

社区成员

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

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