ScriptUtils.readScript() with DEFAULT_STATEMENT_SEPARATOR is not working - spring

I am trying to execute a sql script from file using ScriptUtils.readScript method:
sql = ScriptUtils.readScript(fileReader,
ScriptUtils.DEFAULT_COMMENT_PREFIX,
ScriptUtils.DEFAULT_STATEMENT_SEPARATOR);
getJdbcTemplate().update(sql);
But I get the error org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar and from the logs I see that semicolon * in the sql statement is not ignored even I am using ScriptUtils.DEFAULT_STATEMENT_SEPARATOR why it isn't working? What's wrong here?
Edit: I know that I can solve this by using:
getJdbcTemplate().update(sql.replace(";", ""));
but maybe there is another solution?
Edit2: Here is example of sql that I need to execute:
INSERT
INTO MYTABLE
(
ID,
MYNUMBER,
MYVALUE
)
SELECT
ID,
0,
B.MYVALUE
FROM ATABLE A,
BTABLE B
WHERE A.ID = B.ID
AND NOT EXISTS
(SELECT 1 FROM MYTABLE M WHERE M.ID = A.ID
);

I don't think you're using ScriptUtils.readScript the right way. The javadocs themselves state:
Mainly for internal use within the framework.
Looking at the source code, it seems that all this function does is load all the lines from a file into a single string, with some logic around comments. The use of the separator in this method is minor and appears only to be relevant if there is a whitespace at the end of it.
If you want to ignore the separator, you'll need to remove it the way that you suggested (with a replace).

Related

INNER JOIN two table get error SQL Statment not ended properly

I have two table which I want to select Name from t2.
The situation is following.
I have t1 Policy which containt EmployeeID
And table t2 which containt Name
Now I want to select which Employee release policy.
So in t1(Policy- AUTO.POL) I have column: SIFRA_RAD
and t2(Employee-AUTO.SIFRAD) I have colum: SIFRA_R, Ime
I try something like this:
select auto.pol.sifra_rad, auto.sifrad.ime
from auto.sifrad
inner join auto.pol on auto.sifrad.ime = auto.pol.sifra_rad;
After that I get error
ORA-00933: SQL command not properly ended
I have no idea what is wrong here. Any suggestion?
The problem comes from query, and we fix it
select p.sifra_rad, s.ime from auto.sifrad s, auto.pol p where s.ime = p.sifra_rad

oracle report lexical parameter

