Executing select statements with union all and prepare statements on Oracle - oracle

I need to execute the following UNION ALL Select statements, I am using bind variables for the WHERE conditions. I am using PyODBC for Oracle. While the code executes and returns results, it takes a lot of time if compared to running it from TOAD directly. Any Ideas what is the problem?
query = """
SELECT concat('Abstract ',display_order), abstract_text as field1,
abstract_source_code, display_order
From abstract
WHERE control_number = ? and control_level = ?
UNION ALL
SELECT 'cTitle', chapter_title, null as col2, null as col3
From chapter
WHERE control_number = ? and control_level = ?
"""
result = cursor.execute(query, ['201728871', '005', '201728871', '005']).fetchall()
print(result)

Related

How to do union of multiple tables with where clause and order by in GORM (golang)

I'm trying to do union of multiple tables with selected columns and run where clause and order by clause on the resultset. How do I write this in GORM (Golang)
I tried the following snippet, but didn't run the where clause and order by clause in the DB query:
var union []map[string]interface{}
database.CONNECTION.Raw("? UNION ?",
database.CONNECTION.Select(ContentAttributes).Model(&model1{}),
database.CONNECTION.Select(ContentAttributes).Model(&model2{}),
).Where("id > ?", 1).Order("Name").Scan(&union)
N.B. ContentAttributes is a slice of string which contains the attributes I want to select.
It's running the following query:
SELECT "id","name","created_at","updated_at" FROM "model1" WHERE "model1"."deleted_at" IS NULL UNION SELECT "id","name","created_at","updated_at" FROM "model2" WHERE "model2"."deleted_at" IS NULL
I expected this to run the where condition and the order by clause on the union resultset. But it just did union and collected the results in the union variable. Please suggest a way to do this.
Not sure if this is the cleanest way to do it but it works.
var db = database.CONNECTION
var union []map[string]interface{}
var raw = "SELECT * (? UNION ?) union WHERE union.id > ? ORDER BY union.name"
db.Raw(raw,
db.Select("*").Model(&model1{}),
db.Select("*").Model(&model2{}),
1
).Scan(&union)

can I use `=` sign operator with sub-query instead of `IN`

I am just wondering if use = sign operator with sub-query instead of IN
Is it correct way ? and meet the oracle standard ?
Example
select column_name from my_table_1 where id = (select max(id) from my_table_2);
The Difference is related to the number of rows returned. If you have only one row returned from nested sql you may prefer both = or in operators. But if multiple rows returned from nested query, use in operator.
So, in your sql example you may prefer using any of the operators. Since, max functions returns only one row.
As you are fetching maximum value from subquery to compare with id, Both(= and IN )will work fine. But If you are trying to fetch more than one row then you have to use IN keyword.
If you have 1 result in sub query you are fine with using = sign, except when data type is wrong, for example , checking with same data type of dummy VARCHAR2(1)
select * from dual where 'X' = (select max(dual.dummy) from dual);
Is similar to using in (also same explain plain)
select * from dual where 'X' in (select max(dual.dummy) from dual);
But checking with different/wrong data type will result with exception ORA-01722 Invalid number
select * from dual where 1 =(select max(dual.dummy) from dual);

Using IF statements in Oracle when trying to return data

How do I return data out of IF statements? I have a IF statement which is meant to return a different result dependent of the result of that statement.
IF :Value = 1 THEN
SELECT Name FROM TABLE_A
ELSEIF :Value = 2 THEN
SELECT Name FROM TABLE_B
ELSEIF :Value = 3 THEN
SELECT Name FROM TABLE_C
but this doesn't work. It expects an INTO statement in those selects. I suspect this is because Oracle can't return out of a block?
Is there a quicker way to return those select statements without creating table variables to store the data or messing around with functions?
You can do this by plain SQL:
SELECT
':Value' user_input,
CASE
WHEN ':Value' IN('a1','a2','a3')
THEN (select name from table_a)
WHEN ':Value' = 'i'
THEN (select name from table_b)
END AS output
FROM
dual
(good info about case)
If you want more than one result in your cases, then you may opt to an intelligent UNION:
SELECT t1_Col_1, t1_Col_2,'&VALUE' from TABLE_1
WHERE '&VALUE' = '1'
UNION ALL
SELECT t2_Col_1, t2_Col_2,'&VALUE' from TABLE_2
WHERE '&VALUE' = '2'
In this solution, types and number of tx_Coln must be the same.

Update query in pre sql of source qualifier is running but not commiting the table?

Below given query is executed using Source qualifier in Informatica.
Pre SQL has the below query.
Update CTLSTAGE.VMR226DEV_HSD_SUB A
set A.ATM_IP_IND = 'ATM'
where (A.SW_SITE_CD,A.DEV_NM) = (
select SW_SITE_CD,DEV_NM
from CTLSTAGE.ATM_IP_REF
where FTTC_E_IND = 'Y'
) ;
COMMIT ;
Which is running but not committing in database.
I presume it's running fine. Add additional statements to diagnose, like:
insert into someAuditTable
select SW_SITE_CD,DEV_NM from CTLSTAGE.ATM_IP_REF where FTTC_E_IND = 'Y'
and
insert into someAuditTable2
select * from CTLSTAGE.VMR226DEV_HSD_SUB A where (A.SW_SITE_CD,A.DEV_NM) = (
select SW_SITE_CD,DEV_NM
from CTLSTAGE.ATM_IP_REF
where FTTC_E_IND = 'Y'
) ;
BTW:
Does this kind of tuple comparison work fine? Looks odd to me:
(A.SW_SITE_CD,A.DEV_NM) = (select SW_SITE_CD,DEV_NM ...)

Automate scripts

I have bunch of SQL statements and I want to put them in a script and run them together and produce an EXCEL (*.xls) file; how can I do that in toad?
Example of script
SELECT SUM(CASE
WHEN dvdn.ACDCALLS = (dvdn.ABNCALLS + dvdn.ABNCALLS1)
THEN dvdn.ABNCALLS
ELSE dvdn.ACDCALLS + dvdn.ABNCALLS - dvdn.ABNCALLS1
END) AS InboundCalls
from ccrdba.CMS_DVDN dvdn , ccrdba.VDN_DIM dim
where DIM.DW_VDN_ID = DVDN.DW_VDN_ID (+)
And DETAIL_GROUP like 'HD%'
and vdn_name not like '%ASM%'
And DIM.AGENT_GROUP <>'Smart Specialist'
And row_date BETWEEN TO_DATE('2012-SEP-01', 'YYYY-MON-DD')AND TO_DATE('2012-SEP-30', 'YYYY-MON-DD')
Second Statement
select count(*)
from xx_new.xx_online_registrations#appsread.prd.com oreg
where oreg.client_party_id = 141043767
and oreg.cti_id IS NOT NULL
and oreg.created_by = 'IVR-INTERACTIONS'
and TRUNC(CREATION_DATE) between '01-SEP-2012' and '30-SEP-2012'
and status = 'POSTED'
and last_updated_by = 'IVR-INTERACTIONS'
Like above there are about 10 SQL statements. How do I combine them together in ORACLE?
You can try "union all",
select col1 from table1
union all
select col1 from table2
union all
...

Resources