MariaDB-Statement in Bash here-Document of a Procedure-Block throws ERROR 1064 (42000) [duplicate] - bash

I keep getting this error for the below MySQL SP
any ideas?
CREATE PROCEDURE productpricing(
OUT pl DECIMAL(8,2), OUT ph
DECIMAL(8,2), OUT pa DECIMAL(8,2) )
BEGIN SELECT Min(amount) INTO pl
FROM Card Error Code: 1064 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 9 0.000 sec
DROP PROCEDURE IF EXISTS productpricing;
CREATE PROCEDURE productpricing(
OUT pl DECIMAL(8,2),
OUT ph DECIMAL(8,2),
OUT pa DECIMAL(8,2)
)
BEGIN
SELECT Min(amount)
INTO pl
FROM Card;
END;

Did you define a delimiter?
Try adding delimiter // before the CREATE PROCEDURE statement. Also replace END; with END;//. I checked and it works for me.
See the MySQL doc on stored procedures

you need group by if you use min,max i think. if that doesnt work try #p1 to create a temp table. May be problem is the table you are inserting it to.

Related

INTO error in SELECT while creating stored procedure in PL/SQL

I am trying to create stored procedure where I want to join two tables and save the result into one of the tables, but I am getting INTO clause is required error.
This is my code:
CREATE PROCEDURE DiorItemMaster
AS
SELECT *FROM pcdo_dior_item_master
INNER JOIN pcdo_itemdata on pcdo_itemdata.vpn = pcdo_dior_item_master.vpn;
GO;
ERROR:
Error(4,1): PLS-00428: an INTO clause is expected in this SELECT statement
You have a query that selects columns from a table in your pl/sql block. What do you want to do with the result of that query ? You cannot just select and not do anything with the results in pl/sql (you can in sql). Oracle expects that you to store the results of that select in variables. That can be done using the SELECT INTO clause.
Example (based on sample schema emp/dept):
DECLARE
l_emp emp%ROWTYPE;
BEGIN
SELECT e.* INTO l_emp FROM emp e WHERE e.ename = 'KING';
END;
/
Note that you can SELECT INTO individual columns and into rows. You cannot use SELECT INTO arrays.
A couple of other remarks about your code:
You perform a SELECT * from a table with a join to another table without using aliases. This will return all columns from both tables. It is a lot more readable to prefix the "*" with a table alias like in the example.
The GO; is not part of the oracle syntax - this will cause a compilation error.

Error message Multi Part Identifier could not be bound

I am writing a SQL query to use in a Boyum validation that will flag and BP Master Data NAMES that are LIKE
Here is the query I have written
IF OCRD.Cardname IN (Select OCRD.Cardname from OCRD WHERE OCRD.Cardname
LIKE '%'+Cardname+'%')
BEGIN
SELECT 'Duplicate'
FOR BROWSE
END
Here is the error message I have received
[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]The multi-part identifier "OCRD.Cardname" could not be bound.
2). [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Statement(s) could not be prepared.
)
That is invalid SQL you provided. You can't reference a table column like that
IF EXISTS (Select OCRD.Cardname from OCRD WHERE OCRD.Cardname LIKE '%'+Cardname+'%')
BEGIN
SELECT 'Duplicate'
FOR BROWSE
END

No more data to read from socket (Oracle SQL Developer)

I have created one trigger that calls one procedure, using oracle sql developer. Here is the code for trigger:
CREATE OR REPLACE TRIGGER noteTrigger
BEFORE
INSERT OR
UPDATE OF valoare OR
DELETE
ON note
BEGIN
CASE
WHEN INSERTING THEN
DBMS_OUTPUT.PUT_LINE('Inserting');
updateBursa();
WHEN UPDATING('valoare') THEN
DBMS_OUTPUT.PUT_LINE('Updating valoare');
updateBursa();
WHEN DELETING THEN
DBMS_OUTPUT.PUT_LINE('Deleting');
updateBursa();
END CASE;
END;
/
Now, the procedure is written here:
CREATE OR REPLACE PROCEDURE updateBursa IS
v_countBursieri NUMBER := 0;
BEGIN
UPDATE STUDENTI SET bursa = null;
FOR v_i IN (SELECT nr_matricol from studenti natural join note group by nr_matricol having avg(valoare) =
(select max(avg(valoare)) from studenti natural join note group by nr_matricol)) LOOP
v_countBursieri := v_countBursieri + 1;
END LOOP;
FOR v_i IN (SELECT nr_matricol from studenti natural join note group by nr_matricol having avg(valoare) =
(select max(avg(valoare)) from studenti natural join note group by nr_matricol)) LOOP
UPDATE STUDENTI SET bursa = 1000/v_countBursieri where nr_matricol = v_i.nr_matricol;
END LOOP;
END;
/
And when I try to modify note table by this:
INSERT INTO note VALUES ('111', '25', 5, TO_DATE('20/06/2015', 'dd/mm/yyyy'));
I get error:
Error report -
ORA-00603: ORACLE server session terminated by fatal error
ORA-00600: internal error code, arguments: [kqlidchg0], [], [], [], [], [], [], [], [], [], [], []
ORA-00604: error occurred at recursive SQL level 1
ORA-00001: unique constraint (SYS.I_PLSCOPE_SIG_IDENTIFIER$) violated
00603. 00000 - "ORACLE server session terminated by fatal error"
*Cause: An Oracle server session was in an unrecoverable state.
*Action: Log in to Oracle again so a new server session will be created
automatically. Examine the session trace file for more
information.
Error report -
SQL Error: No more data to read from socket
This is Ora-600 you should deeply explore reasons why is raised in your case, maybe you should open SR.
also, in the code you are referencing the table "note" on which you are creating a trigger, which could lead to mutating table, so consider the option of changing your code logic.
In my understanding, you need statement level trigger, not row level trigger?
ORACLE server session terminated by fatal error
Try this, had the same problem on the same "topic" let's say :) And setting things as shown previously just solved my problem. Good luck!

