2

I am trying to pass in double quote to a scripting variable in SQLCMD. Is there a way to do this?

sqlcmd -S %serverName% -E -d MSDB -i MyScript.sql -m 1 -v Parameter="\""MyValueInDoubleQuote\""" 

And my sql script is as follow:

--This Parameter variable below is commented out since we will get it from the batch file through sqlcmd
--:SETVAR Parameter "\""MyValueInDoubleQuote\"""


INSERT INTO [MyTable]
           ([AccountTypeID]
           ,[Description])
     VALUES
           (1
           ,$(Parameter))
GO

2 Answers 2

3

If you have your sql script set up in this fashion:

DECLARE @myValue VARCHAR(30)
SET @myValue = $(MyParameter)
SELECT @myValue

Then you can get a value surrounded by double quotes into @myValue by just enclosing your parameter in single quotes:

sqlcmd -S MyDb -i myscript.sql -v MyParameter='"123"'

This works because -v is going to replace the $(MyParameter) string with the text '"123"'. The resulting script will look like this before it is executed:

DECLARE @myValue VARCHAR(30)
SET @myValue = '"123"'
SELECT @myValue

Hope that helps.

EDIT
This sample is working for me (tested on SQL Server 2008, Windows Server 2K3). It inserts a record into the table variable @MyTable, and the value in the Description field is enclosed in double quotes:

MyScript.sql (no need for setvar):

DECLARE @MyTable AS TABLE([AccountTypeID] INT, [Description] VARCHAR(50))

INSERT INTO @MyTable ([AccountTypeID] ,[Description])
VALUES(1, $(Parameter))

SELECT * FROM @MyTable

SQLCMD:

sqlcmd -S %serverName% -E -d MSDB -i MyScript.sql -m 1 -v Parameter='"MyValue"'

If you run that script, you should get the following output, which I think is what you're looking for:

(1 rows affected)
AccountTypeID Description
------------- --------------------------------------------------
            1 "MyValue"
Sign up to request clarification or add additional context in comments.

9 Comments

I'll try that route however I am using scripting variable. So my variable is as follow: :SETVAR MyVar "MyValue"
OK. If my suggestion doesn't work can you post a sample of your SQL script in the question?
Posted my sql script in the question
I'd be happy to update my answer, but your update to the question is a little confusing. Are you using SETVAR or not?
I updated my sql script. The SetVar I am trying to define it from SQLCMD using the -v flag. Hence in the SQL script the SetVar is commented out.
|
2

Based on your example, you don't need to include the quotes in the variable, as they can be in the sql command, like so:

sqlcmd -S %serverName% -E -d MSDB -i MyScript.sql -m 1 -v Parameter="MyValueNoQuotes"

and

INSERT INTO [MyTable]
            ([AccountTypeID]
            ,[Description])
      VALUES
            (1
            ,"$(Parameter)")

(Though I am more accustomed to use single quotes, as in ,'$(Parameter)'

3 Comments

That will script it out as MyValueNoQuotes. I am hoping there is an escape key to put the double quotes in.
It seems that at sqlcmd level when I pass in as """MyValue""" it will out put as ""MyValue"". While I wanted to get "MyValue" and I am not able to do so.
I'm missing a point here: why do the double quotes need to be included in the SQLCMD variable? I'm proposing to "move" them into the SQL code, such that they don't need to be part of the variable. In your code you are only using them to delimit the string value being written to the Description column.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.