I know I can use CASE statement inside VALUES part of an insert statement but I am a bit confused.
I have a statement like,
You can try also a procedure:
create or replace procedure insert_XYZ (P_ED_MSISDN IN VARCHAR2,
P_ED_OTHER_PARTY IN VARCHAR2) is
begin
INSERT INTO TABLE_XYZ ( ED_MSISDN,
ED_OTHER_PARTY,
ED_DURATION)
VALUES (P_ED_MSISDN ,
P_ED_OTHER_PARTY ,
CASE
WHEN P_ED_OTHER_PARTY = '6598898745' THEN
'9999999'
ELSE
'88888'
END);
END;
Here's a query structure that you can use (using JohnnyBeGoody's suggestion of using a SELECT statement to select the values).
INSERT INTO TABLE_XYZ (ED_MSISDN, ED_OTHER_PARTY, ED_DURATION)
SELECT
'2054896545' ED_MSISDN,
'6598898745' ED_OTHER_PARTY,
CASE
WHEN ED_OTHER_PARTY = '6598898745' THEN '9999999'
ELSE '88888'
END ED_DURATION
FROM DUAL;
You cannot self-reference a column in an insert statement - that would cause an "ORA-00984: column not allowed here" error.
You could, however, use a before insert trigger to achieve the same functionality:
CREATE OR REPLACE TRIGGER table_xyz_tr
BEFORE INSERT ON table_xyz
FOR EACH ROW
NEW.ed_duration = CASE NEW.ed_other_party
WHEN '6598898745' THEN '9999999'
ELSE '88888' END;
END;
Related
I am trying to write a "case" statement inside a "where clause" which has an "in" statement in the "then" part. Basically, the following code is what I want, but it's not the Oracle correct syntax. Does anybody have any idea what the correct syntax should be like?
create or replace PROCEDURE "Test"
(
Type in number
)
as
begin
select Id, Name, AccCode from myTable
where case Type
when 0 then AccCode in (130,131)
when 1 then AccCode in (230,231);
end;
I don't think you want a case statement. You probably just want
where (accCode in (130,131) and type = 1)
or (accCode in (230,231) and type = 0)
If you really want to use a case statement, you could say
where (case when accCode in (130,131) then 1
when accCode in (230,231) then 0
else null
end) = type
But that will not be as readable nor will it be as easy for the optimizer to find a good execution plan.
I have created some variables. I want to use the variable name as an input of another query.
Is there any method to get a local variable name as a string value in Oracle.
Example Scenario
declare
FASTFUNDS VARCHAR(100);
begin
FASTFUNDS := 'TEST001';
SELECT v_variable, v_value FROM v_Table WHERE v_variable = FASTFUNDS.toString()
Results
v_variable v_value
FASTFUNDS TEST001
This isn't a Java code, so there isn't any String type, but VARCHAR (as you defined)
Just remove .toString() and it'll be a valid statement:
SELECT v_variable, v_value FROM v_Table WHERE v_variable = FASTFUNDS;
declare
FASTFUNDS VARCHAR(100);
stmt varchar2(50);
begin
FASTFUNDS := 'TEST001';
stmt := 'SELECT v_variable, v_value FROM v_Table WHERE v_variable = '|| FASTFUNDS;
EXECUTE IMMEDIATE stmt;
end;
i dont know your objective, but id do something like this.
How to export query of "DESCRIBE table" to excel file?
For example :
I have query like that "DESCRIBE Mst_Fi_Bond_Issuers;" and i have result like that :
I want export that automatically to excel table. Please help me. Thanks..
You could use an SQL block like this: [please edit as you see fit]
DECLARE
createdir VARCHAR2(2000);
directory NUMBER;
filen VARCHAR2(50);
dirn VARCHAR2(50);
filedat UTL_FILE.file_type;
BEGIN
dirn := 'DESC';
filen := 'yourfilename.csv';
createdir := q'{create directory DESC as '[your directory]'}';
execute immediate (createdir);
filedat := UTL_FILE.fopen(dirn, filen, 'W', 32767);
UTL_FILE.put_line (filedat, 'NAME;TYPE');
for rowdat in (select column_name || ';' || data_type currow from dba_tab_columns)
LOOP
UTL_FILE.put_line (filedat, rowdat.currow);
END LOOP;
UTL_FILE.fclose (filedat);
END;
/
As jera already said, it´s much better to do a query and write results to a file, because you have much more flexibility in gathering informations and formatting output.
But maybe due to restrictions, your not able to access the dba_... Objects.
Instead of that, have a look on the USER_... - Views, which are always available to a oracle-user
Everything you want to know can be found there (f.e. like USER_TABLES. USER_TAB_COLS, USER_PROCEDURES, etc.).
this might be a simple one. Basically in PL/SQL in Oracle I am selecting from another database:
select * From Store#dbstore.p009061;
What I want is that the value 061 will be coming from a variable name (myStore).
so it will be like this:
select * from STORE#DBSTORE.P||myStore||'081';
remember myStore above will hold the value 061.
But the above doesn't work? Can someone help? Thanks
hmmm, you can't do this directly in a query, you must use dynamic sql.
something like
declare
myStore varchar2(10):='061';
begin
EXECUTE IMMEDIATE 'SELECT * FROM STORE#DBSTORE.P'||myStore||'081';
end;
Or maybe it should be like this?
declare
myStore varchar2(10):='061';
sStoreCode varchar2(10):='061';
begin
EXECUTE IMMEDIATE 'SELECT * FROM STORE#BNSTORE.P'||myStore
||'081 where storecode='||sStoreCode;
end;
I'd like to make a query insert:
INSERT INTO A_TABLE (BLOB_FIELD) VALUES(MY_BLOB_VAL)
but I have only string values in delphi for ex:
procedure INSERT_BLOB_QUERY
var
query:String;
my_blob_val:String;
begin
my_blob_val := 'a blob string to be inserted';
query := 'INSERT INTO A_TABLE (BLOB_FIELD) VALUES(' + my_blob_val + ')';
// to execute a query....
end;
The problem that occours is string to blob conversion.
So how to I insert a string in a interbase blob field???
Like this:
procedure INSERT_BLOB_QUERY;
begin
query.SQL.Text := 'INSERT INTO A_TABLE (BLOB_FIELD) VALUES (:VAL)';
query.ParamByName('VAL').AsString := 'a blob string to be inserted';
end;
Your code doesn't work because you're not passing the string as a parameter, you're passing it as part of the query. If you do that, you obviously need to QUOTE it: the way you're doing it Interbase will try to interpret it as SQL commands, not as a literal string to be inserted in a db column.
None the less, don't go for quoting. It's always better to use parameters, it's safer!