bcp 导入/导出处理

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[ExportFile]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[ExportFile]
GO

CREATE PROCEDURE dbo.ExportFile
(
   
      @serverip    VARCHAR(10)
    , @user        VARCHAR(10)
    , @pass        VARCHAR(10)
    , @path        VARCHAR(100)
)
AS
BEGIN

    DECLARE @cmd varchar(1000)

    IF OBJECT_ID(N'tempdb..##IMPORT') IS NOT NULL
    BEGIN
        DROP TABLE ##IMPORT
    END

    IF OBJECT_ID(N'tempdb..##EXPORT') IS NOT NULL
    BEGIN
        DROP TABLE ##EXPORT
    END

    SELECT
          CASE WHEN amount < 0
               THEN
                   '-' +
                   REPLICATE ( '0' , 10 - LEN( CAST(amount * (-1) AS VARCHAR(11))) )  + CAST(amount * (-1) AS VARCHAR(11))
               ELSE
                   REPLICATE ( '0' , 11 - LEN( CAST(amount AS VARCHAR(11))) )  + CAST(amount AS VARCHAR(11))
          END AS amount
       
        , ISNULL(user, SPACE(6)) AS user
    INTO
        ##EXPORT
    FROM
        table1 WITH (NOLOCK)


    SELECT
        *
    INTO
        ##IMPORT
    FROM
        ##EXPORT
    WHERE
        1 = 0

    SET @cmd = 'bcp ##IMPORT in '+ @path + ' -S'+ @serverip + ' -U'+ @user + ' -P' + ISNULL(@pass, '') + ' -c -t/t -r/t/n'

    EXEC MASTER..XP_CMDSHELL @cmd

    SET @cmd = 'bcp "SELECT * FROM ##IMPORT UNION ALL SELECT * FROM  ##EXPORT" queryout '+ @path + ' -S'+ @serverip + ' -U'+ @user + ' -P' + ISNULL(@pass, '') + ' -c -t/t -r/t/n'

    EXEC MASTER..XP_CMDSHELL @cmd

   
    DROP TABLE ##IMPORT
    DROP TABLE ##EXPORT

END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值