Can't execute sql file - sql

I'm trying to execute the following code using isql:
IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = N'SFPTB051_ABERTURARCLH'))
BEGIN
SELECT * FROM SFPTB051_ABERTURARCLH;
END
The way i'm doing:
isql -i sql_scripts/test.sql _input/mygdb.GDB -user SYSADM -pass masterkey
Output:
Statement failed, SQLSTATE = 42000
Dynamic SQL Error
-SQL error code = -104
-Token unknown - line 1, column 1
-IF
At line 1 in file sql_scripts/test.sql
Expected end of statement, encountered EOF
Any ideias?
Thanks!
UPDATE ---
I'm trying the following:
SET TERM # ;
EXECUTE BLOCK AS
BEGIN
SELECT * FROM SFPTB051_ABERTURARCLH
END#
SET TERM ; #
But it's returning:
Statement failed, SQLSTATE = 42000
Dynamic SQL Error
-SQL error code = -104
-Token unknown - line 4, column 1
-END

Usually, select statements within a block statement need to return their values. For example, you can return them into variables or return values, which are almost the same.
Furthermore, every statement within a block statement has to be terminated by a semi-colon (;).
Your block statement could look something like this:
SET TERM # ;
EXECUTE BLOCK AS
DECLARE VARIABLE FIELD1 TYPE OF COLUMN SFPTB051_ABERTURARCLH.FIELD1;
/* declare more variables as needed */
BEGIN
FOR
SELECT FIELD1
FROM SFPTB051_ABERTURARCLH
INTO :FIELD1
DO
BEGIN
/* do something with the variables values */
END
END#
SET TERM ; #

Related

function as parameter in MariaDB query

