I have a database that uses "-" in it's columns names.
Example
system-test-id
I mapped the table in Hibernate, but when I try to select all, for example, I get this error:
Invalid column name "system"
Notice that only the first word is taken as column name.
Option show_sql in hibernate shows me this:
select this_.system-test-id as system1_0_0_ (...)
EDIT
I had to add \" in the column name on mapping:
#Id
#Column(name="\"system-test-id\"")
private long systemTestId;
#Column(name="\"system-test-id\"") is the JPA defined way to handle quoted identifiers.
Hibernate has a little more friendly syntax using batck-ticks: #Column(name="system-test-id")
The back-ticks (`) or embedded double-quotes indicate the identifier should be quoted and are replaced with dialect-specific identifier quoting.
Please check the difference between
create table #t
(
[id-Column] int
)
and
create table #t
(
id-Column int
)
Related
I am working on a mySQL to PostgreSQL database migration using pgloader. One of the issues I am facing is that my application is looking for any tables beginning with "ao_" to be "AO_" which I was able to solve by making them all uppercase, however the corresponding columns also need to be uppercase.
Is there a good way to make JUST the "AO_" table columns be all uppercase. It does not seem very efficient to just do this for 400 tables with approximately 10 columns per table:
ALTER TABLE "AO_54307E_QUEUE" RENAME project_id TO "PROJECT_ID";
Is there maybe some kind of wildcard we could use to just grab the "AO_" tables and then have all the columns be uppercase?
I would recommend you against doing it, and I am quoting from the documentation.
Quoting an identifier also makes it case-sensitive, whereas unquoted
names are always folded to lower case.
If you want to write portable applications you are
advised to always quote a particular name or never quote it.
So, quoting "JUST the "AO_" table columns be all uppercase" seems like a bad idea.
If you still wish to proceed, you may use a loop through information_schema.columns and run dynamic ALTER statements.
DO $$
DECLARE
rec RECORD;
BEGIN
for rec IN ( SELECT column_name,table_name,table_schema
FROM information_schema.columns
WHERE table_name like 'AO_%'
AND column_name like 'ao_%' )
LOOP
EXECUTE format ( 'ALTER TABLE %I.%I RENAME %I TO %I' ,
rec.table_schema,rec.table_name,rec.column_name,
upper(rec.column_name)) ;
RAISE NOTICE 'COLUMN % in Table %.% RENAMED',
rec.column_name,rec.table_schema,rec.table_name;
END LOOP;
END$$;
Demo
CREATE TYPE pencil_count AS(
pencil_color varchar(30),
count integer
);
CREATE TABLE pencils(id serial, pencils_ pencil_count[]);
INSERT INTO pencils(pencils_) VALUES('{("blue",5),("red",2)}');
This doesn't work and gives error:
Malformed array literal.
What would be the correct syntax if I want to add this composite array without using ARRAY[...]?
Advice so far is not optimal. There is a simpler solution and an actually applicable explanation.
When in doubt, just ask Postgres to show you:
CREATE TEMP TABLE pencil_count ( -- table also registers row type
pencil_color varchar(30)
, count integer
);
CREATE TEMP TABLE pencils (
id serial
, pencils_ pencil_count[]
);
Insert 2 basic rows:
INSERT INTO pencil_count VALUES ('red', 1), ('blue', 2);
See the syntax for the basic row type:
SELECT p::text AS p_row FROM pencil_count p;
p_row
----------
(red,1)
(blue,2)
See the syntax for an array of rows:
SELECT ARRAY(SELECT p FROM pencil_count p)::text AS p_row_arr;
p_row_arr
------------------------
{"(red,1)","(blue,2)"}
All you need is to enclose each row literal in double quotes - which is only necessary to disable the special meaning of the comma within each row type.
Additional (escaped) double quotes would be redundant noise while there are no additional special characters.
None of this has anything to do with escape string syntax, which has been turned off by default since Postgres 9.1. You would have to declare escape string syntax explicitly by prefixing E, like E'string\n'. But there is no good reason to do that.
db<>fiddle here
Old sqlfiddle
Related answer with more explanation:
Insert text with single quotes in PostgreSQL
How to pass custom type array to Postgres function
PL/pgSQL Array of Rows
I want to add this composite array without using ARRAY
You could use:
INSERT INTO pencils(pencils_)
VALUES('{"(\"blue\",5)","(\"red\",2)"}');
db<>fiddle demo
Row type
Remember that what you write in an SQL command will first be interpreted as a string literal, and then as a composite. This doubles the number of backslashes you need (assuming escape string syntax is used).
The string-literal processor removes one level of backslashes.
The ROW constructor syntax is usually easier to work with than the composite-literal syntax when writing composite values in SQL commands. In ROW, individual field values are written the same way they would be written when not members of a composite.
I have an SQL SELECT query that's grabbing some data from my database. I need to replace a certain word that contains a semicolon in my SELECT query. Exactly this:
REPLACE(Table.Field,'"','') AS Field1
The error I'm getting reads
Unclosed quotation mark after the character string '"'.
So I think the semicolon is terminating the query. How can I escape that semicolon?
I tried backslashes and using double quotes.
Some sample data and expected output, as requested
Sample data
Field
"Hello"
"Goodbye"
Expected output
Field1
Hello
Goodbye
Full Query
SELECT REPLACE(Table.Name,';','') AS Name,
SUM(Table.Quantity) AS Quantity,
SUM(Table.Price*Table.Quantity) AS Price
FROM Table
GROUP BY Name
The ; symbol doesn't terminate the query and it should not be escaped, if it is part of the string literal (the text enclosed in single quotes ').
Here is a complete example that demonstrates that it works fine in SSMS:
CREATE TABLE #TempTable (Name varchar(50));
INSERT INTO #TempTable (Name) VALUES('Field');
INSERT INTO #TempTable (Name) VALUES('"Hello"');
INSERT INTO #TempTable (Name) VALUES('"Goodbye"');
SELECT
Name
,REPLACE(Name,'"','') AS ReplacedName
FROM #TempTable;
DROP TABLE #TempTable;
This is the result set:
Name ReplacedName
---- ------------
Field Field
"Hello" Hello
"Goodbye" Goodbye
You didn't provide all details of how you construct and execute your query, so I have a guess. It looks like you are:
building the text of the query dynamically
use some web-based tools/languages/technologies for that
web-based text processing tool/language that you use parses the text of your SQL query as if it was HTML and interferes with the result. For one thing, it changes " to the " symbol.
during all this processing you end up with unmatched ' symbol in the text of your SQL. It could come from the user input that you concatenate to your query of from a value stored in your database.
it has nothing to do with the ; symbol. Your error message clearly states that the matching quotation mark (which is ') is missing after the " symbol.
To understand what is going on you should print out the text of the actual SQL query that is sent to the server. Once you have it, it should become obvious what went wrong. I don't think that the Full Query that you put in the question is the real query that you are trying to run. It has syntax error. So, get the real thing first.
This works fine for me
declare #a as nvarchar(50) = '"Hello"'
select REPLACE(#a,'"','') AS Field1
declare #b as nvarchar(50) = '"Goodbye"'
select REPLACE(#b,'"','') AS Field1
Error message says unclosed quotation mark ?
Do you have single quotes in few of your fields ?
In that case you can replace them first as below
REPLACE(Table.Field,'''','') AS Field1
Let me know you need more help with this.
Source
"
the double quote sign "
I think there is no where that this parameter is known as a special phrase that refers to " and cause you error message.
In SQL Server there is just a function like QUOTENAME ( 'character_string' [ , 'quote_character' ] ) that used like this: -Just for ' or " or []-
SELECT QUOTENAME('Sample', '"') --> result is `"Sample"`
SELECT QUOTENAME('Sam"ple', '"') --> result is `"Sam""ple"`
In SQL Server identifiers can be delimited by ", When SET QUOTED_IDENTIFIER is ON -for following the ISO rules-. When SET QUOTED_IDENTIFIER is OFF, identifiers cannot be quoted and must follow all Transact-SQL rules for identifiers. Literals can be delimited by either single or double quotation marks.
I suggest you using SET QUOTED_IDENTIFIER OFF that make sure, that you've not identifier between " in your query.
Note:
When a table is created, the QUOTED IDENTIFIER option is always stored as ON in the table's metadata even if the option is set to OFF when the table is created.
If you are using a SQL string I suggest this syntax:
REPLACE(Table.Field, CHAR(34), '') As Field1
or
REPLACE(REPLACE(Table.Field, ';', '.'), '".', '') As Field1
I am trying to run a simple query over jdbc ALTER TABLE Customer ALTER \"Cust-Name\" set PRO_DESCRIPTION 'Customer Name'
This works perfectly well. But, when I have to set description as "Customer's Name", i.e, include a single quote - I am unable to get it to work.
I tried
ALTER TABLE Customer ALTER \"Cust-Name\" set PRO_DESCRIPTION 'Customer~'s Name'
ALTER TABLE Customer ALTER \"Cust-Name\" set PRO_DESCRIPTION 'Customer~~'s Name'
ALTER TABLE Customer ALTER \"Cust-Name\" set PRO_DESCRIPTION 'Customer\\'sName'
ALTER TABLE Customer ALTER \"Cust-Name\" set PRO_DESCRIPTION "Customer's Name"
Nothing works.
I don't know Progress, but the SQL standard is to duplicate the single quote:
'Customer''s Name'
While I was learning Progress I encountered a function called QUOTER that can be used in your situation.
QUOTER function
Converts the specified data type to CHARACTER and encloses the results
in quotes when necessary.
The QUOTER function is intended for use in QUERY-PREPARE where a
character predicate must be created from a concatenated list of string
variables to form a WHERE clause. In order to process variables,
screen values, and input values so that they are suitable for a query
WHERE clause, it is often necessary to enclose them in quotes. For
example, European-format decimals and character variables must always
be enclosed in quotes. You can use the Quoter function to meet that
requirement.
When I run this
ALTER TABLE agency
ADD COLUMN single_word varchar(100)
I get
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'COLUMN'.
I tried removing the COLUMN but still same problem.
For TSQL Flavor try this syntax:
ALTER TABLE agents
ADD [associated department] varchar(100)
I have same issue when running that query on HeidiSQL. The solution is simple, change the query to be like this:
ALTER TABLE "agency"
ADD "single_word" varchar(100)
just remove "COLUMN" keyword.
Depending on the database software you are using, if you want to have a space in the column name (which I would recommend against), you will have to escape it.
For example, in MySQL, you would use the backtick (the character to the left of the number 1 at the top of the keyboard) :
ALTER TABLE agents
ADD COLUMN `associated department` varchar(100);
For SQL Server, you can use [], and for most other DBMSes, the double-quote (") will escape identifiers