一句简单的SQL语句~~~~~~
BillMaster
ID Date
1 2007-03-07
2 2007-03-07
BillDetail
ID BillID bCheck
1 1 0
2 1 1
3 1 0
4 2 1
BillMaster 是主表, BillDetail是明细表,通过BillID关联BillMaster
bCheck为审核状态
然后现在要查询:
select ID, Date, dbo.DetailCheckState(ID) as CheckState from BillMaster
CREATE FUNCTION [dbo].[DetailCheckState] (@BillID int)
RETURNS int AS -- -1.未处理 0.部分未审核 1.已全部审核
BEGIN
declare @Yes int, @no int
select @Yes = count(*) from BillDetail where BillID =@BillID and isnull(bCheck,0) = 1 --已审核
select @no = count(*) from BillDetail where BillID =@BillID and isnull(bCheck,0) = 0
if isnull(@Yes,0) = 0 return -1 -- (-1.未处理)
if isnull(@no,0) =0 return 1 -- (1.已全部审核)
if (isnull(@Yes,0) <> 0) or (isnull(@no,0) <> 0) return 0 -- (0.部分未审核)
END
可是这个函数保存不了,提示错误: 错误 455: 函数中最后一条语句必须是返回语句。
要怎么解决啊?
------解决方案--------------------CREATE FUNCTION [dbo].[DetailCheckState] (@BillID int)
RETURNS int AS -- -1.未处理 0.部分未审核 1.已全部审核
BEGIN
declare @rt int
declare @Yes int, @no int
select @Yes = count(*) from BillDetail where BillID =@BillID and isnull(bCheck,0) = 1 --已审核
select @no = count(*) from BillDetail where BillID =@BillID and isnull(bCheck,0) = 0
if isnull(@Yes,0) = 0 set @rt= -1 -- (-1.未处理)
if isnull(@no,0) =0 set @rt=1 -- (1.已全部审核)
if (isnull(@Yes,0) <> 0) or (isnull(@no,0) <> 0) set @rt= 0 -- (0.部分未审核)
return @rt
END