存储过程,和导出的引用。。。。。。。请指教 100分呀

yuqianpingnihao 2010-05-26 12:29:46
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

create PROCEDURE [dbo].[outjbjnew] as
--create by huawei 20090705,导出搅拌楼误差率大于5%的原材消耗
declare @datatime varchar(20)
declare @datatimemin varchar(20)
declare @sql varchar(1000)
declare @server varchar(20)
declare @jbj varchar(20)
declare @t int
declare @cmd varchar(1000)
declare @path varchar(100)

set @t=0
---set @server='jbl1new.data.dbo.'

select @datatime=convert(varchar(10),getdate(),120 )+' 05:59:59'
print @datatime
select @datatimemin=convert(varchar(10),dateadd(dd,-1,getdate()),120)+' 06:00:00'

print @datatimemin

while @t<=10 --开始将数据导出到表data
begin
select @server=(
case
when @t=1 then'[jbl1new].data.dbo.'
when @t=2 then'[jbl2new].data.dbo.'
when @t=3 then'[jbl3new].data.dbo.'
when @t=4 then '[jblF].data.dbo.'
when @t=5 then '[jbl3].data.dbo.'
when @t=6 then '[jbl4].cb4500.dbo.'
when @t=7 then '[sany8].cb4500.dbo.'
when @t=8 then '[sany9].cb4500.dbo.'
when @t=9 then'[jbl-c].data.dbo.'
when @t=10 then'[jbl-d].data.dbo.'
end
),
@jbj=(
case
when @t=1 then'五乡A号机'
when @t=2 then'五乡B号机'
when @t=3 then'五乡E号机'
when @t=4 then '江北F号机'
when @t=5 then '江北3号机'
when @t=6 then '江北4号机'
when @t=7 then '姜山5号机'
when @t=8 then '姜山6号机'
when @t=9 then '北仑C号机'
when @t=10 then '北仑D号机'


end
)
begin try --捕捉错误,防止关机而影响后续执行
if (@t<=5 OR @t=9 or @t=10)
begin
select @sql='
insert into dbo.data(jbj,jobno,name,mset,mact,batchtime,pr)
select '''+@jbj+''',a.jobno,b.name,b.mset,b.mact,b.batchtime,b.百分比 from '+@server+'ProdMasterData a , (
select prodno,name ,mset,mact,batchtime,cast((batchadd/mset)*100 as numeric(20,4)) as ''百分比'' from
'+@server+'ConsumpMaterial where BatchTime>='''+@datatimemin+''' and BatchTime<'''+ @datatime+'''
and mset>0
and ((batchadd/mset)*100>=5 or (batchadd/mset)*100<=-5)
) b
where a.prodno=b.prodno order by b.name,百分比
'
exec(@sql)
print @sql

end
if (@t>5 and @t<=10)
begin
set @sql=''
select @sql='
insert into dbo.data(jbj,jobno,name,mset,mact,batchtime,pr)
select '''+@jbj+''',a.jobno,b.name,b.MDesign,b.mact,convert(varchar(20),b.batchtime,120),b.pr from '+@server+'ProdMasterData
a , (
select prodno,name ,MDesign,mact,batchtime,cast(((mact-MDesign)/MDesign)*100 as numeric(20,4)) as ''pr'' from
'+@server+'ConsumpMaterial where BatchTime>='''+@datatimemin+''' and BatchTime<'''+ @datatime+'''
and MDesign>0
and (((mact-MDesign)/MDesign)*100 >=5 or ((mact-MDesign)/MDesign)*100 <=-5)
) b
where a.prodno=b.prodno order by b.name,pr
'
exec(@sql)

end
end try
begin catch
set @t=@t+1 --跳过关机的服务器继续执行
continue
end catch

--开始导出data表数据到文件
select
@path=(
case
when @t<=3 then ' where jbj like''%五乡%'' or id=1 " queryout E:\搅拌楼误差率报表\五乡\'
when (@t>3 and @t<=6) then ' where jbj like''%江北%'' or id=1 " queryout E:\搅拌楼误差率报表\江北\'
when (@t>6 and @t<=8) then ' where jbj like''%姜山%'' or id=1 " queryout E:\搅拌楼误差率报表\姜山\'
when (@t>=9 ) then ' where jbj like''%北仑%'' or id=1 " queryout E:\搅拌楼误差率报表\北仑\'

end
)

--select @datatime=cast(datepart(year,@datatime) as varchar(4))+cast(datepart(month,@datatime) as varchar(2))+cast(datepart
--(day,@datatime) as varchar(2))


select @cmd='bcp '+'"select jbj,jobno,name,MSet,MAct,pr,batchtime from data.dbo.data '+@path+convert(varchar
(10),@datatime,120)+'原材误差率±5%.xls -c -S "'+'.'+'"'+' -U"sa" -P"tuodehero"'

