22,298
社区成员
发帖
与我相关
我的任务
分享-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GOEXEC master..xp_cmdshell 'bcp "SELECT e.zipcode ,
f.name_short ,
CONVERT(CHAR(6), a.time_conf, 112) AS ymth ,
c.classid ,
c.name_class ,
COUNT(DISTINCT a.custid) AS 客户数 ,
COUNT(DISTINCT b.shopid) AS SKU数 ,
SUM(b.value_ok) AS [成交额] ,
SUM(b.num_in / d.num_box) AS [成交件数]
FROM t_bp_wsalebrk_h a WITH ( NOLOCK ) ,
t_bp_wsalebrk_d b WITH ( NOLOCK ) ,
t_bp_class c WITH ( NOLOCK ) ,
t_bp_shop d WITH ( NOLOCK ) ,
t_bp_cust e WITH ( NOLOCK ) ,
t_bp_wsalewrkgroup f WITH ( NOLOCK )
WHERE a.bill = b.bill
AND b.shopid = d.shopid
AND e.zipcode = f.groupid
AND LEFT(d.classid, LEN(c.classid)) = c.classid
AND a.flag_conf = ''1''
AND a.custid = e.custid
AND a.time_conf >= ''2010-01-01''
AND a.time_conf < ''当天日期'' --修改起止时间
AND c.classid LIKE ''0%''
AND c.remark LIKE ''%基本%''
GROUP BY e.zipcode ,
f.name_short ,
CONVERT(CHAR(6), a.time_conf, 112) ,
c.classid ,
c.name_class
ORDER BY e.zipcode ,
c.classid;" queryout "c:\test.xls" /c -/S"服务器名" /U"用户名" -P"密码"'
参照以上语法,改服务器和用户密码USE SampleDB
GO
--第1步在SampleDB数据库建存储过程P1
CREATE PROC P1
AS
DECLARE @Dt DATE= GETDATE();
--修改起止时间,取日期
SELECT e.zipcode ,
f.name_short ,
CONVERT(CHAR(6), a.time_conf, 112) AS ymth ,
c.classid ,
c.name_class ,
COUNT(DISTINCT a.custid) AS 客户数 ,
COUNT(DISTINCT b.shopid) AS SKU数 ,
SUM(b.value_ok) AS [成交额] ,
SUM(b.num_in / d.num_box) AS [成交件数]
FROM t_bp_wsalebrk_h a WITH ( NOLOCK ) ,
t_bp_wsalebrk_d b WITH ( NOLOCK ) ,
t_bp_class c WITH ( NOLOCK ) ,
t_bp_shop d WITH ( NOLOCK ) ,
t_bp_cust e WITH ( NOLOCK ) ,
t_bp_wsalewrkgroup f WITH ( NOLOCK )
WHERE a.bill = b.bill
AND b.shopid = d.shopid
AND e.zipcode = f.groupid
AND LEFT(d.classid, LEN(c.classid)) = c.classid
AND a.flag_conf = '1'
AND a.custid = e.custid
AND a.time_conf >= '2010-01-01'
AND a.time_conf < @Dt
AND c.classid LIKE '0%'
AND c.remark LIKE '%基本%'
GROUP BY e.zipcode ,
f.name_short ,
CONVERT(CHAR(6), a.time_conf, 112) ,
c.classid ,
c.name_class
ORDER BY e.zipcode ,
c.classid
GO
--第二步(在作业里调度) 执行导出文件 D:\每个品牌每个区域.XLS
EXEC master..xp_cmdshell 'bcp "EXEC SampleDB.dbo.P1" queryout "D:\每个品牌每个区域.XLS" -w -S"." -U"sa" -P""'
EXEC master..xp_cmdshell 'bcp "EXEC SampleDB.dbo.P1" queryout "c:\test.xls" -w -S"PCRoy" -U"sa" -P""'