Fraser_Admin
asked on
Using xp_cmdshell eror when doing select
I'm trying to use xp_cmbshell by setting up the call as a variable. Just becaues my final select is quite complicated. For this example, I just used a small select.
When I run the following I'm getting an error:
Msg 214, Level 16, State 201, Procedure xp_cmdshell, Line 1
Procedure expects parameter 'command_string' of type 'varchar'.
I also tried casting @sql as varchar, but still the same error. I also tried doing @command_string = @sql and command_string = @sql. But all gives errors. I can run the select fine if I don't have it setup as a variable.
declare @sql as nvarchar(max)
set @sql = 'bcp "SELECT Product_Group_Description FROM Scales_fpnb.dbo.Lookup_Pro duct_Group where product_group_description like ''%pulp%'' for xml auto, type, root(''root'')" queryout c:\test.xml -T -c'
exec xp_cmdshell @sql
When I run the following I'm getting an error:
Msg 214, Level 16, State 201, Procedure xp_cmdshell, Line 1
Procedure expects parameter 'command_string' of type 'varchar'.
I also tried casting @sql as varchar, but still the same error. I also tried doing @command_string = @sql and command_string = @sql. But all gives errors. I can run the select fine if I don't have it setup as a variable.
declare @sql as nvarchar(max)
set @sql = 'bcp "SELECT Product_Group_Description FROM Scales_fpnb.dbo.Lookup_Pro
exec xp_cmdshell @sql
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
OK, my actual query is much more complicated than that. It is quite long, not over 4000 so I'm ok there, but it is over 128 and i'm now getting this message when i try to execute...
Msg 103, Level 15, State 4, Line 1
The identifier that starts with 'SELECT 1 as Tag,
0 as Parent,
NULL as [WOOD_PRODUCT_SOLD!1],
NULL as [HEADER!2!EMAIL_ADDRESS!EL EMENT],
NULL ' is too long. Maximum length is 128.
Msg 102, Level 15, State 1, Line 112
Incorrect syntax near 'queryout'.
Msg 103, Level 15, State 4, Line 1
The identifier that starts with 'SELECT 1 as Tag,
0 as Parent,
NULL as [WOOD_PRODUCT_SOLD!1],
NULL as [HEADER!2!EMAIL_ADDRESS!EL
NULL ' is too long. Maximum length is 128.
Msg 102, Level 15, State 1, Line 112
Incorrect syntax near 'queryout'.
ASKER
OK I think that was a problem with quotes. Here is my next problem. I have done a pre-select, to decrease the number of characters in my @sql. I am selecting into a temp table. Here is my @sql now...
declare @sql as nvarchar(4000)
set @sql = 'bcp "SELECT 1 as Tag,
0 as Parent,
NULL as [WOOD_PRODUCT_SOLD!1],
NULL as [HEADER!2!EMAIL_ADDRESS!EL EMENT],
NULL as [HEADER!2!FILE_NAME!ELEMEN T],
NULL as [LOAD!3!SCALE_DATE!ELEMENT ],
NULL as [LOAD!3!DISTRICT!ELEMENT],
NULL as [LOAD!3!LOAD_SLIP_NUMBER!E LEMENT],
NULL as [LOAD!3!TRANS_SUFFIX!ELEME NT],
NULL as [LOAD!3!PILE_NUMBER!ELEMEN T],
NULL as [LOAD!3!ACTIVITY_NUMBER!EL EMENT],
NULL as [LOAD!3!BLOCK_OR_ORIGIN!EL EMENT],
NULL as [LOAD!3!SECTION!ELEMENT],
NULL as [LOAD!3!PRODUCT!ELEMENT],
NULL as [LOAD!3!SPECIES!ELEMENT],
NULL as [LOAD!3!GRADE!ELEMENT],
NULL as [LOAD!3!PRODUCT_LENGTH!ELE MENT],
NULL as [LOAD!3!TRUCK_ID!ELEMENT],
NULL as [LOAD!3!LOADER_ID!ELEMENT] ,
NULL as [LOAD!3!DESTINATION!ELEMEN T],
NULL as [LOAD!3!CUSTOMER_SCALE_BIL L!ELEMENT] ,
NULL as [LOAD!3!CONTRACT_NO!ELEMEN T],
NULL as [LOAD!3!ERROR_CORRECTION!E LEMENT],
NULL as [LOAD!3!SCALE_UNIT!ELEMENT ],
NULL as [LOAD!3!SCALE_VOLUME!ELEME NT],
NULL as [LOAD!3!CULL!ELEMENT],
NULL as [LOAD!3!VOID!ELEMENT],
NULL as [LOAD!3!DEFECT!ELEMENT],
NULL as [LOAD!3!MOISTURE!ELEMENT],
NULL as [LOAD!3!NET_VOLUME!ELEMENT ],
NULL as [LOAD!3!QUALITY_ADJUSTMENT !ELEMENT],
NULL as [LOAD!3!BILLING_SCALE_UNIT !ELEMENT],
NULL as [LOAD!3!BILLING_VOLUME!ELE MENT],
NULL as [LOAD!3!BILLING_RATE!ELEME NT],
NULL as [LOAD!3!CUSTOMER_HARVEST!E LEMENT],
NULL as [LOAD!3!CUSTOMER_TRUCKED!E LEMENT]
UNION ALL
SELECT 2,
1,
NULL,
''myemailaddy@mydomain.com '',
''BIOMASS.XML'',
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
UNION ALL
SELECT 3,
1,
NULL,
NULL,
NULL,
Date,
'''',
Transportation,
'''',
''0'',
'''',
'''',
'''',
Product,
'''',
Grade,
Length,
'''',
'''',
''FRA'',
ScaleBill,
'''',
'''',
Units,
Amount,
Cull,
Void,
Undersized,
0.000,
Amount,
'''',
Units,
Amount,
rate,
''N'',
''N''
FROM #tmpTable
FOR XML EXPLICIT" queryout c:\test1.xml -T -c'
print @sql
exec xp_cmdshell @sql
I am just getting a USAGE result. So I'm doing something wrong with my syntax, but I can't figure out what????
declare @sql as nvarchar(4000)
set @sql = 'bcp "SELECT 1 as Tag,
0 as Parent,
NULL as [WOOD_PRODUCT_SOLD!1],
NULL as [HEADER!2!EMAIL_ADDRESS!EL
NULL as [HEADER!2!FILE_NAME!ELEMEN
NULL as [LOAD!3!SCALE_DATE!ELEMENT
NULL as [LOAD!3!DISTRICT!ELEMENT],
NULL as [LOAD!3!LOAD_SLIP_NUMBER!E
NULL as [LOAD!3!TRANS_SUFFIX!ELEME
NULL as [LOAD!3!PILE_NUMBER!ELEMEN
NULL as [LOAD!3!ACTIVITY_NUMBER!EL
NULL as [LOAD!3!BLOCK_OR_ORIGIN!EL
NULL as [LOAD!3!SECTION!ELEMENT],
NULL as [LOAD!3!PRODUCT!ELEMENT],
NULL as [LOAD!3!SPECIES!ELEMENT],
NULL as [LOAD!3!GRADE!ELEMENT],
NULL as [LOAD!3!PRODUCT_LENGTH!ELE
NULL as [LOAD!3!TRUCK_ID!ELEMENT],
NULL as [LOAD!3!LOADER_ID!ELEMENT]
NULL as [LOAD!3!DESTINATION!ELEMEN
NULL as [LOAD!3!CUSTOMER_SCALE_BIL
NULL as [LOAD!3!CONTRACT_NO!ELEMEN
NULL as [LOAD!3!ERROR_CORRECTION!E
NULL as [LOAD!3!SCALE_UNIT!ELEMENT
NULL as [LOAD!3!SCALE_VOLUME!ELEME
NULL as [LOAD!3!CULL!ELEMENT],
NULL as [LOAD!3!VOID!ELEMENT],
NULL as [LOAD!3!DEFECT!ELEMENT],
NULL as [LOAD!3!MOISTURE!ELEMENT],
NULL as [LOAD!3!NET_VOLUME!ELEMENT
NULL as [LOAD!3!QUALITY_ADJUSTMENT
NULL as [LOAD!3!BILLING_SCALE_UNIT
NULL as [LOAD!3!BILLING_VOLUME!ELE
NULL as [LOAD!3!BILLING_RATE!ELEME
NULL as [LOAD!3!CUSTOMER_HARVEST!E
NULL as [LOAD!3!CUSTOMER_TRUCKED!E
UNION ALL
SELECT 2,
1,
NULL,
''myemailaddy@mydomain.com
''BIOMASS.XML'',
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
UNION ALL
SELECT 3,
1,
NULL,
NULL,
NULL,
Date,
'''',
Transportation,
'''',
''0'',
'''',
'''',
'''',
Product,
'''',
Grade,
Length,
'''',
'''',
''FRA'',
ScaleBill,
'''',
'''',
Units,
Amount,
Cull,
Void,
Undersized,
0.000,
Amount,
'''',
Units,
Amount,
rate,
''N'',
''N''
FROM #tmpTable
FOR XML EXPLICIT" queryout c:\test1.xml -T -c'
print @sql
exec xp_cmdshell @sql
I am just getting a USAGE result. So I'm doing something wrong with my syntax, but I can't figure out what????
ASKER
I also just tried putting my entire selection into a sp. When I run the sp from Management Studio I get an XML file. Just like I want.
When I run the following command...
EXEC xp_cmdshell 'bcp "exec scales_fpnb.dbo.report_XML File" queryout C:\test.xml -T -c'
I get this for output..
SQLState = HY010, NativeError = 0
Error = [Microsoft][SQL Native Client]Function sequence error
When I run the following command...
EXEC xp_cmdshell 'bcp "exec scales_fpnb.dbo.report_XML
I get this for output..
SQLState = HY010, NativeError = 0
Error = [Microsoft][SQL Native Client]Function sequence error
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Can you give me an example? I am running the sp and it returns an xml file fine. I just need to either email this xml file, or save it so I can email it. This is getting crazy. To me this should be an easy task, and it is very complicated. All I'm trying to do is send the output of a sp to a file....errrggg this is driving me crazy!!
ASKER
OK I'm getting closer. I fixed the above error by getting rid of my creation of the temp table. I also put databasename.dbo infront of all my tables when selecting. So one of the two of those things fixed that problem.
Here is the next problem. When I try to open my xml file, the first few records are good. But then about the third record it screws up. So I edited the file in SQL Server management studio, and it is just keeping everything on one line. Then when it gets to whatever the max line size is, it breaks, well sometimes it is in the middle of a tag, then that is where it screws up.
Is it a problem with the fact that I'm just assigning the output to an xml file. Is there something else I need to state to indicate this is actually an xml file and not a text file or something?
Here is the next problem. When I try to open my xml file, the first few records are good. But then about the third record it screws up. So I edited the file in SQL Server management studio, and it is just keeping everything on one line. Then when it gets to whatever the max line size is, it breaks, well sometimes it is in the middle of a tag, then that is where it screws up.
Is it a problem with the fact that I'm just assigning the output to an xml file. Is there something else I need to state to indicate this is actually an xml file and not a text file or something?
ASKER
omg the madness is over!!!!!!!!!!!!!!!!!!!!!
I added -w -r "" to the bcp statement, and now the formatting is perfect.
I can't believe this finally works. Many hours spent here trying to get something what I would expect to be simple working.
I added -w -r "" to the bcp statement, and now the formatting is perfect.
I can't believe this finally works. Many hours spent here trying to get something what I would expect to be simple working.
ASKER