Concatenate string in ALTER statement in REDSHIFT (syntax error) - sql

I want alter an external table in redshift. If execute this query:
alter table "name_table"
set location 'a string' ||
TO_CHAR((getdate()-1)::date, 'YYYYMMDD') || '/';
give me a syntax error:
SQL Error [500310] [42601]: Amazon Invalid operation: syntax error at or near "||"
Position: 135;
Maybe I must use dynamic SQL but I'm not sure how do that. Can someone help me?
EDIT:
I try to use a procedure for alter an external table, this doesn't give me a syntax error but return an error like this:
You can't alter an external table with function or procedure.

Do you want update?
update "name_table"
set location = 'a string' || TO_CHAR((getdate()-1)::date, 'YYYYMMDD') || '/';

I don't think location can be dynamic. Though you issue this statement in SQL, you can't use standard operators to build the S3 path. It's the same limitation as in good old COPY TO statement (target file can't be dynamic).

Related

PLS-00103: Encountered the symbol "CREATE" when expecting one of the

I am trying to create a stored procedure in oracle 12c database and I am getting error when I am running code to store the procedure.
PLS-00103: Encountered the symbol "CREATE" when expecting one of the
There are multiple stack overflow question already asked on this topic. but they suggest some different syntax. which is deviation from actual oracle documentation. and even those didn't worked for me
I checked for documentation on multiple website including oracle documentation. oracle documetation suggest syntax as following
Oracle STORED PROCEDURE DOCUMENTATION
so I as per the syntax I wrote the following procedure.
CREATE PROCEDURE PDD_PROC_BASE
AS
--DROP TABLE BASE;
CREATE TABLE BASE as
SELECT idno
,DATE
,diff
,SUBSTR(idNO,7,2) AS PRD
,COMPLETED
,CATG
,OP_Number
FROM table1
WHERE = date >= '30-JUN-2018'
AND STATUS = 'G'
END;
and I got the following error.
Procedure PDD_PROC_BASE compiled
Errors: check compiler log
Errors for PROCEDURE AN_5043152.PDD_PROC_BASE:
LINE/COL ERROR
-------- ------------------------------------------------------------------------------
7/5 PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:
( begin case declare exit for goto if loop mod null pragma
raise return select update while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge
json_exists json_value json_query json_object json_array
I checked other resources as well but still didn't understood what went wrong.
I even tried code example from Oracle documentation and got similar error.
I am using SQLdeveloper tool as client
You cant use directly sql ddl statements in plsql block, you can do the same thing using dynamic sql with the "EXECUTE IMMEDIATE" statement like this:
begin
execute immediate 'create table test_table1 (test_column1 varchar2(40))';--your create table statement here
end;
In Oracle in stored procedures you can use DDL statements only as dynamic SQL, that means as EXECUTE IMMEDIATE 'CREATE TABLE ' ...
Check here, for example: http://www.dba-oracle.com/t_using_ddl_create_index_table_plsql.htm

getting 'FROM' error in oracle query

I'm getting this error on the below query. The query is to modify a constraint
SELECT 'EXEC DROP_CONSTRAINTS('RTK_TYUVOICE_SYSTEM','IOA_WRTYOICE');' FROM DUAL
*
ERROR at line 1:
ORA-00923: FROM keyword not found where expected
Please advise on how to overcome this when I execute the above query from sql/nolog
Either double quotes in the string
SELECT 'EXEC DROP_CONSTRAINTS(''RTK_TYUVOICE_SYSTEM'',''IOA_WRTYOICE'');' as txt FROM DUAL;
TXT
------------------------------------------------------------
EXEC DROP_CONSTRAINTS('RTK_TYUVOICE_SYSTEM','IOA_WRTYOICE');
or use the (from 10g) quoted string
SELECT q'[EXEC DROP_CONSTRAINTS('RTK_TYUVOICE_SYSTEM','IOA_WRTYOICE');]' as txt FROM DUAL;
TXT
------------------------------------------------------------
EXEC DROP_CONSTRAINTS('RTK_TYUVOICE_SYSTEM','IOA_WRTYOICE');
You are trying to modify a constraint using a statement I hope, and are not looking to just print your statement.
In that case, you need to make use of Execute Immediate statement, which helps to execute DDL statements on the fly in a procedure.
BEGIN
EXECUTE IMMEDIATE 'ALTER TABLE ANAL2 DROP CONSTRAINT SYS_C008611' ;
END;
/
Any valid DDL statement (like create, alter, drop etc) can be squeezed in the EXECUTE IMMEDIATE statement, using their correct syntactic formation.
Alternatively, you could just mention, if the given SQL is static, as
ALTER TABLE ANAL2 DROP CONSTRAINT SYS_C008611

How to use TQuery with Oracle SQL syntax for variable assignments and Params?

In a regular Query on Oracle SQL we could use:
var_user VARCHAR2(256) := 'eduard'
select * from a_table where user_name = var_user
If I use this code in Delphi's TQuery.SQL.Text I get the error Incorrect Token Followed By ":"..
I believe it is happening because Delphi uses : symbol in this case to specify a parameter inside TQuery.SQL.Text to be replaced by the values set at TQuery.Params[].
What I really want to do is SomeVarInsideSQL := :MyParam and I can't because of that error. What should I do? (delphi-xe3-zeoslib)
You need to escape : to :: if you have to bypass specific TQuery Parameter parsing.

CREATE SCHEMA using a name stored

I am trying to create a new schema on my postgres database which name is stored on an existing table, my query look like this:
CREATE SCHEMA (SELECT name FROM table)
But I am getting a syntax error. What am I doing wrong? Is this a valid way to create the new schema? Which other solution exist for this issue?
You can do it with dynamic sql:
do
$$
declare s_name text;
begin
-- make sure there is exactly one row in that table!
-- otherwise you need some where condition or an aggregat to ensure that.
SELECT name INTO s_name FROM some_table;
execute 'create schema '|| quote_ident(s_name);
end;
$$
It is not possible, create schema syntax required a valid schema name (i.e. valid schema name string). in your case it will throw exception as
********** Error **********
ERROR: syntax error at or near "("
SQL state: 42601
Character: 15

Spatial Data SQL Reprojection Function issues

Hello I am just learning postGIS and thus postgresql (9.1) and am trying to save some time copying the same code over and over by creating an sql function to reproject some spatial data.
Create Function reproject_shapefile(text,text,numeric) returns void as $$
-- Reprojects shapefiles given that they follow the pattern "gid * the_geom"
CREATE TABLE $2 AS
SELECT *, ST_Transform(the_geom,$3) AS the_geom2
FROM $1;
Alter table $2 add Primary Key (gid);
Alter table $2 drop column the_geom;
Alter table $2 rename column the_geom2 to the_geom;
$$ Language SQL;
I read over the docs specifying how to do this, but everytime I try to create the function from the sql editor in pgAdmin, I receive the following error:
ERROR: syntax error at or near "$2"
LINE 5: CREATE TABLE $2 AS
^
********** Error **********
ERROR: syntax error at or near "$2"
SQL state: 42601
Character: 175
Unlike the error messages in python, this tells me absolutely nothing of use, so I am hoping that someone can point me in the right direction on how to fix this error.
If there is some way to perform this same function using python feel free to post that as a solution instead/ as well, as python syntax is much easier for me to understand than ancient SQL.
Any help would be greatly appreciated!
You can not write dynamic SQL in this form. Parameters can only pass in values, not identifiers. Something like this is impossible in an SQL function:
CREATE TABLE $2 AS
You need to write a plpgsql function for that and use EXECUTE. Could look like this:
CREATE OR REPLACE FUNCTION reproject_shapefile(text, text, numeric)
RETURNS void as $$
BEGIN
EXECUTE '
CREATE TABLE ' || quote_ident($2) || ' AS
SELECT *, ST_Transform(the_geom,$1) AS the_geom2
FROM ' || quote_ident($1)
USING $3;
EXECUTE 'ALTER TABLE ' || quote_ident($2) || ' ADD PRIMARY KEY (gid)';
EXECUTE 'ALTER TABLE ' || quote_ident($2) || ' DROP COLUMN the_geom';
EXECUTE 'ALTER TABLE ' || quote_ident($2) || ' RENAME column the_geom2 TO the_geom';
END;
$$ Language plpgsql;
Major points
plpgsql function, not sql
EXECUTE any query with dynamic identifiers
use quote_ident to safeguard against SQLi
pass in values with the USING clause to avoid casting and quoting madness.