Solved

Using xp_cmdshell eror when doing select

Posted on 2007-03-31
9
5,389 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
 

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now