Solved

Using xp_cmdshell eror when doing select

Posted on 2007-03-31
9
5,520 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
  • 7
9 Comments
 
LVL 142

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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

786 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