- 论坛徽章:
- 0
|
写这个游标的初衷是为了读取各个服务器中的每个数据库的表,但出现了问题,希望熟悉游标使用的高手们不吝赐教,谢谢
declare @fwqid char(30),@cnt int --定义服务器标识
declare mdqk_cur cursor for --定义游标(来自于门店的情况表)
select fwqid from mdqkb
open mdqk_cur --打开游标
fetch mdqk_cur into @fwqid --获取第一行记录
while @@fetch_status=0 --状态标识
begin
if left(@fwqid,1)=\'a\' or len(@fwqid)>4 --判断是否为a服务器
begin
select rq,sum(xshje) as xsje into tba from A服务器.@fwqid.dbo.splsk
where zhy=\'零售日清\' and rq between convert(char(10),dateadd(dd,-3,getdate()),21) and convert(char(10),getdate(),21)
group by rq
order by rq desc
select @cnt=count(*) from tba
if @cnt=0
insert into sjqk
select * from mdqkb where fwqid=@fwqid
set @cnt=0
drop table tba
end
if left(@fwqid,1)=\'B\' --判断是否为b服务器
begin
select rq,sum(xshje) as xsje into tbb from B服务器.@fwqid.dbo.splsk
where zhy=\'零售日清\' and rq between convert(char(10),dateadd(dd,-3,getdate()),21) and convert(char(10),getdate(),21)
group by rq
order by rq desc
select @cnt=count(*) from tbb
if @cnt=0
insert into sjqk
select * from mdqkb where fwqid=@fwqid
set @cnt=0
drop table tbb
end
if left(@fwqid,1)=\'C\' --判断是否为c服务器
begin
select rq,sum(xshje) as xsje into tbc from C服务器.@fwqid.dbo.splsk
where zhy=\'零售日清\' and rq between convert(char(10),dateadd(dd,-3,getdate()),21) and convert(char(10),getdate(),21)
group by rq
order by rq desc
select @cnt=count(*) from tbc
if @cnt=0
insert into sjqk
select * from mdqkb where fwqid=@fwqid
set @cnt=0
drop table tbc
end
fetch next from mdqk_cur into @fwqid --获取下一条记录
end
close mdqk_cur --关闭游标
deallocate mdqk_cur --释放游标 |
|