选择时工作但在存储过程中写入时不起作用

时间:2018-01-31 08:46:57

标签: sql-server stored-procedures

我使用这些命令但它只在我正常选择

时才有效
SELECT INSTITUTE as INSTITUTE,
custno+ REPLICATE(' ', 16 - len(custno)) AS CUSTNO,
REPLICATE(' ', 32 - len(LASTNAME))+LASTNAME as LASTNAME
from TB_CUSTOMERS_DAILY

但是当在bcp实用程序的存储过程中使用时,它不起作用

USE [test]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE        [dbo].[DO_TRANSACTION_AML]

AS
Declare   @V_SQL_TMP     VARCHAR (4000)
EXEC DO_LOG @MSG_IN = 'START DO_TRANSACTION_AML'

SET @V_SQL_TMP = 'bcp "select CUST_INSTITUTE,CUST_CUSTNO + REPLICATE (' ', 
16 - len(CUST_CUSTNO)) from test.dbo.TB_TRANSACTIONS_AML" queryout 
"C:\Users\nvhun\test\Transaction_AML.txt" -c -UTF8 -T -Slocalhost'  
EXEC xp_cmdshell @V_SQL_TMP

1 个答案:

答案 0 :(得分:1)

您应该将字符串变量中的引号加倍:

SET @V_SQL_TMP = 'bcp "select CUST_INSTITUTE,CUST_CUSTNO + REPLICATE ('' '', 
16 - len(CUST_CUSTNO)) from test.dbo.TB_TRANSACTIONS_AML" queryout 
"C:\Users\nvhun\test\Transaction_AML.txt" -c -UTF8 -T -Slocalhost'

(见REPLICATE附近)