如何使用xp_cmdshell

时间:2017-11-07 22:55:30

标签: sql-server tsql pdf sql-server-2012

我能够将单个PDF文件从SQL服务器复制到特定位置。 但是我怎么能用多个文件呢?

EXEC master..xp_cmdshell 'BCP "SELECT PDF FROM [MEJAMES].[dbo].tblPolicyForms WHERE FormID = 20 " queryout "C:\Users\UserName\Docs\Folder\'+@FileName+'.pdf" -T -N'

我有一张FormID,PDF,PDF_Filename

的表格
select  FormID,PDF,PDF_Filename
from    [dbo].[tblPolicyForms]

enter image description here

如何遍历每个FormID,提取PDF文件并将其命名为PDF_Filename.pdf?

更新:

出于某种原因,它给了我一个错误:

CREATE TABLE #tblPolicyForms
(
    FormID INT NOT NULL
    ,PDF varbinary(max) NULL
    ,PDF_FIlename VARCHAR(max)
)


INSERT INTO #tblPolicyForms
SELECT  FormID,
        PDF,
        PDF_FileName
FROM    [dbo].[tblPolicyForms]
WHERE   FormID IN (19,20,21,22)
--select * from #tblPolicyForms

DECLARE @FormID varchar(4);
DECLARE @FileName VARCHAR(200);

DECLARE FormID_cursor CURSOR FOR 
SELECT
    FormID
    ,PDF_FIlename
FROM
  #tblPolicyForms
WHERE FormID IN (19,20,21,22)
--where whatever conditions you need to get the FormIDs you want to use


OPEN FormID_cursor

FETCH NEXT FROM FormID_cursor
INTO @FormID, @FileName

WHILE @@FETCH_STATUS = 0
BEGIN

    EXEC master.dbo.xp_cmdshell 'BCP "SELECT PDF FROM [DBname].[dbo].tblPolicyForms " queryout "C:\Users\UserName\Documents\ExportDir\'+@FileName+'" -T -N'


FETCH NEXT FROM FormID_cursor
INTO @FormID, @FileName

END

CLOSE FormID_cursor
DEALLOCATE FormID_cursor

enter image description here

2 个答案:

答案 0 :(得分:0)

我进行了光标路径,因为这使您能够根据需要定义输出列表

CREATE TABLE #tblPolicyForms
(
    FormID INT NOT NULL
    ,PDF TEXT NULL
    ,PDF_FIlename VARCHAR(200)
)


INSERT INTO #tblPolicyForms
    VALUES(18, 'skjdfsakdjfhsldkfhsdlkjfhsdkfjh', 'PDF18Name.pdf')
    ,(19, 'skjdfsakdjfhsldkfhsdlkjfhsdkfjh', 'PDF19Name.pdf')
    ,(20, 'skjdfsakdjfhsldkfhsdlkjfhsdkfjh', 'PDF20Name.pdf')
    ,(21, 'skjdfsakdjfhsldkfhsdlkjfhsdkfjh', 'PDF21Name.pdf')
    ,(22, 'skjdfsakdjfhsldkfhsdlkjfhsdkfjh', 'PDF22Name.pdf')
    ,(23, 'skjdfsakdjfhsldkfhsdlkjfhsdkfjh', 'PDF23Name.pdf')
    ,(24, 'skjdfsakdjfhsldkfhsdlkjfhsdkfjh', 'PDF24Name.pdf')
    ,(25, 'skjdfsakdjfhsldkfhsdlkjfhsdkfjh', 'PDF25Name.pdf')
    ,(26, 'skjdfsakdjfhsldkfhsdlkjfhsdkfjh', 'PDF26Name.pdf')
    ,(27, 'skjdfsakdjfhsldkfhsdlkjfhsdkfjh', 'PDF27Name.pdf')


DECLARE @FormID varchar(4);
DECLARE @FileName VARCHAR(200);

DECLARE FormID_cursor CURSOR FOR 
SELECT
    FormID
    ,PDF_FIlename
FROM
    #tblPolicyForms
--where whatever conditions you need to get the FormIDs you want to use


OPEN FormID_cursor

FETCH NEXT FROM FormID_cursor
INTO @FormID, @FileName

WHILE @@FETCH_STATUS = 0
BEGIN

    EXEC master.dbo.xp_cmdshell 'BCP "SELECT PDF FROM [MEJAMES].[dbo].tblPolicyForms WHERE FormID = '+@FormID+' " queryout "C:\Users\UserName\Docs\Folder\'+@FileName+'.pdf" -T -N'
--  SELECT 'BCP "SELECT PDF FROM [MEJAMES].[dbo].tblPolicyForms WHERE FormID = '+@FormID+' " queryout "C:\Users\UserName\Docs\Folder\'+@FileName+'.pdf" -T -N'

FETCH NEXT FROM FormID_cursor
INTO @FormID, @FileName

END

CLOSE FormID_cursor
DEALLOCATE FormID_cursor

答案 1 :(得分:0)

DECLARE @FormID varchar(4);
DECLARE @FileName VARCHAR(200);
DECLARE @CmdString VARCHAR(2000);

DECLARE FormID_cursor CURSOR FOR 
SELECT
    FormID
    ,PDF_FIlename
FROM
    [dbo].[tblPolicyForms]
WHERE
    FormID IN (19,20,21,22)
--where whatever conditions you need to get the FormIDs you want to use


OPEN FormID_cursor

FETCH NEXT FROM FormID_cursor
INTO @FormID, @FileName

WHILE @@FETCH_STATUS = 0
BEGIN

    SET @CmdString = 'BCP "SELECT PDF FROM [dbo].[tblPolicyForms] where FormID = '+@FormID+' " queryout "C:\Users\UserName\Documents\ExportDir\'+@FileName+'" -T -N';
    EXEC master.dbo.xp_cmdshell @CmdString;

FETCH NEXT FROM FormID_cursor
INTO @FormID, @FileName

END

CLOSE FormID_cursor
DEALLOCATE FormID_cursor