如何修复存储过程以将文件发送到文件夹

时间:2019-03-29 17:14:18

标签: sql sql-server

我对存储过程非常陌生。我编写了一个可以产生良好数据的数据,并且希望将这些数据保存在报告中,该报告将放入我的I盘上的文件夹中。但是,没有报告生成到该文件夹​​中。下面是我的代码。感谢您的帮助。

USE [ULTIPRO_ACE]
GO
/****** Object:  StoredProcedure [dbo].[Dependents_Age_26]    Script Date: 3/29/2019 9:54:26 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Dependents_Age_26]
AS

if object_id('tmp_UpdateCoverageDate') is not null
drop table dbo.tmp_UpdateCoverageDate; 

create table tmp_UpdateCoverageDate  

(eecempno VARCHAR(MAX), eeceeid VARCHAR(MAX), eepnamefirst VARCHAR(MAX), eepnamelast VARCHAR(MAX), conssn VARCHAR(MAX),
connamefirst VARCHAR(MAX), connamelast VARCHAR(MAX), condateofbirth VARCHAR(MAX), condateofbirth2 VARCHAR(MAX), dbndedcode VARCHAR(MAX),    
dbnbenstatus VARCHAR(MAX), dbndeprecid VARCHAR(MAX), consystemid VARCHAR(MAX))

INSERT into tmp_UpdateCoverageDate
select distinct eecempno, eeceeid, eepnamefirst as [EE First Name], eepnamelast as [EE Last Name], replicate('0',9-len(conssn))+conssn as conssn, connamefirst as [Dependent First Name], connamelast as [Dependent Last Name], condateofbirth as DEPDOB, (condateofbirth+9496) as benstpdate, dbndedcode as DEDCODE, dbnbenstatus as [Dep Ben Status], dbndeprecid, consystemid 

from empcomp, contacts, depbplan, emppers 
where eepeeid=coneeid and coneeid=dbneeid and eeceeid=eepeeid and consystemid= dbndeprecid and eecemplstatus in ('A', 'L', 'S') 
and conisdependent = 'y' and ConRelationship='CHL' 
and dbnbenstatus ='a' and datediff(yy, condateofbirth, getdate()) between 25.9 and 26 and (condateofbirth+9490) 
between getdate() and getdate() +7 and dbnbenstopdate is null

select * from tmp_UpdateCoverageDate
------------------------------------------------------------------------------------------------------------------------------------------------------------
declare @FileName varchar(100)
    ,@ExportPath varchar(100)
    ,@sCmd varchar(2000)
    ,@v_Servername varchar (32)
    ,@dbname varchar (30)
    ,@Result int

SELECT @v_ServerName = '"' + rtrim(@@SERVERNAME) + '"'
     , @dbname = rtrim(db_name())
     , @ExportPath = 'tmp_UpdateCoverageDate', 'ExportPath'
     , @FileName = convert(char(8),getdate(),112)+'tmp_UpdateCoverageDate' +'.txt'

    SELECT @sCmd = 'bcp ' + @dbName + '.' + ' out "' +
           @ExportPath + @FileName + '" -o"' + @ExportPath + 'ExportLog.txt"' +
           ' -T -S"' + @v_ServerName + '" -c -t'

    PRINT @sCmd

    EXEC @Result = master.dbo.xp_cmdshell @sCmd, NO_OUTPUT

0 个答案:

没有答案