Oracle PL/SQL Query With Dynamic Parameters in Where Clause - oracle

I'm trying to write a dynamic query that could have a different amount of parameters of different type. The only issue I'm having is handling if the value is a string therefore needing single quotes around it. I am using the value of a field called key_ref_ to determine what my where clause will look like. Some examples are:
LINE_NO=1^ORDER_NO=P6002277^RECEIPT_NO=1^RELEASE_NO=1^
PART_NO=221091^PART_REV=R02^
At the moment I am replacing the '^' with ' and ' like this:
REPLACE( key_ref_, '^' ,' and ' );
Then I'm trying to create the dynamic query like this:
EXECUTE IMMEDIATE
'select '||column_name_||' into column_ from '||base_table_||' where '||
key_ref_ || 'rownum = 1';
This won't work in cases where the value is not a number.
Also I only added "rownum = 1" to handle the extra 'and' at the end instead of removing the last occurence.

If the input will not have the tild symbol(~) then you can try the below code.
if the input has tild, you can replace it with some other value which should not be there in input
considering the input provided in the example..
LINE_NO=1^ORDER_NO=P6002277^RECEIPT_NO=1^RELEASE_NO=1^PART_NO=221091^PART_REV=R02^
use the below code
replace(replace(replace('LINE_NO=1^ORDER_NO=P6002277^RECEIPT_NO=1^RELEASE_NO=1^PART_NO=221091^PART_REV=R02^','^','~ and '),'=','=~'),'~',q'[']')
and the result would be
LINE_NO='1' and ORDER_NO='P6002277' and RECEIPT_NO='1' and RELEASE_NO='1' and PART_NO='221091' and PART_REV='R02' and
System will type cast the number fields so, there would not be any issue.

Related

How can I plug in this simple SQL logic to an already existing script correctly?

Currently I am working on a sql script in informatica powercenter, the current script is this
ltrim(rtrim(Sur_Co))||
ltrim(rtrim(Seqe_N))||
rpad(ltrim(rtrim(Ct_Num)),30,' ')||
**iif(isnull(ltrim(rtrim(ent_Tx_ID))),rpad(' ',20,' '),
rpad(ltrim(rtrim(ent_Tx_ID)),20,' '))||**
**iif(isnull(ltrim(rtrim(ent_Tx_ID_Q))),rpad(' ',2,' '),
rpad(ltrim(rtrim(ent_Tx_ID_Q)),2,' '))||**
ETC
I dont need the iif is null logic for ent_Tx_ID and ent_Tx_ID_Q anymore,
I just need to always give the value of empty spaces for these two columns specifically like this,
**ent_Tx_ID = RPAD(' ',20,' ')
ent_Tx_ID_Q = RPAD(' ',2,' ')**
how would I edit this logic into that script?
You can use below concatenations directly in informatica,
ltrim(rtrim(Sur_Co))||
ltrim(rtrim(Seqe_N))||
rpad(ltrim(rtrim(Ct_Num)),30,' ')||
' 20 ' || ' 30 '

Oracle SQL: Using Replace function while Inserting

