SQL update statement with default as column name - mysql

I've got an SQL code to update values in a column.
I need to find one query to work for MS SQL and MySQL.
The main problem is that the columns which I'm using are Default and Type which are saved names in the SQL and therefore it doesn't work with the normal update statements.
I've found the following solutions, but I would like to make one query for both -
--Clearing Data logs Defualt MS SQL
UPDATE queries
SET [Default] = 0
FROM queries
WHERE [Type] = 4
--Clearing Data logs Defualt MySQL
UPDATE queries q
SET q.Default = 0
WHERE q.TYPE = 4
Thanks a lot for the help!

You've got to enable ANSI quotes for both servers. If you do that you could use double quotes to quote your identifiers.
For MySQL:
SET sql_mode = 'ANSI_QUOTES'
Treat “"” as an identifier quote character (like the “” quote
character) and not as a string quote character. You can still use “”
to quote identifiers with this mode enabled. With ANSI_QUOTES enabled,
you cannot use double quotation marks to quote literal strings,
because it is interpreted as an identifier.
For MS SQL Server.
SET QUOTED_IDENTIFIER ON
Causes SQL Server to follow the ISO rules regarding quotation mark
delimiting identifiers and literal strings. Identifiers delimited by
double quotation marks can be either Transact-SQL reserved keywords or
can contain characters not generally allowed by the Transact-SQL
syntax rules for identifiers.
Now you can write the single query that works for both, MS SQL Server and MySQL:
UPDATE queries q
SET q."Default" = 0
WHERE q."TYPE" = 4
And before I forget it: best way out of such problems is to avoid reserved words as identifiers, see solution 1. You've got to avoid reserved words of all involved worlds (T-SQL and the sql dialect of MySQL).

Related

Escaping hyphen in MySQL query (not using a backtick)

We have an existing schema we're trying to fit some quartz tables into, but the tables are named with hyphen in them, so we'd like to use a prefix like "08-Scheduling_QUARTZ_"
Since quartz doesn't wrap any of the queries in back ticks, the prefix doesn't work.
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '08-Scheduling_QUARTZ_TRIGGERS SET TRIGGER_STATE = 'WAITING' WHERE SCHED_NAME' at line 1]]
Curious if there is any chance there is some other way to escape the "-" in a mysql query other than `` around the whole table name?
I've tried
x'-'x
x\-x
x"-"x
x`-`x
No, you must delimit the identifier if it has certain punctuation characters.
In MySQL, the default identifier delimiter is the back-tick.
If you enable the ANSI or ANSI_QUOTES SQL modes, you can use double-quotes as an identifier delimiter.
If you don't want to use delimiters, you must choose a different convention for prefixing your table names. You could use _ for example.
Read https://dev.mysql.com/doc/refman/8.0/en/identifiers.html for more details on the characters permitted in identifiers without delimiters.

MySQL statement fails due to encoded quotes

Following on from this question MySQL database contains quotes encoded and unencoded and it's breaking javascript
I am executing this MySQL query:
DELETE FROM `example` WHERE `name` = ''12345''
However it fails because the value in the database is '12345'. It seems that old data in the database has a mixture of encoded and unencoded quotes. Is it safe to to update all ' to ' in the database?
In most cases (yours included), store text without any "encoding". That is, do not store htmlentities, store the actual characters, do not store unicode 'codes', store the actual characters, etc.
Do likewise for anything you need to compare to what is in the database.
You will, however, have to escape strings when building SQL statements. Otherwise, you can't get quotes (in text) inside quotes (that are part of the SQL syntax.
That is, you will end up with this SQL when searching for that Irishman:
... WHERE `name` = 'O\'Brian'

Escaping a forward slash in an SQL name? It can be "escaped", but SQL believes it to be multiple columns

