Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2013
    Posts
    4

    Unanswered: Problem With Using "%" Percent Sign in Variable in Sqlcmd

    I am trying to use this command to change the variable in where clause each time:
    psexec \\servername -E cmd.EXE /c "sqlcmd -S servername /E -d dbname -v entertext="'%anything%'" -i c:\myfolder\myscript.sql -o c:\mypath\output.sql"
    myscript.sql is like:
    select * from table_name
    where summary like $(entertext);
    This command works just for variables with single and exact word. It does not work for variables which has space between them or when I enter part of summary .
    In fact, I wanted to write something which the user be able to enter variable each time when he/she run this batch file. because I put this sqlcmd command in batch file( like this in oracle: "select * from table_name where summary = &entertext; " I was looking the same command in sql server , but I did not find it). I do not know .net or any other programing language( I know just csh and command line). I want to do this just with sql command.
    Could anybody help me PLEASE??????? Thank you

  2. #2
    Join Date
    Dec 2013
    Posts
    4

    Red face

    I found this sqlcmd command works from my command prompt correctly, but it does not work from inside my batch file. ANy suggestion would be really appreciated

  3. #3
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Percent signs are used by the batch file interpreter to indicate the use of batch and environment variables. This use is documented at Microsoft Corporation

    To include a literal percent sign like you want to use with SQLCMD.EXE you need to double the percent signs. In other words where you want to use %anything% as a literal, you need to type %%anything%% into your batch file.

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  4. #4
    Join Date
    Dec 2013
    Posts
    4
    It worked! Thank you for your solution Pat.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •