patternsqlMinor
Equivalent to PostgreSQL's Dollar-quoted String Constants for SQL Server
Viewed 0 times
postgresqlequivalentsqldollarquotedforconstantsserverstring
Problem
Is there an equivalent of PostgreSQL's Dollar-quoted String Constants, on SQL Server?
I would like to enter HTML string literals that would potentially contain single or double quotes in them.
Example:
Edit:
The reason I'm asking this is because I would like to update some rather big HTML "web parts" that are stored in the database (yuck!). Because at work we use that kind of CMS (which I won't give you the name). So I don't want to have to escape my single quotes every time. That's why I'm asking about that kind of feature. If I understand correctly,
I would like to enter HTML string literals that would potentially contain single or double quotes in them.
Example:
UPDATE table_name
SET column_name = $
Here's a string that contains "double quotes".
$
WHERE condition = trueEdit:
The reason I'm asking this is because I would like to update some rather big HTML "web parts" that are stored in the database (yuck!). Because at work we use that kind of CMS (which I won't give you the name). So I don't want to have to escape my single quotes every time. That's why I'm asking about that kind of feature. If I understand correctly,
QUOTENAME would still require me to double my single quotes.Solution
This is a PostgreSQL extension of the spec. There is nothing like it in SQL Server. The idea is that you can quote everything that doesn't have the
Dollar quoting is not part of the SQL standard, but it is often a more convenient way to write complicated string literals than the standard-compliant single quote syntax. It is particularly useful when representing string constants inside other constants, as is often needed in procedural function definitions. With single-quote syntax, each backslash in the above example would have to be written as four backslashes, which would be reduced to two backslashes in parsing the original string constant, and then to one when the inner string constant is re-parsed during function execution.
So you can pass something like this,
or, even this.
And we do that frequently with modules like
As a workaround, in SQL Server, you just have to be escape the single quotes and double escape the single-quotes in the single quotes. This is the standard method of literal-quoting.
The
$$[token]$$. If the optional token is absent it looks like $$. Thus the name, Dollar-quoted String ConstantsDollar quoting is not part of the SQL standard, but it is often a more convenient way to write complicated string literals than the standard-compliant single quote syntax. It is particularly useful when representing string constants inside other constants, as is often needed in procedural function definitions. With single-quote syntax, each backslash in the above example would have to be written as four backslashes, which would be reduced to two backslashes in parsing the original string constant, and then to one when the inner string constant is re-parsed during function execution.
So you can pass something like this,
$ SELECT foo FROM "mySchema"."myTable" WHERE bar='foo''bar'; $or, even this.
$OUTER$ SELECT foo FROM "mySchema"."myTable" WHERE bar=$foo'bar$; $OUTER$And we do that frequently with modules like
tablefuncAs a workaround, in SQL Server, you just have to be escape the single quotes and double escape the single-quotes in the single quotes. This is the standard method of literal-quoting.
' SELECT foo FROM "mySchema"."myTable" WHERE bar=''foo''''bar''; 'The
'''' is what we're trying to avoid here. That's fugly.Code Snippets
$$ SELECT foo FROM "mySchema"."myTable" WHERE bar='foo''bar'; $$$OUTER$ SELECT foo FROM "mySchema"."myTable" WHERE bar=$$foo'bar$$; $OUTER$' SELECT foo FROM "mySchema"."myTable" WHERE bar=''foo''''bar''; 'Context
StackExchange Database Administrators Q#191178, answer score: 3
Revisions (0)
No revisions yet.