How to set minvalue of sequence to result of a select query? - oracle

I am trying to create a sequence in oracle sql, using the "minvalue" as the result of a select query.
The select query I'm trying to run is:
SELECT
MAX(customer_id) + 1
FROM
customer
I know it's easy with an anonymous pl/sql, but I'd like to find a way which doesn't use pl/sql. Some ideas I've had include the COLUMN command to set a substitution variable, but I'm a little lost as to how to do that.
Thanks in advance!

Like this:
column startval new_value v_startval
SELECT
MAX(customer_id) + 1
FROM
customer;
create sequence customer_seq start with &v_startval.;

Related

Oracle query in stored procedure

i'm working with stored procedures, i have a query that should bring one specific id
SELECT * into SID_INCOMING FROM
(
SELECT SID
FROM TBL_INCOMING
WHERE XKEY = ''||nro_tarjetatmp||'_'||fecha_hr_trasacfinal||'_'||datos_referencia_adquirentetmp||''
AND CODIGO_AUTORIZACION IN(''||codigo_autorizacion||'')
AND MIT IN(''||mit||'')
ORDER BY SID ASC
) WHERE ROWNUM <= 1;
the variables values are in order
4946110112060005_200116_74064350165099586691985
536018
05
when it's executed i get this result
but when i execute the same query with the same parameters i get this result
and this is the one i sould get with the procedure, so why is this happening?
it seems to me, that the sp is not considering the second and third parameter
any help is welcome, thanks
You need to use aliases to avoid your problem with AND MIT IN(''||mit||''): this predicate compares column MIT with itself.
SO just add aliases:
SELECT * into SID_INCOMING FROM
(
SELECT SID
FROM TBL_INCOMING ti
WHERE ti.XKEY = ''||your_proc_name.nro_tarjetatmp||'_'||your_proc_name.fecha_hr_trasacfinal||'_'||your_proc_name.datos_referencia_adquirentetmp||''
AND ti.CODIGO_AUTORIZACION IN(''||your_proc_name.codigo_autorizacion||'')
AND ti.MIT IN(''||your_proc_name.mit||'')
ORDER BY ti.SID ASC
) WHERE ROWNUM <= 1;
Also why do you add ''||? It doesn't add anything into your variables, but forces impicit type conversion in cases when they are not varchar2/char data types.
Could you show the results of "describe TBL_INCOMING" please? Since looks like CODIGO_AUTORIZACION is a number data type and probably MIT is number too.

ExistsNode - What's wrong

