存储过程、函数中会用到表变量,而且函数中不能用临时表,有时因为需要,在循环时,要先清空表变量中的内容。但表变量不像临时表一样,有一个命令(truncatetable#idqxmp)可以清空内容,于是上网找了好久,想能够找到方法清空表变量中的内容,一直没找到,后来突然想到,既然是个表,为啥不用delete试试,一试,果真可以,哈哈,原来问题不用想的那么复杂。
ALTER FUNCTION [dbo].[fn_yhdm_xwll_xxid]
(
@_xq varchar(36),
@_yhdm varchar(36),
@_lb varchar(36)
)
RETURNS @idmp table
(
xq varchar(36),yhdm varchar(36),xwid int
)
AS
begin
declare @yhsf varchar(50), @bh varchar(36),@i int,@is int,
@qxdjlist varchar(5000),@resint,@xwid int
declare @tmp table(xq varchar(36),yhdmvarchar(36),xwid int,id int identity(1,1))
declare @xwmp table(wjid int,qxdjlistvarchar(5000),id int identity(1,1))
declare @idqxmp table(qxdjint)
declare @qxmp table(qxdj int)
--create table #idqxmp (qxdj int)
insert into @qxmp(qxdj) select qxdj fromdbo.fn_yhdm_qxdj(@_yhdm)
insert into @xwmp(qxdjlist,wjid) selectqxdjlist,wjid from dbo.oa_xwwj_llqx
where xq=@_xq and lb=@_lb
set @i=1
select @is=count(*) from@xwmp
while (@i<=@is)
begin
set @qxdjlist=''
set @res=0
set @xwid=0
select@qxdjlist=qxdjlist,@xwid=wjid from @xwmp where id=@i
--truncatetable #idqxmp
delete from @idqxmp
insert into@idqxmp(qxdj) select chrfld fromdbo.fn_String_Trans_Field_int(@qxdjlist)
select@res=count(*) from @idqxmp idmp,@qxmp qxmp whereidmp.qxdj=qxmp.qxdj
if @res>0
insert into @tmp(xq,yhdm,xwid) values(@_xq,@_yhdm,@xwid)
set@i=@i+1
end
insert into @idmp(xq,yhdm,xwid) selectdistinct xq,yhdm,xwid from @tmp
return
END