Solved

Using xp_cmdshell eror when doing select

Posted on 2007-03-31
9
5,714 Views
Last Modified: 2008-01-09
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_Product_Group where product_group_description like ''%pulp%'' for xml auto, type, root(''root'')" queryout c:\test.xml -T -c'
exec  xp_cmdshell @sql
0
Comment
Question by:Fraser_Admin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
9 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 350 total points
ID: 18829523
please try this:
declare @sql as nvarchar(8000)
set @sql = 'bcp "SELECT Product_Group_Description FROM Scales_fpnb.dbo.Lookup_Product_Group where product_group_description like ''%pulp%'' for xml auto, type, root(''root'')" queryout c:\test.xml -T -c'
exec  xp_cmdshell @sql
0
 

Author Comment

by:Fraser_Admin
ID: 18830018
yes, that fixed the problem, but the maximum size i have is 4000?  is there any way i can have more than that?
0
 

Author Comment

by:Fraser_Admin
ID: 18830054
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!ELEMENT],
    NULL ' is too long. Maximum length is 128.
Msg 102, Level 15, State 1, Line 112
Incorrect syntax near 'queryout'.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Fraser_Admin
ID: 18830483
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!ELEMENT],
    NULL as [HEADER!2!FILE_NAME!ELEMENT],
    NULL as [LOAD!3!SCALE_DATE!ELEMENT],      
    NULL as [LOAD!3!DISTRICT!ELEMENT],
    NULL as [LOAD!3!LOAD_SLIP_NUMBER!ELEMENT],
    NULL as [LOAD!3!TRANS_SUFFIX!ELEMENT],
    NULL as [LOAD!3!PILE_NUMBER!ELEMENT],
    NULL as [LOAD!3!ACTIVITY_NUMBER!ELEMENT],
    NULL as [LOAD!3!BLOCK_OR_ORIGIN!ELEMENT],
    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!ELEMENT],
    NULL as [LOAD!3!TRUCK_ID!ELEMENT],
    NULL as [LOAD!3!LOADER_ID!ELEMENT],
    NULL as [LOAD!3!DESTINATION!ELEMENT],
    NULL as [LOAD!3!CUSTOMER_SCALE_BILL!ELEMENT],
    NULL as [LOAD!3!CONTRACT_NO!ELEMENT],
    NULL as [LOAD!3!ERROR_CORRECTION!ELEMENT],
    NULL as [LOAD!3!SCALE_UNIT!ELEMENT],
    NULL as [LOAD!3!SCALE_VOLUME!ELEMENT],
    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!ELEMENT],
    NULL as [LOAD!3!BILLING_RATE!ELEMENT],
    NULL as [LOAD!3!CUSTOMER_HARVEST!ELEMENT],
    NULL as [LOAD!3!CUSTOMER_TRUCKED!ELEMENT]
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????
0
 

Author Comment

by:Fraser_Admin
ID: 18830674
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_XMLFile" queryout C:\test.xml -T -c'  

I get this for output..
SQLState = HY010, NativeError = 0
Error = [Microsoft][SQL Native Client]Function sequence error
0
 
LVL 24

Assisted Solution

by:DBAduck - Ben Miller
DBAduck - Ben Miller earned 150 total points
ID: 18830743
There is a limit to the command length and that could be one of the problems.  Normally when you have a command this long with bcp, you should test the complete command on the commandline to see if you get any errors before you try to automate it.  This particular one would error because of the quotes, etc. and bcp being a command shell thing, would give a USAGE problem because it was not a complete command.

So I would test it at the command line before doing this and if you do not get an error, then it should work using xp_cmdshell.  
0
 

Author Comment

by:Fraser_Admin
ID: 18830750
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!!
0
 

Author Comment

by:Fraser_Admin
ID: 18830819
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?
0
 

Author Comment

by:Fraser_Admin
ID: 18830832
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.
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question