Link to home
Start Free TrialLog in
Avatar of Fraser_Admin
Fraser_AdminFlag for Canada

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_Product_Group where product_group_description like ''%pulp%'' for xml auto, type, root(''root'')" queryout c:\test.xml -T -c'
exec  xp_cmdshell @sql
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Fraser_Admin

ASKER

yes, that fixed the problem, but the maximum size i have is 4000?  is there any way i can have more than that?
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'.
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????
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!!
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?
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.