I am executing this sql statement dynamically in oracle using EXECUTE IMMEDIATE statement. But when I Do this I get an error 'missing keyword'. I have declare the RULECOUNT variable as NUMBER. When I remove the INTO statement, the sql statement appears to get executed properly.
SELECT COUNT(DISTINCT RULE_ID) INTO RULECOUNT FROM(
SELECT
distinct a.RULE_ID, Rule_Name, Applicability,
Rule_Type, KPI_NAME, BT, DT, Authorised_User,
Rule_Date_of_Creation
from vw_rule_detail_search a WHERE a.Applicability = 'No' order by a.BT
desc);
I don't know what is happening, can anyone good in oracle help me find what I am missing.
I found solution to my problem. I should not have used INTO statement in select statement while executing with execute immediate.
I should have used like this
EXECUTE IMMEDIATE statement INTO RuleCount;
Related
I'm mapping two table in ODI and I have a problem.
i've mapped the source table to the target table (called DM_BUSINESS with the columns BUSINESS_ID, NAME, ADDRESS). After that I've created a procedure with:
UPDATE dm_business SET name = CONCAT(name, CONCAT(' ', address)) WHERE name IN (SELECT name FROM dm_business GROUP BY name HAVING COUNT (business_id)>1);
When I run this query myself, with SQLDeveloper, I have no problem and it all works fine: it adds the address of the business to its name, when there are more than one business with the same name.
When I run the procedure with this task, it gives me error ORA-00933: SQL command not properly ended. I have choosen "Oracle" as target technology. What do I do wrong?
Can you help me? Thank you very much.
You should remove the semicolon to run it as an SQL statement or wrap your UPDATE with BEGIN..END to run it as a PL/SQL block:
BEGIN
UPDATE ... ;
END;
I am trying to use a simple IF ELSE query to test a feature with DB2 SQL. However when I attempt to execute it, I run into an error stating that I am not allowed to execute DDL statements.
What is throwing me off is that as far as I know, only database structure altering statements are considered DDL statements.
What gives?
Code:
IF 'True' = 'True' THEN
SELECT * FROM RM_TRANSACTION
FETCH FIRST 2 ROWS ONLY
FOR READ ONLY WITH UR
ELSE
SELECT * FROM RM_TRANSACTION
FETCH FIRST 4 ROWS ONLY
FOR READ ONLY WITH UR
END IF
https://imgur.com/a/58RYjpu
The problem is that you can’t ‘select to nowhere’ in a compound statement in DB2. Db2 CLP can return you the result set of a single sql statement, but it doesn’t try to do the same for select statements in a compound statement.
If you want to print the result set from a select statement in a compound statement, you can, for example, declare a cursor, fetch it in a loop, and use dbms_output.put_line calls to print the values of variables.
I am inside a SQLScript procedure and would like to return the last SQL Statement from this procedure, like the last rowcount:
/********* Begin Procedure Script ************/
BEGIN
select 1 as "my_data" from dummy;
select '::last_SQL' as "last executed sql" from dummy;
select ::ROWCOUNT as "rowcount" from dummy;
END;
/********* End Procedure Script ************/
The column "last executed SQL" should be populated with select 1 as "my_data" from dummy in this case. Is there any variable holding the last statement (or any easy way to retrieve the query plan)?
Maybe you can query sys.m_sql_plan_cache system view
Please check following SELECT statement
select
statement_string, last_execution_timestamp
from SYS.M_SQL_PLAN_CACHE
where user_name = 'KODYAZ'
order by last_execution_timestamp desc;
I believe you can improve the query by introducing new filter criteria.
There is no way to programmatically get the last executed SQL statement in SQLScript. This is due to the fact that not all statements will be executed in the form and order as they appear in the source code.
If you want to analyse the performance of a procedure, you can run PlanViz against the procedure call. Generally, there is no such thing as "the performance of a procedure/function" as they always occur in a specific context.
If used within a SQL query, things like query-transformation can radically change the way certain results are computed.
Let's say sStorecode is: 00020
The following just executes the select statement to get the accountid at another database STORE in the table STOREINFO, for example connecting to p008081 (where 008 is the substring of the sStorecode and 081 is what I am joining below),
but I am getting errors for some reason (Invalid SQL Statement), can someone help?
EXECUTE IMMEDIATE 'select AccountID from STOREINFO#STORE.p'||substr(sSTORECODE,3,3)||'081';
Thanks in advance!
EXECUTE IMMEDIATE is a PL/SQL command. In PL/SQL, the result of a SELECT statement needs to go somewhere. But it your statement, you don't specify where it should go.
So if you expect a single row, you could write:
EXECUTE IMMEDIATE 'select AccountID from STOREINFO#STORE.p'||substr(sSTORECODE,3,3)||'081'
INTO l_account_id;
l_account_id is a local PL/SQL variable.
If you expect several row, you could use
EXECUTE IMMEDATE ... BULK COLLECT INTO l_account_tab;
l_account_tab is a PL/SQL collection variable.
Or if you want to work with cursors, you can write:
OPEN account_id_cv FOR 'select AccountID from STOREINFO#STORE.p'||substr(sSTORECODE,3,3)||'081';
account_id_cv is a REF CURSOR variable.
Seems like that should be something like this instead:
'select AccountID from STORE.STOREINFO WHERE sStorecode = ''p'||substr(sSTORECODE,3,3)||'081''';
ALTER PROCEDURE [dbo].[HO_GetListOfLeaguesPerLocation]
AS
BEGIN
SELECT DISTINCT LeagueID, (EXEC dbo.HO_GetLeagueNumOfMatches LeagueID)
FROM Games
END
I am getting an error:
Incorrect syntax near the Keyword EXEC.
When I run this query alone, without anything else, it goes well.
I also tried:
SELECT DISTINCT LeagueID, dbo.HO_GetLeagueNumOfMatches(LeagueID)
AND:
SELECT DISTINCT LeagueID, SELECT * FROM dbo.HO_GetLeagueNumOfMatches(LeagueID)
AND:
SELECT DISTINCT LeagueID, SELECT dbo.HO_GetLeagueNumOfMatches(LeagueID)
None worked.
Any idea how should I run this query?
You need to use a (scalar) function for this. Stored procedures cannot be used here.
(You can not use APPLY with a stored procedure.)