"Syntax error at or near ' , '" while trying to SELECT INTO - sql

The query for selecting multiple values and assigning to multiple variables in a single SELECT query leads to an error. My Postgres version is 9.5.
The query is:
SELECT INTO region_id ,doc_type,tax_amt fk_bint_supplier_tax_region_id,chr_supporting_document_type,
dbl_base_currency_client_net-dbl_base_currency_market_fare-dbl_base_currency_cc_charge_collected+
dbl_base_currency_vat_in+dbl_base_currency_cc_charge_collected+(19*(dbl_base_currency_tax))*5/10
FROM tbl_sales_details WHERE chr_document_status='N' AND vchr_document_no='INV/47922/01/18'
AND vchr_supporting_document_no='5111143004'
The error is:
ERROR: syntax error at or near ","
LINE 1: SELECT INTO region_id ,doc_type,tax_amt fk_bint_supplier_ta...
^
********** Error **********
ERROR: syntax error at or near ","
SQL state: 42601

SELECT INTO in PL/pgSQL has a different meaning from
SELECT INTO in SQL. The latter is generally discouraged. The manual:
CREATE TABLE AS is functionally similar to SELECT INTO. CREATE TABLE AS
is the recommended syntax, since this form of SELECT INTO is not
available in ECPG or PL/pgSQL, because they interpret the INTO clause
differently. Furthermore, CREATE TABLE AS offers a superset of the
functionality provided by SELECT INTO.
The error message indicates you tried to run the statement as plain SQL.
There's nothing wrong with your placement of the INTO clause when used in PL/pgSQL like you tagged. You also stated that it's for:
assigning to multiple variables
That, too, only makes sense inside procedural language code as there are no variable assignments in plain SQL.
Related:
SELECT INTO with more than one attribution

You put the into after the column list:
SELECT region_id, doc_type,tax_amt fk_bint_supplier_tax_region_id, chr_supporting_document_type,
(dbl_base_currency_client_net - dbl_base_currency_market_fare -
dbl_base_currency_cc_charge_collected +
dbl_base_currency_vat_in + dbl_base_currency_cc_charge_collected + 19 * dbl_base_currency_tax
) * 5/10
INTO . . .
FROM tbl_sales_details
WHERE chr_document_status = 'N' AND
vchr_document_no = 'INV/47922/01/18' AND
vchr_supporting_document_no = '5111143004';
I don't know what the variable names are, but the go after the INTO and there must be one for each expression in the SELECT.

Related

ERROR: syntax error at or near "." LINE 4: ON like.takerId = frame.likeId;