The last person in my job has flooded column names with special characters such as (?,!, and /), as well as used many reserved keywords for column names (more often than not, timestamp or user is used).
Normally, I step around this by using double quotes or brackets to escape the SQL object. A subset of the full list of columns are below:
DriverID,
Department,
Odometer,
MerchantState,
MerchantCity,
gallons/Units,
timestamp,
tax
Inside my query, I wrap the two columns in question (gallons/units and timestamp) inside double quotes. Timestamp because it's a reserved keyword, and Gallons/units, because without the quotes, SQL reads the query, stops at the slash, and tells me "Gallons" is not a column inside the table.
If I do wrap double quotes around the column name, SQL returns a different error: "Operand should contain 1 column(s)".
I've tried every variant (only capturing the slash in quotes, quoting both, using brackets, mixing brackets and quotes, etc. but with to no avail).
Is there anything I can do to fix this query short of renaming the column name and changing the associated code in the program that pulls from it? (the really tedious task I'm trying to avoid).
In SQL Server, identifiers can be delimited using square brackets, e.g.
SELECT [gallons/units] ...
In MySQL, identifiers can be delimited using backticks, e.g.
SELECT `gallons/units` ...
(NOTE: If MySQL SQL_MODE includes ANSI_QUOTES, then double quotes are treated as delimiters for identifiers, similar to the way Oracle handles double quotes; absent that setting, double quotes are handled as delimiters for string literals. With ANSI_QUOTES included SQL_MODE, "gallons/units" will be interpreted as an identifier (column name). Without ANSI_QUOTES, MySQL will see it as a string literal, as if it were enclosed in single quotes.)
FOLLOWUP:
As far as an error "operand should contain only 1 column(s)", that's usually a problem with query semantics, not an issue with escaping identifiers.
A subquery in the SELECT list can return only a single expression, for example, this would throw an error:
Query: SELECT 'foo' , ( SELECT 'fee' AS fee, 'fi' AS fi )
Error Code: 1241
Operand should contain 1 column(s)
You can try backticks instead of double quotes
`gallons/units`
There are a couple of options. First, have you tried using %/ to escape the slash?
Example: "select * from 'gallons%/units';"
Second one I've found, which may or may not be helpful, is to provide an escape character definition, such as
http://blogs.msdn.com/b/zainala/archive/2008/08/17/using-and-escape-clause-in-sql-server-like-query.aspx
select * from MyTable where Description like '|[summary|]%' escape '|'
In your case
select * from 'gallons|/units' escape '|'
You indicate both mysql and sql-server in your tags, so I'm not sure which server support I should be looking for exactly.

Slick - MySQL Syntax Error

I'm using the Play Framework (2.3.1) together with Slick (play-slick version 0.8.0-M1) and a MySQL Database (5.5.28).
One of my queries results in a MySQLSyntaxErrorException:
Preparing statement: select x2."id", x2."course_id", x2."trainee_id", x2."transaction_id" from "trainee_grouptraining_GroupBooking" x2 where x2."course_id" = 1
The problem appears to be with the double quotation marks, since other queries work just fine and they use single quotation marks like the following:
Preparing statement: select x2.`id`, x2.`courseLanguage`, x2.`date`, x2.`description`, x2.`duration`, x2.`kind`, x2.`maxParticipants`, x2.`name`, x2.`courseType_id`, x2.`trainer_id` from `Course` x2 where x2.`id` = 1
What can i do about this?
I guess you are importing
scala.slick.driver.JdbcDriver.simple._
You should import
scala.slick.driver.MySQLDriver.simple._
instead.
MySQL is using per default backticks to quote identifiers. You could use the SQL Mode ANSI_QUOTES to enable double quotes
ANSI_QUOTES
Treat " as an identifier quote character (like the ` quote
character) and not as a string quote character. You can still use “`”
to quote identifiers with this mode enabled. With ANSI_QUOTES enabled,
you cannot use double quotation marks to quote literal strings,
because it is interpreted as an identifier.
or simply use backticks.

Do different databases use different name quote?

For example, mysql quote table name using
SELECT * FROM `table_name`;
notice the `
Does other database ever use different char to quote their table name
This use of quotes is called delimited identifiers. It's an important part of SQL because otherwise you can't use identifiers (e.g. table names and column names) that:
Include whitespace: "my table"
Include special characters and punctuation: "my-table"
Include international characters: "私のテーブル"
Are case-sensitive: "MyTable"
Match SQL keywords: "table"
The standard SQL language uses double-quotes for delimited identifiers:
SELECT * FROM "my table";
MySQL uses back-quotes by default. MySQL can use standard double-quotes:
SELECT * FROM `my table`;
SET SQL_MODE=ANSI_QUOTES;
SELECT * FROM "my table";
Microsoft SQL Server and Sybase uses brackets by default. They can both use standard double-quotes this way:
SELECT * FROM [my table];
SET QUOTED_IDENTIFIER ON;
SELECT * FROM "my table";
InterBase and Firebird need to set the SQL dialect to 3 to support delimited identifiers.
Most other brands of database use double-quotes correctly.
SQL Server uses [square brackets] or "double quotes" when QUOTED_IDENTIFIER option is ON.
I believe double quotes are in the SQL-92 standard.
Succinctly, yes.
The SQL standard uses double quotes around the name to indicate a 'delimited identifier'.
Informix by default uses single and double quotes interchangeably to indicate character strings. However, by setting the environment variable DELIMIDENT you can turn on the SQL standard behaviour - single quotes around strings and double quotes around delimited identifiers.
Other people have listed other behaviours for other DBMS; I don't need to repeat those.