我这个存储过程是对要导入系统的数据做相应的验证, 如果本次导入的数据全部验证通过,那么就将数据导入到系统里,如果有其中一条验证不通过的话,就都不导,返回对应的提示. 我现在是用游标写的,功能是实现了,但由于是要夸服务器验证,目前我写的这个语句效率不高,比较耗时间。请看语句,请问是否可以怎么优化呢? SQL code
--shelf数据验证并导入
CREATE PROCEDURE [dbo].[sp_ShelfCheckandinput]
@returnvalue nvarchar(200) OUTPUT,
@GUID nvarchar(50)
as
declare @id int,@JobNo nvarchar(50),@ProductNo nvarchar(100),@inputtype int,@lotno nvarchar(50)
declare curJob cursor for select id,JobNo,ProductNo,InputType,lotno from shelf_trans_manage where Guid=@GUID order by id
open curJob
fetch next from CurJob into @id,@JobNo,@ProductNo,@inputtype,@lotno
while @@fetch_status=0
begin
if @inputtype=1 --情况1
begin
if(select count(*) from NeLink.nl_cls_app.dbo.job job where job+'-'+ right('0000'+ rtrim(suffix),4)=@JobNo collate chinese_prc_ci_as and item=@ProductNo collate chinese_prc_ci_as)>0
BEGIN
if (left(ltrim(@ProductNo),2) ='68' or left(ltrim(@ProductNo),2) ='99' or left(ltrim(@ProductNo),2) ='95')
begin
if (
((select sum(qty) from shelf_trans_manage where jobno=@JobNo and guid=@GUID) +(select sum(qty) from shelf_Data_manage where jobno=@JobNo))
>
(select sum(qty_released)*1.1 from NeLink.nl_cls_app.dbo.job where job+'-'+ right('0000'+ rtrim(suffix),4)=@JobNo collate chinese_prc_ci_as and type='J' collate chinese_prc_ci_as)
)
begin
set @returnvalue = @JobNo+'数量超出,数据导入失败!'
return
end
end
else
begin
if (
((select sum(qty) from shelf_trans_manage where jobno=@JobNo and guid=@GUID) +(select sum(qty) from shelf_Data_manage where jobno=@JobNo))
>
(select sum(qty_released) from NeLink.nl_cls_app.dbo.job where job+'-'+ right('0000'+ rtrim(suffix),4)=@JobNo collate chinese_prc_ci_as and type='J' collate chinese_prc_ci_as)
)
begin
set @returnvalue = @JobNo+'数量超出,数据导入失败!'
return
end
end
END
else
begin
set @returnvalue = @JobNo+'作业单不存在,数据导入失败!'
return
end
end
if @inputtype=2 --情况2
begin
if(
select count(*) from NeLink.nl_cls_app.dbo.matltran b where item=@ProductNo collate chinese_prc_ci_as and lot=@lotno collate chinese_prc_ci_as and trans_type='F' collate chinese_prc_ci_as
`)<1
begin
set @returnvalue = @ProductNo+','+@lotno+'系统里不存在,数据导入失败!'
return
end
end
fetch next from curJob into @id,@JobNo,@ProductNo,@inputtype,@lotno
end
close curJob
deallocate curJob
--验证通过,将数据导入系统
insert into shelf_Data_manage (shelfno,jobno,productno,productdesc,whse,loc,lotno,qty,inputdate,creater,inputType,GUID)
select shelfno,jobno,productno,productdesc,whse,loc,lotno,qty,inputdate,creater,inputType,GUID from shelf_trans_manage where Guid=@GUID
set @returnvalue ='验证通过,数据已成功导入!'
GO
这个问题第1个回答:
将每次执行的数据量减少,这样可以减少系统等待、系统回滚的空间要求。
这个问题第2个回答:
怎么都没人帮忙看看啊. 大侠们都还不知道CSDN可以上了 是吗?
这个问题第3个回答:
的确有问题,晃眼一看 SQL code
if(select count(*) from NeLink.nl_cls_app.dbo.job job where job+'-'+ right('0000'+ rtrim(suffix),4)=@JobNo collate chinese_prc_ci_as and item=@ProductNo collate chinese_prc_ci_as)>0
--这样的语句是很低下的,你应该改成下面这样
if exists(select * from ......)
这个问题第4个回答:
up
这个问题第5个回答:
主要问题在于夸服务器验证,你可以在你sp的机上做个数据同步,然后就不需要远程了。这样能快很多很多。
这个问题第6个回答:
无人问津。。。。
这个问题第7个回答:
游标执行,又要跨服务器是比较耗时间的..
这个问题第8个回答:
游标不是好东东.尽量想办法替换掉.
|
|
|