I create a table in oracle like this:
CREATE TABLE XML_TEMP (
XML_DATA CLOB
);
INSERT INTO XML_TEMP(XML_DATA) VALUES('
<envCFe versao="0.07" xmlns="http://www.fazenda.sp.gov.br/sat">
<tpAmb>1</tpAmb>
<idLote>4095</idLote>
<cUF>35</cUF>
<LoteCFe>
</LoteCFe>
<nSeg></nSeg>
<dhEnvio>20171101101517</dhEnvio>
</envCFe>
');
When I select the existsNode return 0
SELECT ExistsNode(XMLTYPE(XML_DATA), '/envCFe') HAS, XML_DATA FROM XML_TEMP;
I don't understang what's wrong, someone could help.
The link http://sqlfiddle.com/#!4/c2b9e/5/0 has the SQLFiddle
Your root node has a namespace, so you need to specify that using the optional third argument:
SELECT ExistsNode(XMLTYPE(XML_DATA), '/envCFe',
'xmlns="http://www.fazenda.sp.gov.br/sat"') HAS, XML_DATA
FROM XML_TEMP;
HAS XML_DATA
---------- --------------------------------------------------------------------------------
1
<envCFe versao="0.07" xmlns="http://www.fazenda.sp.gov.br/sat">
<tpAmb>1</tpAmb>
<idLote>4095</idLote>
<cUF>35</cUF>
<LoteCFe>
</LoteCFe>
<nSeg></nSeg>
<dhEnvio>20171101101517</dhEnvio>
</envCFe>
SQL Fiddle
If you intend to use ExistsNode to filter your results, and since that function is deprecated, you could use the XMLExists operator for that scenario instead:
SELECT XML_DATA
FROM XML_TEMP
WHERE XMLExists(
'declare namespace ns="http://www.fazenda.sp.gov.br/sat"; (: :)
/ns:envCFe'
PASSING XMLTYPE(XML_DATA)
);
SQL Fiddle
You can't get the result of that as part of a query result though, as your current query is doing.
You need to add the namespace to existsNode function:
SELECT ExistsNode(XMLTYPE(XML_DATA), '/envCFe', 'xmlns="http://www.fazenda.sp.gov.br/sat"') HAS, XML_DATA FROM XML_TEMP;

if Statement SAP HANA Views

Add following filter on a column in SAP HANA Analytical view using if statement
if(Col1='a') col2=Col2
else if(Col2='b') col2=col2*1
Can someone help to give me syntax for HANA IF statement for following logic?
Why not using the documentation at the first place?
Not really clear what you are trying to do here. Look's like you are calculating something using col2 based on comparison on col1. As View will not allow you to update the value in the column, you will need to create col3 and put there the following:
if("Col1" = 'a',"Col2", if("Col1" = 'b',"Col2" * 1,'not a not b') )
BTW, do you think col2=col2*1 makes any sense?
Is it possible that you (or Shidai) are confusing the IF-Function with the IF-Statement? Both are working differently:
SELECT IF("Col1"=='a', 'aaahhh', 'uhhhhh') FROM DUMMY;
This works just like in an Excel: If Col1 is 'a' then the first value is returned, otherwise the second.
DECLARE x VARCHAR(100);
IF "Col1"='a'
THEN
x := "Col2";
ELSEIF "Col2"='b'
THEN
x := "Col2" * 1;
END IF
This is a control structure and only allowed in a SQLScript block, e.g. a stored procedure or anonymous block. You cannot use it in a simple SELECT statement.
It's not so clear what you are trying to do with assigning to col2, so I used x instead.
Also note:
HANA is case-sensitive. If you want to use the column Col1 you must write "Col1".
There is also CASE, which works similar to the IF-Function.

Oracle: How to create a function returning values for a "SELECT * FROM tab WHERE name IN (function())"

I have a problem which I can't solve. Maybe you have an idea about how to solve it.
I do have a given parameter-table like this:
P_VALUE P_NAME
----------- ----------
X85 A_03
XH1 A_04
XH2 A_04
XH3 A_04
C84 A_05
As you can see there are parameters with multiple entries. At the moment this parameters are used in this way:
SELECT * FROM tablex
WHERE code IN (SELECT p_value
FROM parameter_table
WHERE p_name LIKE 'A_04');
As the query is very big these parameter sub-select are used very often. I was trying to implement a function in Oracle to get my parameters. This works very fine as long as there is just 1 row per parameter. When I want to use it in "IN-Statements", it won't work because functions just return a single value.
--WORKS
SELECT * FROM tablex
WHERE code = (f_get_param('A_03'));
--DOES NOT WORK
SELECT * FROM tablex
WHERE code IN (f_get_param('A_04'));
Please note that I need it for plain SQL statements, so procedures won't work as they are just good for PL/SQL.
I would be really thankful for good ideas or help!
Use collections. Here you have an example http://www.adp-gmbh.ch/ora/plsql/coll/return_table.html
Technically you can achieve using the function this way but doing this will cause index not to be used on code column on tablex and may affect performance .Using function index you can reduce performance impact
CREATE OR REPLACE FUNCTION f_get_param(p_value1 IN VARCHAR2,p_name1 in VARCHAR2) return NUMBER
DETERMINISTIC
IS
l_count NUMBER;
BEGIN
select count(1) into l_count from parameter_table where p_value =p_value1
and p_name=p_name1;
if l_count > 0
then
return 1;
else
return 0;
end if;
end f_get_param;
AND use the select statement like this
SELECT * FROM tablex
WHERE f_get_param(code,'A_04')=1;
EDIT 1:-
Also to reduce the performance impact in database 10.2 and greater If the parameter_table is static you can use the DETERMINISTIC clause in the Function to say that the function returns the same value if called with same parameters every time
Please find the link on the article about using functions in SELECT statement
--DOES NOT WORK
SELECT * FROM tablex
WHERE code IN (f_get_param('A_04'));
-- Try this
SELECT * FROM tablex
WHERE code IN (select * from TABLE(f_get_param('A_04')));
You have to "CAST" a collection onto SQL TABLE.
Also when you use cast you can also use inner joint:
SELECT * FROM tablex join TABLE(f_get_param('A_04') using (code);
I think - generally - your problem is called "Dynamic where clause". Try to search some articles about it on AskTom.
I think the actual solution to your problem is to simply join the two tables and create the appropriate indexes rather than invoking a PL/SQL function at all:
SELECT x.* FROM tablex x, parameter_table p
WHERE x.code = p.p_value
AND p.p_name LIKE '%A_04%';
Note that you also have a semantic error in your LIKE clause. You're not using the % sign therefore your LIKE 'A_04' is just the same as = 'A_04'

Is it possible to join strings in PL/SQL procedure in my sql statement

As the title suggests, I would like to know if it is possible to join the string in a select statement within a PL/SQL procedure.
For example, I have something like this
SELECT FCS.CATEGORY,
FCS.NUMBERS,
FCS.POINTS
WHERE FCS.OBJECT = 'T'
AND FCS.THIS_DB & strSelectedDB &
So, is it possible to do something like this?
Your example is a little confusing. You can concatenate multiple strings using the || operator. But you'd then you'd have to compare the concatenated string to something. You can compare columns to local variables directly, though, i.e.
SELECT fcs.category,
fcs.numbers,
fcs.points
FROM some_table fcs
WHERE fcs.object = 'T'
AND fcs.this_db = strSelectedDB
assuming that strSelectedDB is a local variable in your PL/SQL block.

Resources