I am using oracle report and have problem with "SELECT ALL" here is my query
SELECT * FROM company A, seller B
WHERE a.id = b.id
&(P)Company_id
and in my after parameter in oracle report i use
begin
if (:(V)Company_id is not null and :(V)Company_id<> '0')
:(P)Company_id:= ' and a.id ='||:(V)Company_id;
end if;
return (true)`
end;
if the id is all digit like 000123 works fine, but if id like ([L]00123) the result is show all data. need help with my lexical parameter.
Information you post is misleading. I've been using Oracle Reports for ages, and I've never seen syntax you use. Code you wrote doesn't even compile; how would it work, then (which is what you claim)? There's no (V)something syntax at all.
Anyway, from my point of view, you don't need a lexical parameter but a simple OR condition, e.g.
select *
from company a join seller b on b.id = a.id
where (a.id = :par_company_id or :par_company_id is null)
the first part of it, a.id = :par_company_id will return rows whose ID is equal to value you enter in the parameter form
the second part, or :par_company_id is null will return all rows if you leave the parameter value empty
I would have thought you'd get an error from that rather than all data, but maybe Reports does something weird in that scenario. Anyway, it looks like you just need to enclose the passed-in value in single quotes, which you will need to escape; so instead of this line:
:(P)Company_id:= ' and a.id ='||:(V)Company_id;
use:
:(P)Company_id:= ' and a.id = ''' || :(V)Company_id || '''';
although it would be better if you could keep it as a bind variable. I'm not familiar with Reports but something like this might work:
select *
from company A
join seller B
on a.id = b.id
where &(V)Company_id is null or a.id = &(V)Company_id
(I've switched to ANSI join syntax as well...)

wso2am 2.1.0 on oracle - sqlexception missing right parenthesis

Using WSO2AM-2.1.0-update12 (carbon-apomgt tag v6.2.108) normally works well with mysql. We intend to switch the underlying database to Oracle DB (11g as far I know), looks simple.
Wen creating an API the apim throws an SQLException
ORA-00907: missing right parenthesis
we traced the exception to
org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO:7652
executing following query:
SELECT
A.SCOPE_ID, A.NAME, A.DISPLAY_NAME,
A.DESCRIPTION, C.SCOPE_BINDING
FROM
((IDN_OAUTH2_SCOPE AS A INNER JOIN AM_API_SCOPES AS B ON A.SCOPE_ID = B.SCOPE_ID)
INNER JOIN IDN_OAUTH2_SCOPE_BINDING AS C ON B.SCOPE_ID = C.SCOPE_ID )
WHERE B.API_ID = 1;
Running this query itself against the APIM database indeed reproduces the exception, so the code hasn't been properly tested. For some customizations we rather try to fix the code if possible than migrate to a newer version (where we have no ensurance the issue is fixed or our customizations will work)
I suspect the query as been not tested as at all and correct query may look like
SELECT
A.SCOPE_ID, A.NAME, A.DISPLAY_NAME,
A.DESCRIPTION, C.SCOPE_BINDING
FROM
IDN_OAUTH2_SCOPE A INNER JOIN AM_API_SCOPES B ON A.SCOPE_ID = B.SCOPE_ID
INNER JOIN IDN_OAUTH2_SCOPE_BINDING C ON B.SCOPE_ID = C.SCOPE_ID
WHERE B.API_ID = 1;
I'd like to validate and correct me if the query is syntactically and semantically not equivalent
Could you please try the following?
SELECT A.SCOPE_ID, A.NAME, A.DISPLAY_NAME, A.DESCRIPTION, C.SCOPE_BINDING FROM ((IDN_OAUTH2_SCOPE A INNER JOIN AM_API_SCOPES B ON A.SCOPE_ID = B.SCOPE_ID) INNER JOIN IDN_OAUTH2_SCOPE_BINDING C ON B.SCOPE_ID = C.SCOPE_ID ) WHERE B.API_ID = ?
You can find this corrected query in https://github.com/wso2/carbon-apimgt/blob/6.x/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java#L2068

ERROR Join: Oracle 00933 - SQL Command Not Properly Ended [duplicate]

I am using a SQL statement with a Temporary relation, and am getting the error ORA-009933: SQL command not properly ended
I don't see anything wrong with the statement, so any assistance is greatly appreciated. The statement is:
SELECT Temp.name,
Temp.AvgSalary
FROM (SELECT A.aid,
A.aname AS name,
AVG(E.salary) AS AvgSalary
FROM Aircraft A,
Certified C,
Employees E) AS Temp;
Thanks
oracle does not support as for table aliases, only for column aliases and they are optional for that use => delete all as keywords ;)
You shouldn't put the AS temp. When putting alias to a table (or subquery) you should only write the alias. This should work:
SELECT Temp.name, Temp.AvgSalary
FROM ( SELECT A.aid, A.aname AS name, AVG(E.salary) AS AvgSalary
FROM Aircraft A, Certified C, Employees E) Temp;
Best regards,

How to loop in sql?

I dont want to use the "loop" related keyword, how can I implement loop with basic sql command in oracle ?
I have two table :
A:
ID, Color
B,
ID, AID, Type
I want to loop all records in B, and if ID = AID, then set the A.Color = B.Type
Thanks in advance !
Looping is, by definition, a procedural construct.
SQL is declarative: tell the database what you want done, not how to do it.
If you're absolutely convinced that you need to program such a thing, then write it in PL/SQL, Oracle's procedural language.
Bu I'm sure that it's possible to do what you want in SQL using an UPDATE with a WHERE clause.
Something like this (corrected per NullUserException):
UPDATE A SET A.Color = (SELECT B.Type FROM B WHERE A.ID = B.AID)
An alternate method:
MERGE INTO a
USING b
ON (b.aid = a.id)
WHEN MATCHED THEN UPDATE SET a.color = b.type;
You could just do:
UPDATE tablea a
SET a.color = (SELECT b.type
FROM tableb b
WHERE b.aid = a.id)
See this SQL script.
To do that you will have to write a stored procedure using PL/SQL. Here is the oracle page with some info and papers on the topic.
As others pointed out, you can probably solve your problem with a normal DML statement, without any looping involved. But to give you some basics on how to accomplish what you asked for in PL/SQL, here's an example...
DECLARE
CURSOR c IS
SELECT id, aid, type FROM b;
statement VARCHAR2(200);
BEGIN
FOR iterator IN c LOOP
IF iterator.id = iterator.aid THEN
statement := 'UPDATE a SET color = ' || iterator.type || 'WHERE id = ' || iterator.id;
EXECUTE IMMEDIATE statement;
END IF;
END LOOP;
END;
This anonymous PL/SQL block will iterate through each record in table b, and if b.id = b.aid, it will update table a and set a.color = b.type where a.id = b.id.
This seems to be what you were asking for. It's not exactly an efficient way to go about doing things, since you're firing off one DML statement per row in table b that has b.id=b.aid. But I wanted more to give this as a syntax example. This is just one way to iterate through a cursor by the way; you can also explicitly open cursors and fetch records, but it's easier this way if you don't need to do anything but iterate over the entire result set.

Resources