I have a query like this:
INSERT INTO TAB_AUTOCRCMTREQUESTS
(RequestOrigin, RequestKey, CommentText) VALUES ('Tracker', 'OPM03865_0', '[Orange.Security.OrangePrincipal]
em[u02650791]okok
it's friday!')
As expected it is throwing an error of missing comma, due to this it's friday! which has a single quote.
I want to remove this single quote while inserting using Replace function.
How can this be done?
Reason for error is because of the single Quote. In order to correct it, you shall not remove the single quote instead you need to add one more i.e. you need to make it's friday to it''s friday while inserting.
If you need to replace it for sure, then try the below code :
insert into Blagh values(REPLACE('it''s friday', '''', ''),12);
I would suggest using Oracle q quote.
Example:
INSERT INTO TAB_AUTOCRCMTREQUESTS (RequestOrigin, RequestKey, CommentText)
VALUES ('Tracker', 'OPM03865_0',
q'{[Orange.Security.OrangePrincipal] em[u02650791]okok it's friday!}')
You can read about q quote here.
To shorten this article you will follow this format: q'{your string here}' where "{" represents the starting delimiter, and "}" represents the ending delimiter. Oracle automatically recognizes "paired" delimiters, such as [], {}, (), and <>. If you want to use some other character as your start delimiter and it doesn't have a "natural" partner for termination, you must use the same character for start and end delimiters.
Obviously you can't user [] delimiters because you have this in your queries. I sugest using {} delimiters.
Of course you can use double qoute in it it''s with replace. You can omit last parameter in replace because it isn't mandatory and without it it automatically will remove ' character.
INSERT INTO TAB_AUTOCRCMTREQUESTS (CommentText) VALUES (REPLACE('...it''s friday!', ''''))
Single quotes are escaped by doubling them up
INSERT INTO Blagh VALUES(REPLACE('it''s friday', '''', ''),12);
You can try this, (sorry but I don't know why q'[ ] works)
INSERT INTO TAB_AUTOCRCMTREQUESTS
(RequestOrigin, RequestKey, CommentText) VALUES ('Tracker', 'OPM03865_0', q'[[Orange.Security.OrangePrincipal] em[u02650791]okok it's friday!]')
I just got the q'[] from this link Oracle pl-sql escape character (for a " ' ") - this question could be a possible duplicate

Oracle/ PLSQL condition like 'string' || '%'

Why this
SELECT * FROM STUDENT
WHERE FULLNAME LIKE 'Nguyen' || '%'
as the same
SELECT * FROM STUDENT
WHERE FULLNAME LIKE 'Nguyen%'
How does the first one work?
|| is concatenation operator. Oracle will first perform concatenation and then will use LIKE to match the pattern. Hence operationally it will be same as the second one.
However you should use the second one as it will be more efficient in performance and easy to read.
First one has extra overhead to append two strings before using LIKE to match the pattern.
the first one catenate 'Nguyen' and '%' through the pipes '||' in first place.
Because you don't have any space like 'Nguyen ' or ' %', it's the same as 'Nguyen%'.
There is no difference between these two:
Here the double pipe(||) is just a concatenation of expression.
Before db evaluate against like parameter, it concatenates
Hence both of those are same.
Both are same, as || operator concats 'Nguyen' and '%', it will be more helpful if you want to concat parameterize variable like below
SELECT * FROM STUDENT
WHERE FULLNAME LIKE :name || '%'

oracle regexp_replace delete last occurrence of special character

I have a pl sql string as follows :
String := 'ctx_ddl.add_stopword(''"SHARK_IDX19_SPL"'',''can'');
create index "SCOTT"."SHARK_IDX2"
on "SCOTT"."SHARK2"
("DOC")
indextype is ctxsys.context
parameters(''
datastore "SHARK_IDX2_DST"
filter "SHARK_IDX2_FIL"
section group "SHARK_IDX2_SGP"
lexer "SHARK_IDX2_LEX"
wordlist "SHARK_IDX2_WDL"
stoplist "SHARK_IDX2_SPL"
storage "SHARK_IDX2_STO"
sync (every "SYSDATE+(1/1)" memory 67108864)
'')
/
';
I have to get search the final occurrence of '/' and add ';' to it. Also I need to escape the quotes preset in parameters ('') to have extra quotes. I need output like
String := 'ctx_ddl.add_stopword(''"SHARK_IDX19_SPL"'',''can'');
create index "SCOTT"."SHARK_IDX2"
on "SCOTT"."SHARK2"
("DOC")
indextype is ctxsys.context
parameters(''''
datastore "SHARK_IDX2_DST"
filter "SHARK_IDX2_FIL"
section group "SHARK_IDX2_SGP"
lexer "SHARK_IDX2_LEX"
wordlist "SHARK_IDX2_WDL"
stoplist "SHARK_IDX2_SPL"
storage "SHARK_IDX2_STO"
sync (every "SYSDATE+(1/1)" memory 67108864)
'''')
/;
';
Any help.
There's an age-old saying: "Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems"
Unless you are confronted by a problem that truly requires regular expressions, I'd recommend working with basic string manipulation functions.
Semi-colon:
Use INSTR to find last occurence of '/', call this P1.
Result = Substr from position 1 through P1||';'||substr from P1+1 through to end-of-string
Parameters substitution:
Use INSTR to find where parameter list starts (i.e. find "parameters(" in your string) and ends (presumably the last closing parenthesis ")" in your string). Call these P2 and P3.
Result = substr from 1 through P2 || REPLACE(substr from P2+1 through P3-1,'''','''''''') || substr from P3 to end-of-string

addig char in select statement

I want to add char in Select statement.
Ex:
SELECT '.' + OUTTRUNK as NUMBER
Expected Result:
.348977834
.456935534
.090922834
.234999734
How can I do this?
Thanks.
Try the more SQL-ish:
SELECT '.' || OUTTRUNK as NUMBER
but keep in mind that, if OUTRUNK is a numeric rather than string type, you'll probably find that 090922834 will actually be 90922834 and will render as .90922834, not what you want.
If that's the case, you're probably looking for something more like:
SELECT OUTTRUNK / 1000000000 as NUMBER
(check the number of zeros there, I tried to get it right but testing is rightly your concern).

Resources