EXEC master..xp_cmdshell @cmd
--print @cmd

set @t=@t+1
end


---select @cmd='bcp '+'"select jbj,jobno,name,MSet,MAct,pr,batchtime from data.dbo.data where id>1 and (cast(pr as numeric
---(20,4))>=20 or cast(pr as numeric(20,4))<=-20 ) " queryout F:\搅拌楼误差率报表\'+convert(varchar(10),@datatime,120)+'原材误
---差率±20%.xls -c -S "'+'127.0.0.1'+'"'+' -U"sa" -P"88328088"'
--EXEC master..xp_cmdshell @cmd

---delete data where id>1'




这两个是新加的。、、、、









outjbjnew
...全文
89 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuqianpingnihao 2010-05-27
  • 打赏
  • 举报
回复
防火墙都关了。。。还要开端口???这台式新安装的SQL。。补充。我用windows验证登陆主服务器可以链接到jbl3new服务器的????
永生天地 2010-05-27
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 yuqianpingnihao 的回复:]
单独使用5楼的查询,结果出现这个错误

链接服务器"jbl3new"的 OLE DB 访问接口 "SQLNCLI" 返回了消息 "通讯链接失败"。
消息 109,级别 16,状态 1,第 0 行
命名管道提供程序: 管道已结束。
消息 18452,级别 14,状态 1,第 0 行
用户 'sa' 登录失败。原因: 未与信任 SQL Server 连接相关联。
不知道是什么原因呀
[/Quote]


链接服务器"jbl3new"的机器的安全设置很高,检查那台机器的防火墙是不是关闭了什么端口
yuqianpingnihao 2010-05-26
  • 打赏
  • 举报
回复
when @t=3 then'五乡E号机'
when @t=4 then '江北F号机'

这两个是新增加的,但是导不出来,data这个表里面没有这两个数据?????
FlySQL 2010-05-26
  • 打赏
  • 举报
回复
问题是什么?
yuqianpingnihao 2010-05-26
  • 打赏
  • 举报
回复
BEGIN TRY

end try
begin catch
set @t=@t+1 --跳过关机的服务器继续执行
continue

这个是2005的功能,2000的如何实现这个功能呀,,。。。。。谢谢啦
yuqianpingnihao 2010-05-26
  • 打赏
  • 举报
回复
我这个跟你的不太一样??????????
feixianxxx 2010-05-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 yuqianpingnihao 的回复:]

单独使用5楼的查询,结果出现这个错误

链接服务器"jbl3new"的 OLE DB 访问接口 "SQLNCLI" 返回了消息 "通讯链接失败"。
消息 109,级别 16,状态 1,第 0 行
命名管道提供程序: 管道已结束。
消息 18452,级别 14,状态 1,第 0 行
用户 'sa' 登录失败。原因: 未与信任 SQL Server 连接相关联。
不知道是什么原因呀……
[/Quote]
http://blog.csdn.net/feixianxxx/archive/2010/04/24/5523922.aspx
yuqianpingnihao 2010-05-26
  • 打赏
  • 举报
回复
单独使用5楼的查询,结果出现这个错误

链接服务器"jbl3new"的 OLE DB 访问接口 "SQLNCLI" 返回了消息 "通讯链接失败"。
消息 109,级别 16,状态 1,第 0 行
命名管道提供程序: 管道已结束。
消息 18452,级别 14,状态 1,第 0 行
用户 'sa' 登录失败。原因: 未与信任 SQL Server 连接相关联。
不知道是什么原因呀
nalnait 2010-05-26
  • 打赏
  • 举报
回复
那是你连接有问题 看权限
yuqianpingnihao 2010-05-26
  • 打赏
  • 举报
回复
用户 'sa' 登录失败。原因: 未与信任 SQL Server 连接相关联
永生天地 2010-05-26
  • 打赏
  • 举报
回复
你把这个查询单独在查询分析器里执行,看看有没有数据
insert into dbo.data(jbj,jobno,name,mset,mact,batchtime,pr)
select '五乡E号机',a.jobno,b.name,b.mset,b.mact,b.batchtime,b.百分比
from [jbl3new].data.dbo.ProdMasterData a ,
(
select prodno,name ,mset,mact,batchtime,cast((batchadd/mset)*100 as numeric(20,4)) as '百分比' from
[jbl3new].data.dbo.ConsumpMaterial where BatchTime>='2010-05-25 06:00:00' and BatchTime<'2010-05-26 05:59:59'
and mset>0 and ((batchadd/mset)*100>=5 or (batchadd/mset)*100<=-5)
) b
where a.prodno=b.prodno order by b.name,百分比
yuqianpingnihao 2010-05-26
  • 打赏
  • 举报
回复
明白我的意思 没呀 。。。。就是说还有when @t=3 then'五乡E号机'
when @t=4 then '江北F号机'取不到数据。。data这个表取不到ConsumpMaterial这个表的数据

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