我在SQL Server 2008中有一个构建xml的存储过程。我需要将此xml写入同一服务器中的文件夹。我使用的用户标识具有执行权限。但是,当我运行此存储过程时,没有抛出任何错误,但它不会将文件写入该文件夹。
我出错的任何想法?
ALTER PROCEDURE [dbo].[kc_comp_data_sp]
AS
DECLARE
@company_xmlpartO nvarchar(MAX),
@company_xmlpart nvarchar(MAX),
@company_xml nvarchar(MAX),
@xml_beg nvarchar(200),
@xmlEnd nvarchar(100),
@xml XML,
@temp NVARCHAR(255),
@CompName VARCHAR(255),
@GivenName VARCHAR(255),
@Gender CHAR(1),
@City VARCHAR(255),
@State CHAR(2),
@count int =0,
@strbcpcmd VARCHAR(500),
@filename varchar(70)
SET @xml_beg = '<?xml version="1.0" encoding="UTF-16"?>
<Company xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Company.xsd">'
SET @xmlEnd = '</Company>'
SET @comp_xmlpartO = '<CompanyData>
<ID></ID>
<CompanyName><![CDATA[~fname~]]></CompanyName>
<GivenName><![CDATA[~lname~]]></GivenName>
<Addresses>
<BillingAddress>
<Address1></Address1>
<Address2></Address2>
<Address3></Address3>
<City></City>
<State></State>
<ZipCode></ZipCode>
<Fax1></Fax1>
</BillingAddress>
</Addresses>
<CompanyData>'
set @company_xmlpart = @company_xmlpartO
DECLARE xml_cursor CURSOR FOR
SELECT CompanyName , GivenName
OPEN xml_cursor
FETCH NEXT FROM xml_cursor into @CompName, @GivennNme
WHILE @@fetch_status = 0
BEGIN
SET @company_xmlpart = REPLACE(@company_xmlpart,N'~fname~',@CompName )
SET @customer_xmlpart = REPLACE(@company_xmlpart,N'~lname~',@GivenName )
if(@count > 0)
BEGIN
SET @company_xml = @company_xml + @company_xmlpart
END
else
BEGIN
SET @company_xm = @company_xmlpart
END
SET @count = @count +1
SET @company_xmlpart = @company_xmlpartO
FETCH NEXT FROM xml_cursor into @CompName, @GivenName
END
SET @company_xml= @xml_beg + @company_xml + @xmlEnd
SET @xml = CAST(@company_xml AS XML)
set @fileName = 'D:\Samples\compsample1.xml'
CLOSE xml_cursor
DEALLOCATE xml_cursor
set @strbcpcmd = 'bcp "exec ' + cast(@xml as varchar(max)) + ' " queryout ' + @fileName + ' -S servernmae -U userid -P password -T -w -r -t';
exec xp_cmdshell @strbcpcmd
答案 0 :(得分:0)
错误不是SQL Server错误,因此会被忽略。错误来自命令shell。
xp_cmdshell确实返回返回代码值。
DECLARE @result int
EXEC @result = xp_cmdshell .....
命令shell的输出返回结果集。
DECLARE @ReturnCode INT,
@ErrorMessage varchar(2000)
CREATE TABLE #temp (SomeCol VARCHAR(500))
INSERT #temp
EXEC @ReturnCode = master.dbo.xp_cmdshell ....
IF @ReturnCode <> 0
BEGIN
SELECT @ErrorMessage = @ErrorMessage + SomeCol
FROM #temp
WHERE SomeCol IS NOT NULL
SELECT @ErrorMessage as ErrorMessage, @ReturnCode as ReturnCode
END