Sqlcmd: Error: near command 'Ë' - sql-server

I'm importing a 7GB .sql file and I got this error:
Sqlcmd: Error: Syntax error at line 7863597 near command 'Ë' in file 'C:\inetpub\wwwroot\project\MS_SQL_Dump\MS_SQL_Dump.sql'.
I'm currently trying running the import command again but with double quotes around the path/filename. I don't know if that will work though, it was suggested on a comment on this question.
Any idea what the issue is and how to resolve it?

It could be because you have a $ in your query. Try adding -x to the end of your sqlcmd statement, ie:
sqlcmd -i c:\import.sql -x
Worked for me...hope it helps someone!
source: https://ts.apexsql.com/the-sqlcmd-error-syntax-error-at-line-xyz-near-command-x-in-file-file_name-sql-error-is-encountered-when-running-sql-script-via-sqlcmd-utility/

all I've got is a .sql file
In this case try to load this script view dbForge for SQL Server:
and provide error message...

I've experienced this before and this error is due to quotation marks.
Please on the quoted_identifier setting which is defaulted to OFF.
Please take a look on sqlcmd options here : sqlcmd utility
When SET QUOTED_IDENTIFIER is ON, identifiers can be delimited by double quotation marks, and literals must be delimited by single quotation marks. When SET QUOTED_IDENTIFIER is OFF, identifiers cannot be quoted and must follow all Transact-SQL rules for identifiers. For more information, see Database Identifiers.
To set this to on just add sqlcmd option "-I" (take note of the case )
Like this :
sqlcmd -I

Related

T-SQL error if use dollar and bracket symbols together

