Add generated column to an existing table Postgres - sql

I am trying to add a generated column to an existing table with this script.
alter table Asset_Store add column
md5_hash VARCHAR(100) GENERATED ALWAYS AS
(CAST(UPPER(
case
when OR_ID is not null then MD5(cast(OR_ID as varchar(100)))
when Asset_ID is not null then MD5(Asset_ID)
else null
end
) as VARCHAR(100)))
STORED
;
but I am getting an error:
SQL Error [42601]: ERROR: syntax error at or near "("
Position: 88
ERROR: syntax error at or near "("
Position: 88
ERROR: syntax error at or near "("
Position: 88
What is the issue? I don't get it.
In the schema of my Asset_Store table the column
OR_ID is int and Asset_ID is varchar(100).
I guess it expects a slightly different syntax... but what is the right syntax?

Your syntax is correct. Your version of PostgreSQL apparently is not.
In version 12:
create table asset_store(or_id text, asset_id text);
alter table Asset_Store add column
md5_hash VARCHAR(100) GENERATED ALWAYS AS
(CAST(UPPER(
case
when OR_ID is not null then MD5(cast(OR_ID as varchar(100)))
when Asset_ID is not null then MD5(Asset_ID)
else null
end
) as VARCHAR(100)))
STORED
;
ALTER TABLE
Time: 17.678 ms

More general, simplified command
ALTER TABLE "items"
ADD COLUMN "revenue" numeric
GENERATED ALWAYS AS ("price" * (1-"discount")) STORED;

Related

Alter column length in SQL

ALTER TABLE tableName
MODIFY COLUMN columnName VARCHAR (256);
Error :
Incorrect syntax near 'VARCHAR'
The syntax for modifying a column definition is depending on used database. For Microsoft SQL Server for example, you would need to replace MODIFY COLUMN with ALTER COLUMN.
For better help you should specify your database type.

Why can't I modify column type in Postgres

I am trying to change type of columns from varchar to integer. When I run the following command
alter table audio_session
alter column module_type_cd type INT USING module_type_cd::integer,
alter column sound_type_cd type INT USING sound_type_cd::integer
I got the error:
ERROR: invalid input syntax for integer: "string"
SQL state: 22P02
The error does not make any sense. There is no "string" in the command at all. What did I do wrong?
The columns audio_session.module_type_cd or audio_session.sound_type_cd has at least one non-numeric value among all values of each row. So, it's impossible to convert to a completely numeric data type.
As an example :
create table Table1( col1 varchar(10), col2 varchar(10) );
insert into Table1 values('1','1');
insert into Table1 values('2','2');
insert into Table1 values('3','C3');
select CAST(coalesce(col1, '0') AS integer) as converted from Table1; -- this works
select CAST(coalesce(col2, '0') AS integer) as converted from Table1; -- but, this doesn't
SQL Fiddle Demo
There is no "string" in the command at all.
But must be in the data. The command you're going to use tries to cast all the columns values to integer. Check this:
select *
from audio_session
where module_type_cd = 'string' or sound_type_cd = 'string';

DELETE with having dynamic fields in condition clause seems not working for phoenix

I am trying to delete rows in apache phoenix by specifying dynamic fields.
Here is an example:
Say we create "test" table with schema below:
create table "test" ("id" INTEGER not null primary key, "staticColumn" VARCHAR);
and insert one row like this
upsert into "test" ("id", "staticColumn", "dynamicColumn" VARCHAR) values (0, 'static', 'dynamic');
Then according to the documentation, I do can search what by:
select * from "test"("dynamicColumn" VARCHAR) where "dynamicColumn" = 'dynamic';
but I can only delete by:
delete from "test" where "staticColumn" = 'static';
NOT by:
delete from "test"("dynamicColumn" VARCHAR) where "dynamicColumn" = 'dynamic';
Error message I got:
Error: ERROR 602 (42P00): Syntax error. Missing "EOF" at line 1, column 19. (state=42P00,code=602)
org.apache.phoenix.exception.PhoenixParserException: ERROR 602 (42P00): Syntax error. Missing "EOF" at line 1, column 19.
at org.apache.phoenix.exception.PhoenixParserException.newException(PhoenixParserException.java:33)
at org.apache.phoenix.parse.SQLParser.parseStatement(SQLParser.java:111)
at org.apache.phoenix.jdbc.PhoenixStatement$PhoenixStatementParser.parseStatement(PhoenixStatement.java:1285)
at org.apache.phoenix.jdbc.PhoenixStatement.parseStatement(PhoenixStatement.java:1366)
at org.apache.phoenix.jdbc.PhoenixStatement.execute(PhoenixStatement.java:1429)
at sqlline.Commands.execute(Commands.java:822)
at sqlline.Commands.sql(Commands.java:732)
at sqlline.SqlLine.dispatch(SqlLine.java:808)
at sqlline.SqlLine.begin(SqlLine.java:681)
at sqlline.SqlLine.start(SqlLine.java:398)
at sqlline.SqlLine.main(SqlLine.java:292)
Caused by: MissingTokenException(inserted [#-1,0:0='<missing EOF>',<-1>,1:18] at ()
at org.apache.phoenix.parse.PhoenixSQLParser.recoverFromMismatchedToken(PhoenixSQLParser.java:350)
at org.antlr.runtime.BaseRecognizer.match(BaseRecognizer.java:115)
at org.apache.phoenix.parse.PhoenixSQLParser.statement(PhoenixSQLParser.java:510)
at org.apache.phoenix.parse.SQLParser.parseStatement(SQLParser.java:108)
... 9 more
You should file a bug report or feature request with them, but as a workaround:
delete from test where id in (select id from test(dynamicColumn VARCHAR) where dynamicColumn = 'dynamic');
It works with or without double quotations around the column name, which were unrelated to the error message.

How to create Temporary table with AS in postgresql?

Create Temporary Table Temp3 (
user_id int not null,
) AS query(
select id
from users
)
I use the above code in postgresql but it says:
ERROR: syntax error at or near ")"
LINE 3: ) AS (
^
****** Error ******
ERROR: syntax error at or near ")"
SQL state: 42601
Character: 55
How can I fix it to do the same job? Thx ahead!
It is much simpler than your try:
create temporary table Temp3 as
select id as user_id
from users;
SeeCREATE TABLE AS

Issue with setting default value for a column (sql server 2005)

The following syntax won't work:
ALTER TABLE MyCustomers
ALTER COLUMN CompanyName SET DEFAULT 'A. Datum Corporation'
per url, http://msdn.microsoft.com/en-us/library/ms174123.aspx
Error message:
Incorrect syntax near the keyword 'set'.
What gives?
Thanks.
Per this question:
How to set a default value for an existing column
The following will work on SQL Server, and has the added benefit of naming the constraint:
ALTER TABLE MyCustomers
ADD CONSTRAINT DF_SomeName DEFAULT 'A. Datum Corporation' FOR CompanyName;