In Postgres i am trying to return the value but it shows a error - postgresql-9.1

In postgres I am trying to return this but it shows a error
select substr('10111',3,1) as aa_code
return aa_code;
ERROR: syntax error at or near "return"
LINE 2: return aa_code;
^
********** Error **********

Why use the return at all?
=# select substr('10111',3,1) as aa_code;
aa_code
---------
1
(1 row)

Related

How do I properly deal with triggers in PL SQL?

create or replace trigger grade_
before insert on student_det
for each row
BEGIN
CASE
WHEN new.grade > 10 then dbms_output.put_line('A');
WHEN new.grade > 20 then dbms_output.put_line('A+');
ELSE dbms_output.put_line('Failed');
END
END
When I run the above code, I get the following error
Error at line 4: PLS-00103: Encountered the symbol "WHEN" when expecting one of the following:
:= . ( % ;
The symbol ";" was substituted for "WHEN" to continue.
Error at line 6: PLS-00103: Encountered the symbol "END" when expecting one of the following:
:= . ( % ;
Error at line 5: PLS-00103: Encountered the symbol "ELSE" when expecting one of the following:
:= . ( % ;
The symbol ";" was substituted for "ELSE" to continue.
2. before insert on student_det
3. for each row
4. BEGIN
5. CASE new.grade
6. WHEN 10 then dbms_output.put_line('A')
Could anybody help me out here? I'm relatively new to triggers. My idea is to simply invoke this trigger whenever I insert a new record into student_det record and depending on the conditions given, display the grade obtained by the student.
Some syntax errors:
CREATE OR REPLACE TRIGGER grade_
BEFORE INSERT
ON student_det
FOR EACH ROW
BEGIN
CASE
WHEN :new.grade > 10 /* :new instead of new */
THEN
DBMS_OUTPUT.put_line ('A');
WHEN :new.grade > 20 /* :new instead of new */
THEN
DBMS_OUTPUT.put_line ('A+');
ELSE
DBMS_OUTPUT.put_line ('Failed');
END CASE; /* missing CASE and semicolon */
END; /* missing semicolon */

PLS-00103: Encountered the symbol "END" when expecting one of the following: . ; The symbol ";" was substituted for "END" to continue

I wrote the following program:
create or replace procedure ADDPHONE(IDPELATH in number,IDTHLEFWNO in number )
is
cursor cursor_number is select id_pelath ,TelephoneNumber from PhoneNumbers
where id_pelath>2;
more_than_two_numbers exception;
begin
open cursor_number ;
fetch cursor_number into IDPELATH;
if id_pelath%FOUND then raise more_than_two_numbers
end if;
close cursor_number;
exception
when more_than_two_numbers then
raise_application_error('Error');
END;
/
When I run it, I get the following error:
PLS-00103: Encountered the symbol "END" when expecting one of the following: . ;
The symbol ";" was substituted for "END" to continue.
Could you help me to find the error?
There are multiple issues in the procedure.
Input Parameters are never used in the body.
Cursor is declared to fetch two but FETCH statement is only fetching one.
Incorrect usage of raise_application_error,it requires two input arguments.
I have modified the code that should do the job,
CREATE or REPLACE procedure ADDPHONE(IDPELATH in number,IDTHLEFWNO in number)
is
lv_cnt number(10):=0;
more_than_two_numbers exception;
BEGIN
select COUNT(1) INTO lv_cnt
from PhoneNumbers
where id_pelath = IDPELATH
and TelephoneNumber = IDTHLEFWNO;
if (lv_cnt > 2) then
raise_application_error(-20001,'Error - More than two Numbers ');
end if;
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20002,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
END ADDPHONE;
/

Vertica Function Creation issue with flywaydb

Using Vertica 7.1 with flyway 3.2.1 getting errors when creating functions even tried the example from the docs. Almost like it doesn't ignore the first ';' :
code in the .sql file
CREATE or replace FUNCTION default_to_hello(x VARCHAR) RETURN VARCHAR
AS BEGIN
-- The body of a SQL function in Vertica must be in the form 'RETURN expression'
RETURN (CASE WHEN (x IS NOT NULL) THEN x ELSE 'Hello' END);
END;
Error reported:
Current version of schema "public": 2
Migrating schema "public" to version 3 - test
ERROR: Migration of schema "public" to version 3 failed! Please restore backups and roll back database and code!
ERROR:
Migration V3__test.sql failed
-----------------------------
SQL State : 42601
Error Code : 4856
Message : [Vertica][VJDBC](4856) ERROR: Syntax error at or near "EOL"
Location : /opt/flywaydb/sql/V3__test.sql (/opt/flywaydb/sql/V3__test.sql)
Line : 1
Statement : CREATE or replace FUNCTION default_to_hello(x VARCHAR) RETURN VARCHAR
AS BEGIN
-- The body of a SQL function in Vertica must be in the form 'RETURN expression'
RETURN (CASE WHEN (x IS NOT NULL) THEN x ELSE 'Hello' END)
Has any one else had this happen or am I missing something?
Flyway's VerticaStatementBuilder does not support OR REPLACE. You can see the code here for how that parse (it leaves no room for deviation).
if (statementStart.startsWith("CREATE FUNCTION")) {
if (line.startsWith("BEGIN") || line.endsWith("BEGIN")) {
insideBeginEndBlock = true;
}
if (line.endsWith("END;")) {
insideBeginEndBlock = false;
}
}
I submitted a pull request for it and it was merged for Flyway 4.0.
Your function works, it is some hidden character or something like this.
i managed to reproduce the error while trying to execute it in DbVis.
But in vsql client on the Vertica nodes is working fine.
dbadmin=> CREATE or replace FUNCTION default_to_hello(x VARCHAR) RETURN VARCHAR
dbadmin-> AS BEGIN
dbadmin-> -- The body of a SQL function in Vertica must be in the form 'RETURN expression'
dbadmin-> RETURN (CASE WHEN (x IS NOT NULL) THEN x ELSE 'Hello' END);
dbadmin-> END;
CREATE FUNCTION
dbadmin=> select default_to_hello('Working');
default_to_hello
------------------
Working
(1 row)
dbadmin=> select default_to_hello(null);
default_to_hello
------------------
Hello
(1 row)
This looks like a parsing bug. Please file an issue in the Flyway issue tracker. Pull request also welcome.

SQLite if condition - near "CASE": syntax error

I keep getting this error for my sqlite if-condition query below,
SELECT *
FROM speckdata AS s
LEFT JOIN weatherunderground AS w
ON s.wid_timestamp = w.wid_timestamp
LEFT JOIN nodes AS n
ON n.nid = s.nid
AND n.datatype = 'speck'
WHERE DATE(localdate) BETWEEN '2014-10-09' AND '2015-05-12'
AND n.nid = '9'
CASE WHEN 'polar plot' != NULL THEN w.wspdi != '-9999' ELSE w.wspdi != NULL END
Error,
near "CASE": syntax error ] Exception Name: NS_ERROR_FAILURE
Exception Message: Component returned failure code: 0x80004005
(NS_ERROR_FAILURE) [mozIStorageConnection.createStatement]
Any ideas?
Your CASE expression has nothing to do with the rest of the statement --- it isn't connected in any way. It looks like you're trying to add a condition to the WHERE clause, but you don't have an AND or OR there. So, you get a syntax error because you have a query followed by a random unconnected expression.

can't understand warnings in simple Pl/SQL function

I am using Oracle 10g with SqlDeveloper. When I execute the following code, it says
"FUNCTION wafadar compiled
Warning: execution completed with warning"
create or replace function wafadar
return varchar2(10)
is
cursor c1 is
SELECT employee_id,first_name FROM employees where department_id=50 ;
begin
for i in c1
loop
dbms_output.put_line(i.first_name);
end loop;
return 'hello';
end;
SHOW ERRORS at the end is also not showing the warnings. Why are the warnings there ?
Errors!
At first, you should care about errors, and I bet you have one on the return clause of your function (you can't specify the size of the "varchar2".
Warnings
Did you look for "warning" in the manuals?
http://download.oracle.com/docs/cd/E11882_01/appdev.112/e17126/errors.htm#LNPLS00711
How to see warnings(enable categories you need)
alter function wafadar compile plsql_warnings='ENABLE:ALL' reuse settings
Check:
select plsql_warnings
from user_plsql_object_settings ps
where ps.name = 'WAFADAR'
Your warnings:
Client tools like sql*plus or Sql Developer(if supported):
show errors
or
select *
from user_errors ur
where ur.name = 'WAFADAR'
NAME TYPE SEQUENCE LINE POSITION TEXT ATTRIBUTE MESSAGE_NUMBER
------------------------------ ------------ ---------- ---------- ---------- -------------------------------------------------------------------------------- --------- --------------
WAFADAR FUNCTION 1 1 1 PLW-05018: unit WAFADAR omitted optional AUTHID clause; default value DEFINER us WARNING 5018
Finally, I suggest you to read a little bit of:
How to ask: http://www.catb.org/~esr/faqs/smart-questions.html#before
http://download.oracle.com/docs/cd/E11882_01/appdev.112/e17126/toc.htm
All the "oracles" are here: http://tahiti.oracle.com/
Sql developer oracles(if you want to use it!): http://download.oracle.com/docs/cd/E11882_01/doc.112/e12152/toc.htm

Resources