xp_cmdshell - Procedure expects parameter 'command_string' of type 'varchar'

  • Here are the contents of @Cmd (varchar(max)):

    bcp "SELECT [Data] FROM myserver..dbo.PEC_Citrus_RptData where [Data] is not null order by [Data]" queryout "C:\Documents and Settings\All Users\Documents\Shared\OutputFile.txt" -c -t -T -S "MYMACHINE\MYINSTANCE"

    Here's the code:

    EXEC master..xp_cmdshell @Cmd ;

    Here is the error:

    Msg 214, Level 16, State 201, Procedure xp_cmdshell, Line 1

    Procedure expects parameter 'command_string' of type 'varchar'.

    ?

    *******************
    What I lack in youth, I make up for in immaturity!

  • You can't use varchar(max) with xp_cmdshell. Found that out the hard way. Try a finite number (I usually just go with either 1000 or 8000).

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Well good grief, that was the problem! Kinda misleading error message! Thanks.

    *******************
    What I lack in youth, I make up for in immaturity!

  • You're welcome.

    It's probably because they haven't rewritten xp_cmdshell since SQL 2000, and that didn't have the max option available.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Thank Odin for the internet -- and GSquared for this post! You just saved me a lot of time.

  • I experienced this error, I now make sure I make nvarchar types with 4000 rather than using the max function.

    Also just a note on BCP. Ensure it's always on one line, else it won't work, won't even print, very annoying!

  • Hi,

    I am having the same problem. My query string is greater than 8000 characters. I am using varchar(max).

    Can someone assist urgently with a result, please.

  • Maybe switch to a Powershell script.

    *******************
    What I lack in youth, I make up for in immaturity!

  • riswana (11/1/2012)


    Hi,

    I am having the same problem. My query string is greater than 8000 characters. I am using varchar(max).

    Can someone assist urgently with a result, please.

    On computers running Microsoft Windows XP or later, the maximum length of the string that you can use at the command prompt is 8191 characters.

    so you need to shorten up whatever is being passed as a query to your bcp.

    use the query string to insert the data into a view, and use bcp to select from the view instead.

    see this very similar post, where the Original poster needed to change his bcp query to select from a view(or global temp table):

    http://www.sqlservercentral.com/Forums/FindPost1378950.aspx

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • riswana (11/1/2012)


    Hi,

    I am having the same problem. My query string is greater than 8000 characters. I am using varchar(max).

    Can someone assist urgently with a result, please.

    It's easy. Write a stored procedure and use BCP to call the stored procedure.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Thank you again to all on this thread. Just saved me a lot of time and headache. 🙂

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply