VS2017 DBPROJ SQLCMD Variable Test for Literal Value - sql-server-2017

I want to conditionally run a post-deploy script. My script (currently) looks like this:
declare #tier nvarchar(100) = $(tier)
if( #tier = 'TEST' )
begin
:r .\ConfigSeedData.TEST.sql
end
I have defined the variable in the project file, as detailed in Using variable in sql postdeployment build script?. The variable is (currently) defined as TEST (no quotes, but I've tried it with quotes, too).
However, no matter how I try to twist my syntax, I keep getting this error:
.Net SqlClient Data Provider: Msg 207, Level 16, State 1, Line 875 Invalid column name 'TEST'.
How do I check a sqlcmd variable for a literal value, not a column?
I have tried adding the square brackets around the $(), tried accessing the value directly (without assigning to nvarchar).
Thanks!!

So it turns out, you need to put single quotes around the variable use. To make the above work, the code would be:
if( '$(tier)' = 'TEST' )
So even though it looks like you're using the literal "$(tier)", it's actually being pulled out of the quotes, interpreted, and put back in.

Related

Cannot query my container using the GridDB Shell?

I have a device container with the name 1cbfce15ec4d which houses some my data. I know for a fact there's data in there, but when I try a simple query in the griddb shell, I got the following error:
gs[public]> sql select * from 1cbfce15ec4d;
D20332: An unexpected error occurred while executing a SQL. : msg=[[240001:SQL_COMPILE_SYNTAX_ERROR] Parse SQL failed, reason = Syntax error: 1cbfce15ec4d; on executing query (sql="select * from 1cbfce15ec4d") (db='public') (user='admin') (appName='gs_sh') (clientId='a6d92f48-e558-440-86dd-a05e949fa726:1') (clientNd='{clientId=3, address=127.0.0.1:55744}') (address=127.0.0.1:20001, partitionId=983)]
I am not exactly sure what is going on here -- at first I assumed my data must be corrupt or empty, but that is not the case. It seems to be a case of the shell dying trying to process something about that container name.
Any ideas?
According to the manual :
"If the name of a table or a column contains characters other than ASCII alphanumeric characters and underscore, or if the first character of the name is a number in a SQL statement, enclose the name with double quotation marks."
Try select * from "1cbfce15ec4d"

Error while converting from getdate() function

On command line
convert(varchar,getdate(),120)
gives below error:
Unknown argument '04:59:42.xml'
I am saving data to an xml file.
when I use below command,proc works perfectly.
convert(varchar,getdate(),112)
I need the file to save with time.
You are trying to create a file with a colon in the name, which is not allowed. No way to get around that restriction. However, you can replace the colons with other characters when naming the file, e.g.
replace(convert(varchar,getdate(),120),':','')

Tcl SQLite update variable substitution cannot have apostrophe

Here's the problem: if I use { } for the update command like so:
package require sqlite3
fileRepo eval {UPDATE uploads SET $col=$data WHERE rowid=$id}
I cannot substitute any variables inside the curly brackets. it all has to be hard coded.
However, if I use " " for the update command like so:
fileRepo eval "UPDATE uploads SET $col='$data' WHERE rowid=$id"
I can substitute variables inside the double quotes, but I must use ' ' in order to put in data with spaces so sql sees it as one input. If I don't I get an error if I send something like
$data = "Legit Stack"
Because it has a space the sql will choke on the word: Stack
unless it is placed within single quotes
Therefore...
If I send this data to the update command:
$col = description
$data = "Stack's Pet"
I get the following error:
near "s": syntax error while executing "fileRepo eval "UPDATE uploads
SET $col='$data' WHERE rowid=$id" ...
Thus given these rules I can see no way to pass a single quote or apostrophe to the update command successfully. Is there a different way to do this?
Thanks!
While it is true that you can escape the single quotes by doubling them (as usual in SQL), you open up your code to the dangers of SQL injection attacks.
It might be better to split your code into two distinct steps:
Substitute with format {UPDATE uploads SET %s=$data WHERE rowid=$id} $col
let sqlite3 magic eval turn the $data and $id into bound variables for a prepared statement
This way you only need to sanitize your col variable, to make sure it contains a valid column name and nothing else (should be easy), instead of all your data. In addition, you do not need to copy large values as often, so a two step approach will even be faster. To make it even clearer you want to use a bind variable, try the alternative syntax with a : in front of a variable name.
package require sqlite3
set stmt [format {UPDATE uploads SET %s=:data WHERE rowid=:id} $col]
fileRepo eval $stmt
Recommended Reading:
For the : syntax: https://www.sqlite.org/tclsqlite.html#eval
For more information about SQL Injections: https://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet
You have to use an escape apostrophe. So it should look like this:
$data = "Stack''s Pet"

SSIS pkg does not recognize the file path

I'm using the 2012 version of Visual Studio to build an SSIS package. I have a variable var_root which has the string value - 'C:\Projects\OBC\Clients\ABC'. When I try to run the pkg, I get the following error:
Error: The expression contains unrecognized token "C". If "C" is a variable, it should be expressed as "#C". The specified token is not valid. If the token is intended to be a variable name, it should be prefixed with the # symbol.
Error: Attempt to parse the expression "C:\Projects\OBC\Clients\ABC" failed and returned error code 0xC00470A4. The expression cannot be parsed. It might contain invalid elements or it might not be well-formed. There may also be an out-of-memory error.
Now, this runs fine in the 2008 version of the Business Intelligence studio. I don't know how to specify the variable name. Please help me if possible. Thanks
The SSIS expression language is a C based language and the \ is a token, this means you have to escape it with another one. i.e "\" becomes "\", unlike C# you can't prefix the string with a #, you have to use the escaping route.
In summary when ever you want to use \ you need to use two \
Why use the expression though when you can set the value directly in the values column for the variable - without the quotes or double slashes - Just - C:\Projects\OBC\Clients\ABC

Incorrect syntax near ''

I'm trying to run the following fairly simple query in SQL Server Management Studio:
SELECT TOP 1000 *
FROM
master.sys.procedures as procs
left join
master.sys.parameters as params on procs.object_id = params.object_id
This seems totally correct, but I keep getting the following error:
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near ''.
It works if I take out the join and only do a simple select:
SELECT TOP 1000 *
FROM
master.sys.procedures as procs
But I need the join to work. I don't even have the string '' in this query, so I can't figure out what it doesn't like.
Such unexpected problems can appear when you copy the code from a web page or email and the text contains unprintable characters like individual CR or LF and non-breaking spaces.
Panagiotis Kanavos is right, sometimes copy and paste T-SQL can make appear unwanted characters...
I finally found a simple and fast way (only Notepad++ needed) to detect which character is wrong, without having to manually rewrite the whole statement: there is no need to save any file to disk.
It's pretty quick, in Notepad++:
Click "New file"
Check under the menu "Encoding": the value should be "Encode in UTF-8"; set it if it's not
Paste your text
From Encoding menu, now click "Encode in ANSI" and check again your text
You should easily find the wrong character(s)
The error for me was that I read the SQL statement from a text file, and the text file was saved in the UTF-8 with BOM (byte order mark) format.
To solve this, I opened the file in Notepad++ and under Encoding, chose UTF-8. Alternatively you can remove the first three bytes of the file with a hex editor.
You can identify the encoding used for the file (in this case sql file) using an editor (I used Visual studio code). Once you open the file, it shows you the encoding of the file at the lower right corner on the editor.
encoding
I had this issue when I was trying to check-in a file that was encoded UTF-BOM (originating from a non-windows machine) that had special characters appended to individual string characters
You can change the encoding of your file as follows:
In the bottom bar of VSCode, you'll see the label UTF-8 With BOM. Click it. A popup opens. Click Save with encoding. You can now pick a new encoding for that file (UTF-8)
I was using ADO.NET and was using SQL Command as:
string query =
"SELECT * " +
"FROM table_name" +
"Where id=#id";
the thing was i missed a whitespace at the end of "FROM table_name"+
So basically it said
string query = "SELECT * FROM table_nameWHERE id=#id";
and this was causing the error.
Hope it helps
I got this error because I pasted alias columns into a DECLARE statement.
DECLARE #userdata TABLE(
f.TABLE_CATALOG nvarchar(100),
f.TABLE_NAME nvarchar(100),
f.COLUMN_NAME nvarchar(100),
p.COLUMN_NAME nvarchar(100)
)
SELECT * FROM #userdata
ERROR:
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '.'.
DECLARE #userdata TABLE(
f_TABLE_CATALOG nvarchar(100),
f_TABLE_NAME nvarchar(100),
f_COLUMN_NAME nvarchar(100),
p_COLUMN_NAME nvarchar(100)
)
SELECT * FROM #userdata
NO ERROR
For me I was miss single quote in the statement
Incorrect One : "INSERT INTO Customers (CustomerNo, FirstName, MobileNo1, RelatedPersonMobileNo) VALUES ('John123', John', '1111111111', '1111111111)"
missed quote in John' and '1111111111
Correct One: "INSERT INTO Customers (CustomerNo, FirstName, MobileNo1, RelatedPersonMobileNo) VALUES ('John123', 'John', '1111111111', '1111111111')"
I was able to run this by replacing the 'Dot'; with and 'Underscore'; for the [dbo][tablename].
EXAMPLE:
EXEC sp_columns INFORMATION_SCHEMA.COLUMNS
GO //**this will NOT work. But will intelliSence/autocomplete as if its correct.
EXEC sp_columns INFORMATION_SCHEMA_COLUMNS
GO //**This will run in Synapse. but funny enough will not autocomplete.