用户自定义函数错误
今天写了自定义函数,执行查询没有问题,但是具体运行时就总是报错服务器: 消息 208,级别 16,状态 1,过程 chmxzb,行 26
对象名 '@liushuizh' 无效。
查了一些资料,“服务器: 消息 208,级别 16,状态 1”说明相应表不存在,但是函数中涉及的两个表格@djb,@liushuizh都是存在的,而且运行实例时也只是提示@liushuizh不存在,并没有提示@djb不存在。查了一上午的资料也没有找到原因,请大家伙帮个忙看看。
函数的具体内容如下:
create function chmxzb(@gsmc varchar(50),@ckmc varchar(10),@wpbh varchar(10),@rqtj varchar(200))
returns @chmxzb table
(
rq1 datetime,
djh varchar(9),
czlx varchar(20),
sname varchar(200),
zyxx varchar(200),
rshliang decimal(18,2),
rdj decimal(18,2),
rje decimal(18,2),
cshliang decimal(18,2),
cdj decimal(18,2),
cje decimal(18,2)
)
as
begin
declare @djb varchar(6)
declare @liushuizh varchar(20)
select @djb=djb,@liushuizh=zhangbiao from gongsibiao where gsmc=@gsmc and nian='2008'
if @ckmc<>'工程库'
insert into @chmxzb select convert(char(20),rq,20) as rq1,[@djb].djh,czlx,sname,case when czlx='调出' then '调往'+sname else sname+czlx end as zyxx,case when czlx in ('采购进货','调入') then shliang when czlx='采购退货' then -shliang else null end as rshliang,case when czlx in ('采购进货','调入','采购退货') then bhsdj else null end as rdj,case when czlx in ('采购进货','调入') then bhsje when czlx='采购退货' then -bhsje else null end as rje,case when czlx in ('领用出库','销售出库','调出') then shliang when czlx in('领用退库','销售退库') then -shliang else null end as cshliang,case when czlx in ('领用出库','销售出库','领用退库','销售退库','调出') then bhsdj else null end as cdj,case when czlx in ('领用出库','销售出库','调出') then bhsje when czlx in('领用退库','销售退库') then -bhsje else null end as cje from [@djb],[@liushuizh] where [@djb].djh=[@liushuizh].djh and [@liushuizh].zfbz=0 and [@djb].zfbz=0 and ckmc=@ckmc and wpbh=@wpbh+@rqtj
else
insert into @chmxzb select convert(char(20),rq,20) as rq1,[@djb].djh,czlx,sname,case when czlx='调出' then '调往'+sname else sname+czlx end as zyxx,case when czlx in ('采购进货','调入') then shliang when czlx='采购退货' then -shliang else null end as rshliang,case when czlx in ('采购进货','调入','采购退货') then hsdj else null end as rdj,case when czlx in ('采购进货','调入') then zjiner when czlx='采购退货' then -zjiner else null end as rje,case when czlx in ('领用出库','销售出库','调出') then shliang when czlx in('领用退库','销售退库') then -shliang else null end as cshliang,case when czlx in ('领用出库','销售出库','领用退库','销售退库','调出') then hsdj else null end as cdj,case when czlx in ('领用出库','销售出库','调出') then zjiner when czlx in('领用退库','销售退库') then -zjiner else null end as cje from [@djb],[@liushuizh] where [@djb].djh=[@liushuizh].djh and [@liushuizh].zfbz=0 and [@djb].zfbz=0 and ckmc=@ckmc and wpbh=@wpbh+@rqtj
return
end