i have a table whose name is like. But whenever i have to select data from like, i was getting this error, i figured it out public.like..but when i try to join two tables
SELECT *
FROM frame
INNER JOIN public.like
ON like.takerId = frame.likeId;
i get this error
ERROR: syntax error at or near "."
LINE 4: ON like.takerId = frame.likeId;
i also use public prefix but it throws
ERROR: column like.takerid does not exist
LINE 4: ON public.like.takerId = frame.likeId;
^
HINT: Perhaps you meant to reference the column "like.takerId".
even if it is saying column like.takerid does not exist , then why it gives me HINT: Perhaps you meant to reference the column "like.takerId". I dont know, i think it is problem with like table name, like is a sql syntax, and it assumes like and a sql syntax and throwing me error. Should I change my table name? Or is there any way to make sql case sensetive or how can i tell sql to ignore like. public.like is not working for joining table.
As like is a reserved keyword, you need to use double quotes for each occurance of it (unless it's prefixed with the schema name as you found out)
SELECT *
FROM frame
JOIN public.like ON "like".takerId = frame.likeId;
Or
SELECT *
FROM frame
JOIN "like" ON "like".takerId = frame.likeId;
Or use an alias
SELECT *
FROM frame f
JOIN "like" l ON l.takerId = f.likeId;
But in the long run you should find a different name for the table that does not require quoting.
You should definitely chose another name for your table. LIKE is a reserved command, and it is considered a bad practice to use it, although possible by using ", e.g.
CREATE TABLE public."like" (id int);
INSERT INTO public."like" VALUES (42);
SELECT * FROM "public.like"
EDIT: As pointed out by #a_horse_with_no_name, specifying a schema in temporary tables won't work (check db<>fiddle), so only the table name should be between double quotes as corrected in the snippet above. For temporary tables just omit the schema:
CREATE TEMPORARY TABLE "like" (id int);
INSERT INTO "like" VALUES (42);
SELECT * FROM "like"
Demo: db<>fiddle

MariaDB to calculate table reference in select query

I have a quite dumb client application that wants to get information from MariaDB based on a parameter.
This parameter is a string that contains spaces, like 'valid parameter'.
In MariaDB there is a table for each of the possible string values, and the table name is the string value after spaces have been replaced by underscores and a prefix is added. So I can perform the necessary conversion like this:
SELECT CONCAT('prefix_', REPLACE('valid parameter',' ','_'));
Now the result 'prefix_valid_parameter' is the table to query, so actually I need to fire off
SELECT * from CONCAT('prefix_', REPLACE('valid parameter',' ','_'));
but MariaDB responds with
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '('prefix_', REPLACE('valid parameter',' ','_'))' at line 1
I would have expected either the content of table 'prefix_valid_parameter' or an error stating "table 'prefix_valid_parameter' not found". How can I make the table_reference part of my SQL SELECT statement dynamic?
You need to use dynamic SQL:
set #sql = 'select * from [table]';
execute immediate replace(#sql, '[table]',
concat('prefix_', replace('valid parameter', ' ', '_'))
);
I should add that the need to do this perhaps suggests a flaw in your data model. If the tables have the same structure, it is better to put all the rows in a single table.

Execution error: [Vertica][VJDBC](4856) ERROR: Syntax error at or near "."

Trying to insert this activity into my table but getting the error seen below. Any thoughts? SQL query checkers keep telling me its in my insert statement but I'm not seeing any outlier on my end. Thanks!
INSERT INTO
"_CEL_MD_ACTIVITIES" ("_CEL_MD_ACTIVITIES"."CASE_KEY", "_CEL_MD_ACTIVITIES"."ACTIVITY_EN", "_CEL_MD_ACTIVITIES"."EVENTTIME")
SELECT
"_CEL_MD_CASE_TABLE"."CASE_ID" AS "_CEL_MD_ACTIVITIES"."CASE_KEY",
'Case Reopened' AS "_CEL_MD_ACTIVITIES"."ACTIVITY_EN",
"_CEL_MD_CASE_TABLE"."CASE_REOPEN_DATE" AS "_CEL_MD_ACTIVITIES"."EVENTTIME"
FROM
"_CEL_MD_CASE_TABLE"
JOIN
"_CEL_MD_ACTIVITIES"
ON "_CEL_MD_CASE_TABLE"."CASE_ID" = "_CEL_MD_ACTIVITIES"."CASE_KEY"
WHERE
"_CEL_MD_CASE_TABLE"."CASE_REOPEN_DATE" IS NOT NULL;
Execution error: [Vertica]VJDBC ERROR: Syntax error at or near
"."
You use as aliases the name and fields of a table involved in the join.
For example
AS "_CEL_MD_ACTIVITIES"."ACTIVITY_EN",
This is not correct. You should change your alias names and not use dots (.) in them or remove the usage of aliases

psql column doesn't exist but it does

I am trying to select a single column in my data table using raw SQL in a postgresql database from the psql command line. I am getting an error message that says the column does not exist. Then it gives me a hint to use the exact column that I referenced in the select statement. Here is the query:
SELECT insider_app_ownershipdocument.transactionDate FROM insider_app_ownershipdocument;
Here is the error message:
ERROR: column insider_app_ownershipdocument.transactiondate does not exist
SELECT insider_app_ownershipdocument.transactionDate FROM in...
HINT: Perhaps you meant to reference the column "insider_app_ownershipdocument.transactionDate".
I have no idea why this is not working.
(Postgres) SQL converts names automatically to lower case although it support case-sensitive names. So
SELECT insider_app_ownershipdocument.transactionDate FROM insider_app_ownershipdocument;
will be aquivalent to:
SELECT insider_app_ownershipdocument.transactiondate FROM insider_app_ownershipdocument;
You should protect the column name with double quotes to avoid this effect:
SELECT insider_app_ownershipdocument."transactionDate" FROM insider_app_ownershipdocument;

HIVE: cannot recognize input near 'distinct' '('

I am trying to execute the below query in Hive:
SELECT
regexp_replace('2016-08-05_11:29:46', '\\_', ' ') as tmstmp,
distinct(P.name)
FROM table P;
It throws an exception saying cannot recognize input near 'distinct' '(' 'P' in selection target.
where as when I run the query interchanging the columns like:
SELECT
distinct(P.name),
regexp_replace('2016-08-05_11:29:46', '\\_', ' ') as tmstmp
FROM table P;
It works fine. Any idea on the issue ?
To my knowledge, This is a restriction imposed by hive in select syntax.
As per the Select syntax in hive language manual , DISTINCT should come first in order followed by other expressions.
Reference:
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Select
I guess the reason being DISTINCT is a row level operation (even if its specified as function call for a column) and specifically in hive it will be a mapreduce operation.
The similar behavior could be observed in SQL ANSI standard supported database engines like Mysql as well.