I have the following code in Maria DB ,
I want to use my function in a query to create a sequence that starts with the count(*) +1 of a my TABLE1
It gives me an error in the CREATE SEQUENCE query :
CREATE FUNCTION myFuntion() RETURNS INT
BEGIN
DECLARE lastID INT DEFAULT 1;
SELECT COUNT(*) INTO lastID FROM TABLE1;
RETURN lastID+1;
END;
CREATE SEQUENCE seq101 START WITH myFuntion() INCREMENT BY 1 ;
Error :
MySqlError { ERROR 1064 (42000): 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 'myFuntion() INCREMENT BY 1' }
I think the only way is to use a prepared statement and a variable, since user functions are not allowed in PREPARE FROM/EXECUTE IMMEDIATE.
SELECT count(*) + 1 INTO #lastid FROM yourtable
EXECUTE IMMEDIATE CONCAT("CREATE SEQUENCE seq101 START WITH ", #lastid," INCREMENT BY 1");

Dynamic Column on SQL doesn't work (APEX,Interactive Report)

I tried to implement a page on APEX(19.2) , where the user has to make an input , which is the column name. This input shall restrict the select statement on the where clause and is the following:
select * FROM UEBERSICHT where
:P904_COLUMN = :P904_AUSDRUCK;
:P904_COLUMN and :P904_AUSDRUCK; are both APEX items, which is needed for the user input.
When I write the column name instead of :P904_COLUMN, I get an output, otherwise not.
But as the headline says, I want to implement a dynamic column.
I also tried it with PL/SQL , which returns a SQL - statement like the following:
declare
statement varchar2(4000);
begin
statement:= 'SELECT * FROM UEBERSICHT where
:P904_COLUMN = :P904_AUSDRUCK;';
return statement;
end;
Another approach was , to save the input on a variable first and write the variable name into the SQL - Statement:
declare
statement varchar2(4000);
spalte varchar2(50);
begin
if :P904_COLUMN = '"Gesellschaft"' then spalte := '"Gesellschaft"'; end if;
statement:= 'SELECT * FROM UEBERSICHT where
'||spalte||' = :P904_AUSDRUCK;';
return statement;
end;
Here i get this syntax error, which shouldn't appear normally: "ORA-20999: Parsing returned query results in "ORA-20999: Failed to parse SQL query! ORA-06550: line 5, column 6: ORA-00936: missing expression"."
How can I solve this problem ?
PS: Yes I am submitting all APEX items.
Update: The debug shows me that I get the Input, but the interactive Report doesn't give me any output though.
IF all the columns in :P904_COLUMN is of same data type you can use something like below where A and B are column names
select * FROM UEBERSICHT where
DECODE(:P904_COLUMN,'A',A,'B',B) = :P904_AUSDRUCK;

Can I call a global variable in source variable in ODI?

I am trying to build a ODI procedure, which will take schema name, db procedure name and parameters from a oracle database metadata table. The parameter field contains a name of a ODI global variable.The source command is like this
SELECT SCHEMA_NAME VAR_SCHEMA, PROCEDURE_NAME VAR_PROCEDURE, PARAMETER_NAME
VAR_PARAMETER FROM SCHEMA-NAME.TABLE_NAME
the output of the source command is like this:
VAR_SCHEMA_NAME VAR_TABLE_NAME VAR_PARAMETER
ABC PROC_LIST TO_DATE('#VAR_ETL_LOAD_DATE','DD/MM/RRRR')
Here, #VAR_ETL_LOAD_DATE is a global variable in ODI.
In the target command of the procedure, I want to use these information from source command to execute procedures listed in metadata table. I wrote a command like this:
DECLARE
VVC_SQL_STMT LONG;
BEGIN
VVC_SQL_STMT := 'BEGIN
#VAR_SCHEMA_NAME.#VAR_PROCEDURE_NAME(#VAR_PARAMETER);
END;';
INSERT INTO AK_TST2 VALUES(VVC_SQL_STMT,SYSDATE);
COMMIT;
EXECUTE IMMEDIATE (VVC_SQL_STMT);
END;
This code gives the following error in ODI:
ODI-1228: Task PROC_SP_HANDLER (Procedure) fails on the target ORACLE
connection OCDM_SYS.
Caused By: java.sql.SQLException: ORA-06550: line 8, column 61:
PLS-00103: Encountered the symbol "#" when expecting one of the following:
* & = - + ; < / > at in is mod remainder not rem
<an exponent (**)> <> or != or ~= >= <= <> and or like like2
like4 likec between || multiset member submultiset
What is the reasons for this and how can I execute stored procedures in ODI by reading procedure names and parameters from a metadata table?
If you select data from a table and use the result as a code for further execution, normally you cannot use ODI variables there. Because it too late for ODI to recognse that it is a variable and substitute it by a variable. This is the same for both global and project variables.
If you could print "#"+variable_name from ?- or %-substitution than it will work. But if #-substitution prints variable name or if variable appears as a final code after fetching values from Source it is too late. In this cases it remains as a plain text #VAR.
In your particular case you can do the following:
Declare all variables like #VAR_ETL_LOAD_DATE in a package. I mean all variables that could potentially appear in the metadata table. Bacause scenario should know all variables in advance.
Select and fetch records within ?-substitution using odiRef.getJDBCConnection('SRC'). Collect all results into a java-variable in the form of executable code.
E.g., source code could look like this:
select 1 from dual;
<?
import java.sql.*;
String crlf = System.getProperty("line.separator");
String result = "begin"+crlf+"null;"+crlf;
PreparedStatement stmt = odiRef.getJDBCConnection("SRC").prepareStatement("select schema||'.'||proc||'('||param||')' from metatable");
ResultSet rs = stmt.executeQuery();
while(rs.next()){
result += "insert into ak_tst2 values('"+rs.getString(1).replaceAll("'",'"'.toString())+"');"+crlf;
result += "commit;"+crlf;
result += rs.getString(1)+";"+crlf;
}
result += "end;";
rs.close();
stmt.close();
?>
Target code should be very simple
<?=result?>
At runtime target code will appear like this
begin
null;
insert into ak_tst2 values('qwe.asd("param_using_#var")');
commit;
qwe.asd('param_using_#var');
insert into ak_tst2 values('qwe2.asd2("param2_using_#var")');
commit;
qwe2.asd2('param2_using_#var');
insert into ak_tst2 values('qwe3.asd3("param3_using_#var")');
commit;
qwe3.asd3('param3_using_#var');
end;
And ODI variables will be successfully substituted by values.

Error occurs with anonymous block pl/sql for multiple Update statement

I am having problems in following pl/sql anonymous. Here is the scaled down version of my pl/sql block..What is wrong with my code?
WHENEVER SQLERROR EXIT sql.sqlcode ROLLBACK
WHENEVER oserror EXIT FAILURE ROLLBACK
SET SERVEROUTPUT ON
SET ECHO OFF
SET DEFINE OFF
SET LINESIZE 120
SET AUTOCOMMIT OFF
BEGIN
update MYTABLE set GID = '12345' where MYTABLE.COLUMN1=456456 and MYTABLE.PARTY<>0 and MYTABLE.EXPIRY = to_date('17/05/2013','DD/MM/YYYY')
AND EXISTS (SELECT PARTIES.LABEL
FROM PARTIES
WHERE PARTIES.m_id = MYTABLE.PARTY_ID and PARTIES.LABEL = 'PARTY_NAME');
dbms_output.put_line( 'Rows Updated : ' || to_char(sql%rowcount));
END;
/
Here is the output I get
update MYTABLE set GID = '12345' where MYTABLE.COLUMN1=456456 and MYTABLE.PARTY0 and MYTABLE.EXPIRY = to_date('17/05/2013','DD/MM/YYYY')
*
ERROR at line 2:
ORA-06550: line 2, column 8:
PL/SQL: ORA-06552: PL/SQL: Compilation unit analysis terminated
ORA-06553: PLS-488: invalid variable declaration: object 'TIMESTAMP' must be a type or subtype
ORA-06550: line 2, column 1:
PL/SQL: SQL Statement ignored
Based on the error you have reported I am guessing that somewhere in your PL/SQL block (which is not present in your question) you are referring to a table column named TIMESTAMP. Unfortunately this as a reserved word in Oracle as it is a column data type. I would recommend that you rename the column to something like DATE_CREATED and see if your code compiles.

Can't declare variable in Firebird 2.5, why?

I have a one line query:
DECLARE VARIABLE var_SecondsOfTime INTEGER;
But after running the query I am getting this message:
Engine Error (code = 335544569):
Dynamic SQL Error. SQL error code =
-104. Token unknown - line 1, column 9. VARIABLE.
SQL Error (code = -104): Invalid
token.
I've looked everywhere on the Internet and all examples showing the same declaration style which I am using.
What is wrong?
Firebird 2.5 supports execution of code blocks surrounded by a execute block statement, try this:
set term ^ ;
EXECUTE BLOCK
AS
DECLARE VARIABLE var_SecondsOfTime INTEGER;
BEGIN
SELECT 1 from RDB$DATABASE into var_SecondsOfTime ;
END
^
set term ; ^
I issued the select because I'm pretty sure it is not possible to execute an empty block, try this by yourself removing the select.
Edit
My original select was invalid for a block, I added the into clause to collect the result. I never used firebird maestro, but it now works perfectly on isql, as shown.
Try this:
set term ^ ;
EXECUTE BLOCK
AS
DECLARE VARIABLE var_SecondsOfTime INTEGER;
BEGIN
SELECT 1 from RDB$DATABASE into :var_SecondsOfTime ;
END^
set term ;^
The second set term needs the semi colon before the carat.