I'm getting the following error message with execution T-SQL script with this construction "$(", in sqlcmd utility:
C:\Users\Admin>sqlcmd -S DESKTOP-5AA9JIA\SQLEXPRESS -q "select '$(' " -R
Sqlcmd: Error: Syntax error at line 1 near command '''.
1>
if run this script via SSMS then everything works smoothly.
Getting the same error, when using INSERT INTO statement or any other statement.
Any suggestion on how to resolve it?
You should use -x when launch sqlcmd to ignore scripting variables.
-x
Causes sqlcmd to ignore scripting variables. This is useful when a
script contains many INSERT statements that may contain strings that
have the same format as regular variables, such as $(variable_name).
sqlcmd Utility
When sqlcmd parser sees $(it expects scripting variable that is not provided so it throws the error.
Here is my test:

Deploying SQL Changes Containing $(ESCAPE_SQUOTE())

I have a Database project in Visual Studio that I am attempting to deploy automatically to a test environment nightly. To accomplish this I am using TFS which leverages a PowerShell script to run "SqlPackage.exe" to deploy any changes that have occurred during the day.
Some of my procs contain logic that is run inside of a script that is part of a agent job step and contains the following code(In dynamic SQL):
$(ESCAPE_SQUOTE(JOBID))
When deploying changes that affect this proc, I get the following issue:
SQL Execution error: A fatal error occurred. Incorrect syntax was
encountered while $(ESCAPE_SQUOTE( was being parsed.
This is a known issue, it appears as though that is not supported. It appears to be a function of the "SQLCmd" command misinterpreting the $( characters as a variable:
"override the value of a SQL command (sqlcmd) variable used during a
publish action."
So how do I get around this? It seems to be a major limitation of "sqlcmd" that you can't disable variables, I don't see that parameter that supports that...
Update 1
Seems as through you can disable variable substitution from "sqlcmd" by feeding it the "-x" argument(Source, Documentation):
-x (disable variable substitution)
Still don't see a way to do this from "SqlPackage.exe" though.
It seems that sqlcmd looks for the $( as a token, so separating those two characters is good enough. You can do this with a dynamic query that does nothing more than break the query into two strings:
DECLARE #query nvarchar(256) = N'... $' + N'(ESCAPE_SQUOTE(JOBID)) ...';
EXEC sp_executesql #query
One way to get around this is to refactor the "$(ESCAPE_SQUOTE(JOBID))" string into a scalar function, then setup a PowerShell script to directly invoke the "Sqlcmd" command with the "-x" parameter to "Create/Alter" said function before running "SqlPackage.exe".
Looks something like this in PowerShell:
$sql = #"
USE $database
GO
CREATE FUNCTION [dbo].[GetJobID] ()
RETURNS NVARCHAR(MAX)
AS
BEGIN
RETURN '$(ESCAPE_SQUOTE(JOBID))'
END
GO
"#;
Sqlcmd -S $servername -U $username -P $password -Q $sql -x;
This is a pretty poor workaround, but it does accomplish the task. Really hoping for something better.
I propose another workaround
my job has a step running : DTEXEC.exe /SERVER "$(ESCAPE_NONE(SRVR))"
I just have to add a SQLCMD variable before:
:setvar SRVR "$(ESCAPE_NONE(SRVR))"
this way the toked is considered as SQLCMD variables $(SRVR) and is replaced by the requested value

r: "$(fileName)" in a script called from sqlcmd

I'm trying to create a script just to run other scripts and do some extra stuff in case of successful or failure.
You have the full code on this link just to try to be clear what I'm trying to achieve.
Basically I want to:
-- DO SOME STUFF HERE
r: "$(fileName)"
-- MORE STUFF HERE
and call it from sqlcmd this way:
sqlcmd -i "RunScript.sql" -v fileName="someFileName.sql" -s server -d database
But I can't, I'm getting the following error:
Msg 102, Level 15, State 1, Server SERVER, Line 19 Incorrect syntax
near 'someFileName.sql'.
So, it seems that the little r: couldn't be used with a parameter on his side.
Just to clarify, someFileName.sql isn't in the SQL Server, but in my machine, so I couldn't use this way to read the file. In fact, I just tried it later.
Is there a workaround to archive this? Any ideas to solve it?
You could break your current RunScript.sql script into separate header and footer SQL scripts and concatenate them together with a SQL script in the middle that is denoted by an input parameter to a CMD script. For example:
RunSQL.CMD consists of:
#ECHO OFF
COPY /V /Y RunScriptHeader.sql + %1 + RunScriptFooter.sql RunScriptTemp.sql
SQLCMD -i "RunScriptTemp.sql" -s server -d database
The /V does a verify on the copy
The /Y suppresses prompting to confirm overwriting an existing file
You would run it as follows:
RunSQL.CMD someFileName.sql

Need sqsh to ignore a dollar sign

The Environment:
Solaris 10, /bin/sh script using sqsh and freetds to talk to an MS SQL Server
The (TLDR) Problem:
I need sqsh to ignore a dollar sign and pass it through to MS SQL.
The Background:
I'm writing some code that dynamically builds SQL to alter existing indexes but when it runs, I get errors:
Msg 2727, Level 11, State 1
Server 'HOST\SERVER', Line 1
Cannot find index 'foo'.
I dig around and see that there is no index named foo but there is one named foo$bar.
The built-up input SQL input looks fine...
% grep 'foo' input.sql
alter index [foo$bar] on ...
...and running this SQL through a New Query session or a third party app succeeds. However, the results when passed through sqsh don't see past that dollar sign:
% sqsh -e -i input.sql -o output.sql
% grep 'foo' output.sql
1> alter index [foo] on ...
...which suggests it's interpreting $bar as a variable or something.
Anyone know how to get sqsh to escape a dollar sign or allow one to pass through? I've tried various combinations of quotes and backslashes.
Help me, stackoverlow. You're my only hope.
Another option is to disable buffer expansion altogether by executing:
\set expand=0
on the sqsh prompt, or specify this command in the .sqshrc file, or start sqsh with the parameter
sqsh -e -i input.sql -o output.sql -Lexpand=0
If expansion is on (default) then sqsh will substitute the variable $bar with its contents.
Reference manual on page 5 states: "Note that in order to prevent the expansion of a variable use either single quotes, or two \’s, like thus:
1> \echo \\$name
$name
So, I believe that to prevent sqsh to substitute $bar with an empty string, you have to write something like:
alter index [foo\\$bar] on ...
alter index ['foo$bar'] on ...
To test it you can try first with SELECT 'foo\\$bar' = 1 or something similar.

SQLCMD Syntax error when running script that runs fine in management studio

When I run the this select '$(''test'')' in SQL Management Studio 2008 it returns $('test')
When I run sqlcmd -S SERVER -E -d DATABASE -q "select $(''test'')" on the command line it returns Sqlcmd: Error: Syntax error at line 1 near command '''.
If I remove the dollar sign it works. Is the "$" a special character?
Is this a sqlcmd bug? How can I change the script to get the desired result.
Yes, $(id) has special semantics in SQLCMD: it is a variable substitution. You can run commands like:
sqlcmd /E /S . /v variable=MyTable /Q "select * from $(variable)"
and this will select from MyTable. As you guess, the /v is the switch to define a variable. SSMS on the other hand does not, by default, interpret the SQL for variable substitution. SSMS can be made to do this, by checking the option 'Open query widows in SQLCMD mode'.
For mode details see Using sqlcmd with Scripting Variables.
the command you are trying to run:
select $('test')
is not valid. As you note, when you remove the "$" it works:
select ('test')
I'm not sure what you are really trying to do, you have three " double quote characters, you could try using this command:
select '$(test)'
which would be:
sqlcmd -S SERVER -E -d DATABASE -q "select ''$(test)''"

Resources