PostgreSQL CASE statement syntax error - case

I'm trying to follow the instructions in the PostgreSQL manual.
PostgreSQL: Documentation: 9.1: Control Structures
My PostgreSQL server is version 9.1.14 on Windows 32-bit.
The following SQL statement is unexpectedly resulting in a syntax error:
SELECT
CASE 1
WHEN 1,2 THEN 'x'
ELSE 'y'
END;
I'm expecting it to return 'x';
The more traditional code runs fine, however:
SELECT
CASE 1
WHEN 1 THEN 'x'
WHEN 2 THEN 'x'
ELSE 'y'
END;

You are using the CASE syntax as provided by the procedural language plpgsql. This is similar but not identical to the SQL CASE syntax. Here is the link to the SQL version of CASE.
Here you see, that 1,2 is not allowed, only a plain expression. So you could write:
SELECT
CASE
WHEN 1 in (1,2) THEN 'x'
ELSE 'y'
END;

Related

CREATE FUNCTION throws SQL Error (1064) (42000)

I'm trying to create a stored function in a MariaDB database.
I copied the function I'm trying to create from the MariaDB Docs:
DELIMITER //
CREATE FUNCTION FortyTwo() RETURNS TINYINT DETERMINISTIC
BEGIN
DECLARE x TINYINT;
SET x = 42;
RETURN x;
END
//
DELIMITER ;
Unfortunately, I get the following error:
SQL Error [1064] [42000]: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3
What baffles me most is that the given code is supposed to resolve the very error code I'm getting according to the MariaDB docs
The solution is to specify a distinct delimiter for the duration of the process, using the DELIMITER command
It turned out, the client I used to issue the command, DBeaver, was causing the trouble.
After switching over to MySqlWorkbench everything worked as expected.
Apparently, DBeaver didn't recognise the delimiters correctly..
I think you may have forgotten to select the database you want this routine to be stored into.
So try adding a use as the first line
use `test`;
DELIMITER //
CREATE FUNCTION FortyTwo() RETURNS TINYINT DETERMINISTIC
BEGIN
DECLARE x TINYINT;
SET x = 42;
RETURN x;
END
//
DELIMITER ;

procedure created with compilation error

CREATE OR REPLACE PROCEDURE ex9a(n NUMBER ,c CHAR) IS
pi NUMBER(7,4):=3.14;
v_record Areas%rowtype;
BEGIN
IF c='R' THEN
DBMS_OUTPUT.PUT_LINE('CHOICE : R');
v_record.Input_Value:= n;
v_record.Circle_Area:=pi*n*n;
v_record.Square_Area:=null;
v_record.Sphere_Area:=2*pi*n;
v_record.Sphere_Volume:=(3/4)*r*r;
v_record.Cube_Volume:=null;
END IF;``
END;
/
I am getting procedure created with compilation errors.I want to compute area of a circle and insert it into the table .
When I give SHOW ERRORS , it lists the errors as
1) plsql statement
2) 'R' must be declared .
(Even after I gave 'then'. I forgot the line number)
It would help if you would list the errors, and the lines on which they occur.
However, one obvious issue is that your IF statement has no THEN. It should be:
IF c = 'R' THEN

PLS-00103 error when creating object type

I am trying to create and assign variables using following code to create object types in plsql (11g) but facing some errors:
begin
execute immediate 'drop type picu_obj force';
execute immediate 'drop type picu_obj_tab force';
execute immediate 'create type picu_obj as object(Customer_ID varchar2(32767),Customer_Name varchar2(32767),Server_Name varchar2(32767),Time_stamp varchar2(32767))';
execute immediate 'create type picu_obj_tab is table of picu_obj;';
picu_var picu_obj_tab;
picu_var := picu_obj_tab(picu_obj('101','xyz','pro-ssr-qr','12:13'));
end;
The above code gives following errors:
ERROR at line 6:
ORA-06550: line 6, column 10:
PLS-00103: Encountered the symbol "PICU_OBJ_TAB" when expecting one of the
following:
:= . ( # % ;
The symbol ":=" was substituted for "PICU_OBJ_TAB" to continue.
Please suggest what I am doing wrong here.
There are two problems with this code:
First: In Oracle 11g you can not use varchar2(32767) the maximum length is 4000 for a varchar there. So even if the code did run, it wouldn't create the types.
Secondly: the PL/SQL code is validated/compiled when you run it. But as you use dynamic SQL to create the types, the PL/SQL compiler can't see those types when it tries to compile the lines:
picu_var picu_obj_tab;
picu_var := picu_obj_tab(picu_obj('101','xyz','pro-ssr-qr','12:13'));
and that's the error you are seeing.
You have to create the types before you run PL/SQL code that uses them.

Conditional insert xquery failing with error "unexpected insert, expecting else"

I am trying to execute the xquery on Sedna database to conditionally update the container, as below
if(fn:exists(doc("blog")/entries/entry[#active="1" and #id="1"]/comments)) then
UPDATE insert <comment active="1"><name>sunil.s</name><email>suni.l.s#gmail.com</email><desc>desbbh</desc></comment> into doc("blog")/entries/entry[#active="1" and #id="1"]/comments
else
UPDATE insert <comments><comment active="1"><name>sunil.s</name><email>sunil.s#gmail.com</email><desc>sdd</desc></comment></comments> into doc("blog")/entries/entry[#active="1" and #id="1"]
But this query always failing with below error
SEDNA Message: ERROR XPST0003 It is a static error if an expression is
not a valid instance of the grammar defined in A.1 EBNF. Details: at
(2:8), syntax error, unexpected insert, expecting else
The error indicate that it is expecting else instead of insert in the second line.
Can someone please help me understand problem with the query and possible fix?
Your query assumes the existence of an expression with syntax like
UPDATE insert expr into expr
There is no such expression in XQuery (or the XQuery Update Facility). Instead, it appears to be a non-standard syntax supported by Sedna.
However, the documentation (http://www.sedna.org/progguide/ProgGuidesu6.html#x12-430002.3) refers to it as a "statement", not an "expression", suggesting that it must be the 'top-level' (outermost) construct in your query.
To accomplish this, you could rewrite your query as:
UPDATE
insert
if ... then <comment>...</comment> else <comments>...</comments>
into
if ... then doc("blog")/.../comments else doc("blog")/...
Unfortunately, this repeats the 'if' condition; it's not clear whether Sedna provides a syntax for factoring that out, e.g.
UPDATE
let $c := ...
insert
if $c then <comment>...</comment> else <comments>...</comments>
into
if $c then doc("blog")/.../comments else doc("blog")/...

Error "BIND VARIABLE "A" NOT DECLARED" when running PL/SQL block

When running the following code in Oracle 10g (pl/sql)
DECLARE
A NUMBER;
B NUMBER;
BEGIN:
A:=&N;
B:=&M;
IF (A>B)
DBMS_OUTPUT.PUT_LINE('THE MAXIMUM OF TWO NUMBER IS:' || TO_CHAR(A));
ELSE
DBMS_OUTPUT.PUT_LINE('THE MAXIMUM OF TWO NUMBERS IS:' || TO_CHAR(B));
END IF;
END;
I get the error 'BIND VARIABLE "A" NOT DECLARED' and I don't know why. What is causing this?
There are a couple of things wrong with your code.
The first problem is that there should be no colon after BEGIN. What you've written is being interpreted by Oracle as BEGIN :A := ..., and that should explain why you're getting an error about bind variable A.
The second problem is with the line IF (A>B). You need to add a THEN to the end.
Incidentally, you can use GREATEST(A, B) to return the larger of two numbers.

Resources