Openedge progress - Can't escape single quote - Is this a bug? - database

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.

Related

PostgreSQL Query to change columns to uppercase

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

alternative to single quote in MSSQL query

My question may be silly.
I have an application that does not like single quotes.
I want to execute SQL query from that application.
When I form my SQL query, I cannot use single quotes.
I am looking for alternative way for single quotes.
I am on MicroSoft SQL 2012.
Example, my SQL is like this,
SELECT name
FROM People
WHERE peopleId = '123'
However I want to write this without single quotes,
Something like below I was trying,
SELECT name
FROM People
WHERE peopleId = CHAR(39)123CHAR(39)
Thank you
If it accepts double quotes...
SET QUOTED_IDENTIFIER ON
GO
https://msdn.microsoft.com/en-us/library/ms174393.aspx
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. Literals can be delimited by either single or double quotation marks.
If your PeopleID or filter value is initially a numeric value you can do this. If it is not numeric I don't know.
SELECT name
FROM People
WHERE peopleId = CAST(123 AS VARCHAR(25))

How to set a function as a default value in SQL Server?

I am in trouble please help.
After searching for my question I decided to post here.
Look at my code below and tell me if I can set default value as a function.
What I want to do is to show first three characters of my first column's data which is Product_Name.
For example my first column has a name Xperia and and I want its first three characters to be shown in the last column which I have created using alter code.
I hope you understand what I say.
Xperia ----> Xpe
Code is :
alter table dummy
add new_col varchar(250) default substring(first_column, 1, 3)
returns an error:
Msg 128, Level 15, State 1, Line 2
The name "first_column" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
However I can set getdate() function as a default value.
Thanks
Why are you trying to alter the table? It sounds like you need a VIEW.
A view can add an additional field, selecting only the first three characters, and would not need to be updated each time you UPDATE or INSERT data. For example:
CREATE VIEW Table_With_Truncated_Product_Name
AS
SELECT *, LEFT(Product_Name, 3) AS truncated_product_name
FROM Your_Table
However, if you don't want to use a VIEW it may be possible to use a Computed Column.
In which case you would alter your table and add the field:
ALTER TABLE Your_Table ADD truncated_product_name AS LEFT(Product_Name, 3)
Although I don't have access to SQL Server at the moment to check that would work.

ORA-00911: invalid character error while truncating table

I have a table that name is _VERSION_HISTORY I got ORA-00911 error while truncating this table. Oracle allow the name start with underscore(_) but throws an error while truncating it. Is it a silly mistake?
Oracle does not allow database object names to start with an underscore:
SQL> create table _T34 (col1 number);
create table _T34 (col1 number)
*
ERROR at line 1:
ORA-00911: invalid character
SQL>
So you must have used double quotes when creating that table:
SQL> create table "_T34" (col1 number);
Table created.
SQL>
Having done that once you must use double quotes whenever you reference that object?
SQL> truncate table "_T34";
Table truncated.
SQL>
So is it "a silly mistake"? Yes, but alas on your part (or whoever decided on using double-quotes to circumvent Oracle's naming conventions). Find out more.
It seems like you have tried to execute SQL statement with a special charter in it. I don't think it is to do with the truncate command. You might just replace the truncate with a simple Select and test this scenario. Note that the special character might appear as white space due to the font you are using.

Hiberante and SQL Server Column name

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
)

Resources