SQL 服务器 BCP 命令
SQL Server BCP command
我正在组合一个 bcp
命令以在存储过程结束时输出一个文件。
如果我 运行 下面显示的代码然后 select 变量的内容并将 EXEC xp_cmdshell
附加到开头,它工作正常。
如果我 运行 它一直通过,即 EXEC xp_cmdshell @cmd
,我收到错误
'bcp' is not recognized as an internal or external command
我看不出我的代码哪里错了,请问有人能看出问题所在吗?
DECLARE @PeriodStamp VARCHAR(8000) = REPLACE(CAST(GETDATE() - 2 AS DATE), '-', '')
DECLARE @marketingoptinp1 VARCHAR(8000) = '''bcp "select ''''Created_DateTime'''', ''''Application_Date'''', ''''Application_ID'''', ''''Customer_ID'''', ''''First_Name'''', ''''Last_Name'''', ''''Email'''', ''''Mobile_Phone'''', ''''Home_Phone'''', ''''Gender'''', ''''DOB'''', ''''AGE'''', ''''Marital_Status_ID'''', ''''Marital_Status_Desc'''', ''''Dependent_Number'''', ''''Dependent_Number_Desc'''', ''''Employment_Industry_ID'''', ''''Employment_Industry_Desc'''', ''''Income_Source_ID'''', ''''Income_Source_Desc'''', ''''Loan_Amount'''', ''''Loan_Purpose_ID'''', ''''Loan_Purpose_Desc'''', ''''Residence_Type_ID'''', ''''Residence_Type_Desc'''', ''''Postcode'''', ''''Address1'''', ''''Address2'''', ''''City'''', ''''County'''', ''''Marketing_Opt_In'''', ''''IP_Address'''', ''''Bank_Sort_Code'''', ''''Device'''', ''''Lender_Result'''', ''''Rental_Mortgage_Payments'''', ''''Has_Pension'''', ''''PeriodStamp'''' union all SELECT Created_DateTime, cast(Application_Date as nvarchar(10)) as Application_Date, cast(Application_ID as nvarchar(15)) as Application_ID, cast(Customer_ID as nvarchar(15)) as Customer_ID , cast(First_Name as varchar(150)) as First_Name , cast(Last_Name as varchar(150)) as Last_Name , cast(Email as varchar(250)) as Email , quintegrity.[dbo].[udf_AlphaNumericElement](Mobile_Phone,4) as Mobile_Phone , quintegrity.[dbo].[udf_AlphaNumericElement](Home_Phone,4) as Home_Phone , Gender, cast(DOB as nvarchar(15)) as DOB, cast(AGE as nvarchar(3)) as AGE, cast(Marital_Status_ID as nvarchar(2)) as Marital_Status_ID, Marital_Status_Desc, cast(Dependent_Number as nvarchar(20)) as Dependent_Number, Dependent_Number_Desc, cast(Employment_Industry_ID as nvarchar(10)) as Employment_Industry_ID, Employment_Industry_Desc, cast(Income_Source_ID as nvarchar(10)), Income_Source_Desc, cast(Loan_Amount as nvarchar(20)) as Loan_Amount, cast(Loan_Purpose_ID as nvarchar(6)) as Loan_Purpose_ID, Loan_Purpose_Desc, cast(Residence_Type_ID as nvarchar(6)) as Residence_Type_ID, Residence_Type_Desc , cast(Postcode as varchar(150)) Postcode , cast(Address1 as varchar(150)) Address1 , cast(Address2 as varchar(150)) Address2 , cast(City as varchar(150)) City , cast(County as varchar(150)) County , cast(Marketing_Opt_In as nvarchar(2)) as Marketing_Opt_In, cast(IP_Address as nvarchar(20)) as IP_Address, cast(Bank_Sort_Code as nvarchar(10)) as Bank_Sort_Code, Device, Lender_Result, cast(Rental_Mortgage_Payments as nvarchar(15)) as Rental_Mortgage_Payments, Has_Pension, PeriodStamp FROM scratch.dbo.data_extract where Marketing_Opt_In = 1 and PeriodStamp = ' + @PeriodStamp
DECLARE @marketingoptinp2 VARCHAR(8000)
SET @marketingoptinp2 = ' " queryout "C:\test\data' + @PeriodStamp+'.txt" -T -c'''
DECLARE @cmd VARCHAR(8000)
SET @cmd = @marketingoptinp1 + @marketingoptinp2
EXEC xp_cmdshell @cmd
通过变量传递的字符串不需要像您那样用双引号引起来,
应该是这样的:
DECLARE @marketingoptinp1 VARCHAR(8000) = 'bcp "select Created_DateTime, Application_Date, Application_ID, Customer_ID, First_Name, Last_Name, Email, Mobile_Phone, Home_Phone, Gender, DOB, AGE, Marital_Status_ID, Marital_Status_Desc, Dependent_Number, Dependent_Number_Desc, Employment_Industry_ID, Employment_Industry_Desc, Income_Source_ID, Income_Source_Desc, Loan_Amount, Loan_Purpose_ID, Loan_Purpose_Desc, Residence_Type_ID, Residence_Type_Desc, Postcode, Address1, Address2, City, County, Marketing_Opt_In, IP_Address, Bank_Sort_Code, Device, Lender_Result, Rental_Mortgage_Payments, Has_Pension, PeriodStamp union all SELECT Created_DateTime, cast(Application_Date as nvarchar(10)) as Application_Date, cast(Application_ID as nvarchar(15)) as Application_ID, cast(Customer_ID as nvarchar(15)) as Customer_ID , cast(First_Name as varchar(150)) as First_Name , cast(Last_Name as varchar(150)) as Last_Name , cast(Email as varchar(250)) as Email , quintegrity.[dbo].[udf_AlphaNumericElement](Mobile_Phone,4) as Mobile_Phone , quintegrity.[dbo].[udf_AlphaNumericElement](Home_Phone,4) as Home_Phone , Gender, cast(DOB as nvarchar(15)) as DOB, cast(AGE as nvarchar(3)) as AGE, cast(Marital_Status_ID as nvarchar(2)) as Marital_Status_ID, Marital_Status_Desc, cast(Dependent_Number as nvarchar(20)) as Dependent_Number, Dependent_Number_Desc, cast(Employment_Industry_ID as nvarchar(10)) as Employment_Industry_ID, Employment_Industry_Desc, cast(Income_Source_ID as nvarchar(10)), Income_Source_Desc, cast(Loan_Amount as nvarchar(20)) as Loan_Amount, cast(Loan_Purpose_ID as nvarchar(6)) as Loan_Purpose_ID, Loan_Purpose_Desc, cast(Residence_Type_ID as nvarchar(6)) as Residence_Type_ID, Residence_Type_Desc , cast(Postcode as varchar(150)) Postcode , cast(Address1 as varchar(150)) Address1 , cast(Address2 as varchar(150)) Address2 , cast(City as varchar(150)) City , cast(County as varchar(150)) County , cast(Marketing_Opt_In as nvarchar(2)) as Marketing_Opt_In, cast(IP_Address as nvarchar(20)) as IP_Address, cast(Bank_Sort_Code as nvarchar(10)) as Bank_Sort_Code, Device, Lender_Result, cast(Rental_Mortgage_Payments as nvarchar(15)) as Rental_Mortgage_Payments, Has_Pension, PeriodStamp FROM scratch.dbo.data_extract where Marketing_Opt_In = 1 and PeriodStamp = ' + @PeriodStamp
你的第二个变量也是一样,你应该删除两个结束引号:
DECLARE @marketingoptinp2 VARCHAR(8000)
SET @marketingoptinp2 = ' " queryout "C:\test\data' + @PeriodStamp+'.txt" -T -c'
我正在组合一个 bcp
命令以在存储过程结束时输出一个文件。
如果我 运行 下面显示的代码然后 select 变量的内容并将 EXEC xp_cmdshell
附加到开头,它工作正常。
如果我 运行 它一直通过,即 EXEC xp_cmdshell @cmd
,我收到错误
'bcp' is not recognized as an internal or external command
我看不出我的代码哪里错了,请问有人能看出问题所在吗?
DECLARE @PeriodStamp VARCHAR(8000) = REPLACE(CAST(GETDATE() - 2 AS DATE), '-', '')
DECLARE @marketingoptinp1 VARCHAR(8000) = '''bcp "select ''''Created_DateTime'''', ''''Application_Date'''', ''''Application_ID'''', ''''Customer_ID'''', ''''First_Name'''', ''''Last_Name'''', ''''Email'''', ''''Mobile_Phone'''', ''''Home_Phone'''', ''''Gender'''', ''''DOB'''', ''''AGE'''', ''''Marital_Status_ID'''', ''''Marital_Status_Desc'''', ''''Dependent_Number'''', ''''Dependent_Number_Desc'''', ''''Employment_Industry_ID'''', ''''Employment_Industry_Desc'''', ''''Income_Source_ID'''', ''''Income_Source_Desc'''', ''''Loan_Amount'''', ''''Loan_Purpose_ID'''', ''''Loan_Purpose_Desc'''', ''''Residence_Type_ID'''', ''''Residence_Type_Desc'''', ''''Postcode'''', ''''Address1'''', ''''Address2'''', ''''City'''', ''''County'''', ''''Marketing_Opt_In'''', ''''IP_Address'''', ''''Bank_Sort_Code'''', ''''Device'''', ''''Lender_Result'''', ''''Rental_Mortgage_Payments'''', ''''Has_Pension'''', ''''PeriodStamp'''' union all SELECT Created_DateTime, cast(Application_Date as nvarchar(10)) as Application_Date, cast(Application_ID as nvarchar(15)) as Application_ID, cast(Customer_ID as nvarchar(15)) as Customer_ID , cast(First_Name as varchar(150)) as First_Name , cast(Last_Name as varchar(150)) as Last_Name , cast(Email as varchar(250)) as Email , quintegrity.[dbo].[udf_AlphaNumericElement](Mobile_Phone,4) as Mobile_Phone , quintegrity.[dbo].[udf_AlphaNumericElement](Home_Phone,4) as Home_Phone , Gender, cast(DOB as nvarchar(15)) as DOB, cast(AGE as nvarchar(3)) as AGE, cast(Marital_Status_ID as nvarchar(2)) as Marital_Status_ID, Marital_Status_Desc, cast(Dependent_Number as nvarchar(20)) as Dependent_Number, Dependent_Number_Desc, cast(Employment_Industry_ID as nvarchar(10)) as Employment_Industry_ID, Employment_Industry_Desc, cast(Income_Source_ID as nvarchar(10)), Income_Source_Desc, cast(Loan_Amount as nvarchar(20)) as Loan_Amount, cast(Loan_Purpose_ID as nvarchar(6)) as Loan_Purpose_ID, Loan_Purpose_Desc, cast(Residence_Type_ID as nvarchar(6)) as Residence_Type_ID, Residence_Type_Desc , cast(Postcode as varchar(150)) Postcode , cast(Address1 as varchar(150)) Address1 , cast(Address2 as varchar(150)) Address2 , cast(City as varchar(150)) City , cast(County as varchar(150)) County , cast(Marketing_Opt_In as nvarchar(2)) as Marketing_Opt_In, cast(IP_Address as nvarchar(20)) as IP_Address, cast(Bank_Sort_Code as nvarchar(10)) as Bank_Sort_Code, Device, Lender_Result, cast(Rental_Mortgage_Payments as nvarchar(15)) as Rental_Mortgage_Payments, Has_Pension, PeriodStamp FROM scratch.dbo.data_extract where Marketing_Opt_In = 1 and PeriodStamp = ' + @PeriodStamp
DECLARE @marketingoptinp2 VARCHAR(8000)
SET @marketingoptinp2 = ' " queryout "C:\test\data' + @PeriodStamp+'.txt" -T -c'''
DECLARE @cmd VARCHAR(8000)
SET @cmd = @marketingoptinp1 + @marketingoptinp2
EXEC xp_cmdshell @cmd
通过变量传递的字符串不需要像您那样用双引号引起来, 应该是这样的:
DECLARE @marketingoptinp1 VARCHAR(8000) = 'bcp "select Created_DateTime, Application_Date, Application_ID, Customer_ID, First_Name, Last_Name, Email, Mobile_Phone, Home_Phone, Gender, DOB, AGE, Marital_Status_ID, Marital_Status_Desc, Dependent_Number, Dependent_Number_Desc, Employment_Industry_ID, Employment_Industry_Desc, Income_Source_ID, Income_Source_Desc, Loan_Amount, Loan_Purpose_ID, Loan_Purpose_Desc, Residence_Type_ID, Residence_Type_Desc, Postcode, Address1, Address2, City, County, Marketing_Opt_In, IP_Address, Bank_Sort_Code, Device, Lender_Result, Rental_Mortgage_Payments, Has_Pension, PeriodStamp union all SELECT Created_DateTime, cast(Application_Date as nvarchar(10)) as Application_Date, cast(Application_ID as nvarchar(15)) as Application_ID, cast(Customer_ID as nvarchar(15)) as Customer_ID , cast(First_Name as varchar(150)) as First_Name , cast(Last_Name as varchar(150)) as Last_Name , cast(Email as varchar(250)) as Email , quintegrity.[dbo].[udf_AlphaNumericElement](Mobile_Phone,4) as Mobile_Phone , quintegrity.[dbo].[udf_AlphaNumericElement](Home_Phone,4) as Home_Phone , Gender, cast(DOB as nvarchar(15)) as DOB, cast(AGE as nvarchar(3)) as AGE, cast(Marital_Status_ID as nvarchar(2)) as Marital_Status_ID, Marital_Status_Desc, cast(Dependent_Number as nvarchar(20)) as Dependent_Number, Dependent_Number_Desc, cast(Employment_Industry_ID as nvarchar(10)) as Employment_Industry_ID, Employment_Industry_Desc, cast(Income_Source_ID as nvarchar(10)), Income_Source_Desc, cast(Loan_Amount as nvarchar(20)) as Loan_Amount, cast(Loan_Purpose_ID as nvarchar(6)) as Loan_Purpose_ID, Loan_Purpose_Desc, cast(Residence_Type_ID as nvarchar(6)) as Residence_Type_ID, Residence_Type_Desc , cast(Postcode as varchar(150)) Postcode , cast(Address1 as varchar(150)) Address1 , cast(Address2 as varchar(150)) Address2 , cast(City as varchar(150)) City , cast(County as varchar(150)) County , cast(Marketing_Opt_In as nvarchar(2)) as Marketing_Opt_In, cast(IP_Address as nvarchar(20)) as IP_Address, cast(Bank_Sort_Code as nvarchar(10)) as Bank_Sort_Code, Device, Lender_Result, cast(Rental_Mortgage_Payments as nvarchar(15)) as Rental_Mortgage_Payments, Has_Pension, PeriodStamp FROM scratch.dbo.data_extract where Marketing_Opt_In = 1 and PeriodStamp = ' + @PeriodStamp
你的第二个变量也是一样,你应该删除两个结束引号:
DECLARE @marketingoptinp2 VARCHAR(8000)
SET @marketingoptinp2 = ' " queryout "C:\test\data' + @PeriodStamp+'.txt" -T -c'