I have many columns in table of which 2 columns are
A : number
B : varchar
I am trying to insert value in B based on value of A from java.
insert into table(A,B) values (? , decode('A',110,'ABC',NA));
this gives me an error : illegal number
So i tried the below :
insert into table(A,B) values (? , decode('A','110','ABC','NA'));
This always inserts NA in the column.
Can someone please help me with this ?
Instead of using 'A' you should provide the content of variable A
Related
I need your help to frame a SQL query in Oracle . I am creating a XML type Table in Oracle Database and making one column of it a Multi Value Field. The data given to the Multi Value column for the records are of different cases with Multi Value and Sub Value. (Ex : ( “ ” : #VM: “ 10 ”) , (“ 12” : #VM: “ ”) , (“ ” : #SM: “ 8 ”) , (“ 6” : #SM: “ ”)) . When a SQL query is executed to select the records for which the Multi value column is equated to Null, the record where the first value of #SM is NULL is not getting selected ie. (“ ” : #SM: “ 8 ”).
Table Creation :
CREATE TABLE "F_TESTMV" (RECID VARCHAR2(255) NOT NULL PRIMARY KEY, XMLRECORD XMLTYPE) XMLTYPE COLUMN XMLRECORD STORE AS CLOB
CREATE TABLE D_F_TESTMV (RECID VARCHAR2(255) NOT NULL PRIMARY KEY, XMLRECORD CLOB)
Insert Statement :
INSERT INTO TAFJ24.F_TESTMV (RECID,XMLRECORD) VALUES
('11000',TO_CLOB(' <row id='11000'><c1></c1><c1 m='2'>US</c1></row>')),
('12000',TO_CLOB(' <row id='12000'><c1>US</c1><c1 m='2'></c1></row>')),
('13000',TO_CLOB(' <row id='13000'><c1>GB</c1><c1 m='2'>US</c1></row>')),
('14000',TO_CLOB(' <row id='14000'><c1>US</c1><c1 m='2'>GB</c1></row>'));
('15000',TO_CLOB('<row id='15000'><c1>OF</c1><c1 m='2'></c1></row>')),
('16000',TO_CLOB('<row id='16000'><c2>gb</c2></row>')),
('17000',TO_CLOB(<row id='17000'><c1>US</c1><c1 m='1' s='2'></c1></row>'));
('18000',TO_CLOB('<row id='18000'><c1 m='1' s='2'>GB</c1></row>'));
View Definition:
SELECT a.RECID, a.XMLRECORD "THE_RECORD",extractValue(a.XMLRECORD,'/row/c1[position()=1]') "COUNTRY" ,extract(a.XMLRECORD,'/row/c1') "COUNTRY_1" FROM "F_TESTMV"
Select Query :
SELECT RECID FROM "V_F_TESTMV" WHERE ( XMLEXISTS('$t[/row/c1[not(text())][not(*)] or /row/c1/text()= "" or fn:not(/row/c1/text()) ] ' PASSING "THE_RECORD" as "t") )
Current Result :
From the eight records in the table, The above SQL query selects 5 records, with RECID 11000,12000,15000,16000,17000. It does not select RECID 18000 even when the first part of Sub Value is NULL in the record.
EXPECTED RESULT
The Query should select 6 Records . The records with RECID 11000,12000,15000,16000,17000 and 18000 should be selected, as RECID 18000 also has first part of Sub Value as NULL.
Kindly provide your Suggestion on the SQL Query required that will fetch the above records when the name of the Column with Multi Value field is Equated to NULL.
Thanks
I am unable to append data to tables that contain an array column using insert into statements; the data type is array < varchar(200) >
Using jodbc I am unable to insert values into an array column by values like :
INSERT INTO demo.table (codes) VALUES (['a','b']);
does not recognises the "[" or "{" signs.
Using the array function like ...
INSERT INTO demo.table (codes) VALUES (array('a','b'));
I get the following error using array function:
Unable to create temp file for insert values Expression of type TOK_FUNCTION not supported in insert/values
Tried the workaround...
INSERT into demo.table (codes) select array('a','b');
unsuccessfully:
Failed to recognize predicate '<EOF>'. Failed rule: 'regularBody' in statement
How can I load array data into columns using jdbc ?
My Table has two columns: a STRING, b ARRAY<STRING>.
When I use #Kishore Kumar Suthar's method, I got this:
FAILED: ParseException line 1:33 cannot recognize input near '(' 'a' ',' in statement
But I find another way, and it works for me:
INSERT INTO test.table
SELECT "test1", ARRAY("123", "456", "789")
FROM dummy LIMIT 1;
dummy is any table which has atleast one row.
make a dummy table which has atleast one row.
INSERT INTO demo.table (codes) VALUES (array('a','b')) from dummy limit 1;
hive> select codes demo.table;
OK
["a","b"]
Time taken: 0.088 seconds, Fetched: 1 row(s)
Suppose I have a table employee containing the fields ID and Name.
I create another table employee_address with fields ID and Address. Address is a complex data of type array(string).
Here is how I can insert values into it:
insert into table employee_address select 1, 'Mark', 'Evans', ARRAY('NewYork','11th
avenue') from employee limit 1;
Here the table employee just acts as a dummy table. No data is copied from it. Its schema may not match employee_address. It doesn't matter.
I have a Table in Oracle like:
create table TEST (num number(7,2))
And i want to insert a integer into that table like that:
insert into TEST (num) values (5555522)
I'am searching for a way to convert the integer "5555522" automatically into the number(5,2) value so that the record contains 55555.22 instead of 55555. Is there some kind of function like "setscale(5555522, 2)" which just interprets the last 2 numbers as decimal place?
I already checked the to_number() Funktion in the Oracle docs. But it expects a proper formatted inputstring like to_number('55555.22', 99999D99) which is not suitable in my case.
Oracle should do that implicitly for you, after you divided by 100 :
insert into TEST (num) values (5555522/100)
EDIT : Like you said, it wont fit, you would need to change your table in order to do it :
create table TEST (num number(7,2))
column x (Not NULL) | column z
1............................| P
0............................| T
I got a check constraint : column x in (1, 0), so the column only accept value 1 and 0
what I want which is really weird (I don't want a procedure or a trigger). I want a way like constraints to change the nullable of the column z by the value that the column x have
example:
if column x has value of 1 column z should be nullable,
if column x have value of 0 column z should be Not null with default N
Why do I want that? I am using oracle and sybase, the problem in sybase if I add column with out specifiying if null or not , automatically it will be not null(add test varchar(12) <-- this will be Null in sybase by default and null by oracle , so by that column I want to specify if its syabse or oracle)
Maybe a procedure will be better , but I am searching if there is another way for my problem.
I would start with a check constraint for integrity:
ALTER TABLE <table> ADD CONSTRAINT chk_x_z CHECK (X = 1 OR Z IS NOT NULL)
This will make sure that your business rule is always enforced.
You will need either a procedure or a trigger to implement your custom default N.
insert values in table contain 5 column some times insert with 3 columns and some time all 5 columns data come from front end is not constant dependent upon user insert in front end?
if 4 column value no value for one column and 3 column and 2 column no value also we need to insert value in the table ?
Specify all the columns in your insert statement. The parameters to which you pass null values will be stored as null in the table. There's no need to change your insert statement based on which parameters have non-null values.
create or replace procedure insert_stuff(i_input1 in varchar2, i_input2 in
varchar2... i_input10 in varchar2)is
begin
insert into my_table (col1, col2... col10) values(i_input1, i_input2...
i_input10);
end;
If you are looking for query, then use syntax:
insert into tablename(columnlist seperated by comma) values (values list seperated by comma).
For Example:
insert into TableName(column1_name,column2_name) values(val1,val2);
Use your stored procedure in this way:
create or replace
FUNCTION testFunction
(
field1 IN NUMBER DEFAULT NULL,
field2 IN NUMBER DEFAULT NULL,
field3 IN NUMBER DEFAULT NULL
)
RETURN NUMBER
AS
BEGIN
// your insert query goes here.
END;
If the value for any of the field is not assigned then, it will automatically assign the default value i.e. NULL. Just have a check and i hope it will help you.:)