The mystic getClobVal()

I have a table (AKADMIN) with an XMLTYPE column which name is XML.
I would like to use the getClobVal() with this column.
select t.xml.getClobVal() /**/
, t.xml.getClobVal() --
, t.xml.getClobVal() as clobval
, t.xml.getClobVal()
from akadmin t where ROWID = 'AAAQc6AAIAAAADDAAA' ;
In the resultset the first 4 column give CLOB type, but the fifth column XMLTYPE. I have to type any comment or alias after getClobVal() to correct (CLOB) type of the result. Why?
Another issue, when I leave the alias of tablename:
select xml.getClobVal()
from akadmin t where ROWID = 'AAAQc6AAIAAAADDAAA' ;
It throws an ORA-00904 string: invalid identifier
Hmmm...
Does anybody have any idea?
Addition info about environment:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0;
PL/SQL Developer 10.0.5.1710
But a tried this in our Java apllication via OJDBC6 with same results
You should put xml in a brackets:
select (xml).getClobVal() from akadmin;
works for me

DB2 for IBM iSeries: IF EXISTS statement syntax

I am familiar with Sybase which allows queries with format: IF EXISTS () THEN ... ELSE ... END IF (or very close). This a powerful statement that allows: "if exists, then update, else insert".
I am writing queries for DB2 on IBM iSeries box. I have seen the CASE keyword, but I cannot make it work. I always receive the error: "Keyword CASE not expected."
Sample:
IF EXISTS ( SELECT * FROM MYTABLE WHERE KEY = xxx )
THEN UPDATE MYTABLE SET VALUE = zzz WHERE KEY = xxx
ELSE INSERT INTO MYTABLE (KEY, VALUE) VALUES (xxx, zzz)
END IF
Is there a way to do this against DB2 on IBM iSeries? Currently, I run two queries. First a select, then my Java code decides to update/insert. I would rather write a single query as my server is located far away (across the Pacific).
+UPDATE+
DB2 for i, as of version 7.1, now has a MERGE statement which does what you are looking for.
>>-MERGE INTO--+-table-name-+--+--------------------+----------->
'-view-name--' '-correlation-clause-'
>--USING--table-reference--ON--search-condition----------------->
.------------------------------------------------------------------------.
V |
>----WHEN--+-----+--MATCHED--+----------------+--THEN--+-update-operation-+-+----->
'-NOT-' '-AND--condition-' +-delete-operation-+
+-insert-operation-+
'-signal-statement-'
See IBM i 7.1 InfoCenter DB2 MERGE statement reference page
DB/2 on the AS/400 does not have a conditional INSERT / UPDATE statement.
You could drop the SELECT statement by executing an INSERT directly and if it fails execute the UPDATE statement. Flip the order of the statements if your data is more likely to UPDATE than INSERT.
A faster option would be to create a temporary table in QTEMP, INSERT all of the records into the temporary table and then execute a bulk UPDATE ... WHERE EXISTS and INSERT ... WHERE NOT EXISTS at the end to merge all of the records into the final table. The advantage of this method is that you can wrap all of the statements in a batch to minimize round trip communication.
You can perform control-flow logic (IF...THEN...ELSE) in an SQL stored procedure. Here's sample SQL source code:
-- Warning! Untested code ahead.
CREATE PROCEDURE libname.UPSERT_MYTABLE (
IN THEKEY DECIMAL(9,0),
IN NEWVALUE CHAR(10) )
LANGUAGE SQL
MODIFIES SQL DATA
BEGIN
DECLARE FOUND CHAR(1);
-- Set FOUND to 'Y' if the key is found, 'N' if not.
-- (Perhaps there's a more direct way to do it.)
SET FOUND = 'N';
SELECT 'Y' INTO FOUND
FROM SYSIBM.SYSDUMMY1
WHERE EXISTS
(SELECT * FROM MYTABLE WHERE KEY = THEKEY);
IF FOUND = 'Y' THEN
UPDATE MYTABLE
SET VALUE = NEWVALUE
WHERE KEY = THEKEY;
ELSE
INSERT INTO MYTABLE
(KEY, VALUE)
VALUES
(THEKEY, NEWVALUE);
END IF;
END;
Once you create the stored procedure, you call it like you would any other stored procedure on this platform:
CALL UPSERT_MYTABLE( xxx, zzz );
This slightly over complex piece of SQL procedure will solve your problem:
IBM Technote
If you want to do a mass update from another table then have a look at the MERGE statement which is an incredibly powerful statement which lets you insert, update or delete depending on the values from another table.
IBM DB2 Syntax

Resources