I'm trying to create an SQL script to automatize inserting of values into a table.
I have a table table1 with 2 columns: key, value.
I want to insert a few rows:
INSERT INTO table1 (key, value) VALUES ("tomato1","random_value_1")
INSERT INTO table1 (key, value) VALUES ("tomato2","random_value_2")
INSERT INTO table1 (key, value) VALUES ("tomato3","random_value_3")
INSERT INTO table1 (key, value) VALUES ("tomato4","random_value_4")
How can I put this into a shell script that I can execute from command line.
Thanks
save as a file with .sql extension.
then run from the command line with a sql connection tool like SQLPLus (you don't indicate which database you are on)
You should also combine inserts to the same table into one, as it is much faster, like so:
INSERT INTO table1
(key, value) VALUES
("tomato1","$1"),
("tomato2","$2"),
("tomato3","$3"),
("tomato4","$4")
Related
I'm trying to copy data from a table called accounts into an empty table called accounts_by_area_code. I have the following fields in accounts_by_area_code: acct_num INT, first_name STRING, last_name STRING, phone_number STRING. The table is partitioned by areacode (the first 3 digits of phone_number.
I need to use a SELECT statement to extract the area code into an INSERT INTO TABLE command to copy the speciļ¬ed columns to the new table, dynamically partitioning by area code.
This is my last attempt:
impala-shell -q "INSERT INTO TABLE accounts_by_areacode (acct_num, first_name, last_name, phone_number, areacode) PARTITION (areacode) SELECT STRLEFT (phone_number,3) AS areacode FROM accounts;"
This generates ERROR: AnalysisException: Column permutation and PARTITION clause mention more columns (5) than the SELECT / VALUES clause and PARTITION clause return (1). I'm not convinced I have even the basic syntax correct so any help would be great as I'm new to Impala.
Impala creates partitions dynamically based on data. So not sure why you want to create an empty table with partitions because it will be auto created while inserting new data.
Still, I think you can create empty table with partitions like this-
impala-shell -q "INSERT INTO TABLE accounts_by_areacode (acct_num) PARTITION (areacode)
SELECT CAST(NULL as STRING), STRLEFT (phone_number,3) AS areacode FROM accounts;"
I am using Toad for oracle 12c. I need to copy a table and data (40M) from one shcema to another (prod to test). However there is an unique key(not the PK for this table) called record_Id col which has something data like this 3.000*******19E15. About 2M rows has same numbers(I believe its because very large number) which are unique in prod. When I try to copy it violets the unique key of that col. I am using toad "export data to another schema" function to copy the data.
when I execute query in prod
select count(*) from table_name
OR
select count(distinct(record_id) from table_name
Both query gives the exact same numbers of data.
I don't have DBA permission. How do I copy all data without violating unique key of the table.
Thanks in advance!
You can use UPSERT for decisional INSERT or UPDATE or you may write small procedure for this.
you may consider to use NOT EXISTS, but your data is big and it might not be resource efficient.
insert into prod_tab
select * from other_tab t1 where NOT exists (
select 1 from prod_tab t2 where t1.id = t2.id
);
In Oracle you can use a MERGE query for that.
The following query proceeds as follows for each data row :
if the source record_id does not yet exist in the target table, a new record is inserted
else, the existing record is updated with source values
For the sake of the example, I assumed that there are two other columns in the table : column1 and column2.
MERGE INTO target_table t1
USING (SELECT * from source_table t2)
ON (t1.record_id = t2.record_id)
WHEN MATCHED THEN UPDATE SET
t1.column1 = t2.column1,
t1.column2 = t2.column2
WHEN NOT MATCHED THEN INSERT
(record_id, column1, column2) VALUES (t2.record_id, t2.column1, t2.column2)
I have an ORC Table. I populate it using the data from some other table as follows:
INSERT INTO TABLE orc_table_name SELECT * FROM other_table_name
Is there any way I can prevent inserting of duplicate entries into the ORC Table?
you can use not in command See a general code below: it inserts records to the orc_table_name based on the fact that value1 from TABLE_1 was not inserted before.
INSERT INTO orc_table_name
(Value1, Value2)
SELECT t1.Value1,
t1.Value2
FROM TABLE_1 t1
WHERE t1.Value1 NOT IN (SELECT Value1 FROM orc_table_name)
INSERT INTO orc_table_name(field1,field2....fieldn)
select field1,field2... field(n-1),MIN(fieldn) as fieldn
from other_table_name
Group By field1,field2...field(n-1)
I have a hive table dynpart.
id int
name char(30)
city char(30)
thisday string
# Partition Information
# col_name data_type comment
thisday string
It is partitioned by 'thisday' whose datatype is STRING.
How can I insert a single record into the table in a particular partition. I know there is load command to load an entire file data into hive table. I just want to know how an Insert statement can be written for a partitioned table. I tried to write command like below but this is taking data from another table.
insert into droplater partition(thisday='30/03/2017') select * from dynpart;
The table: Droplater has the same structure as dynpart. But the above command is to insert the data from another table. What I'd like to learn is to write a simple insert command into a partition, like: insert into tabname values(1,"abcd","efgh");into the table.
This will work for primitive types only (no arrays, structs etc.)
insert into tabname partition (thisday='30/03/2017') values (1,"abcd","efgh");
This will work for all types
insert into tabname partition (thisday='30/03/2017') select 1,"abcd","efgh";
P.s.
By all means, partition your table by date ((thisday date) )
insert into tabname partition (thisday=date '2017-03-30') ...
or at least use the ISO date format
insert into tabname partition (thisday='2017-03-30') ...
I have 3 tables, TBL_A, TBL_B and TBL_C.
TBL_A has an ID (lets call it ID_A) thats PK of A, and FK of B and C. ID_A have a trigger with an autoincrement before insert.
The problem:
I need to make an Insert All that looks kinda like this
INSERT ALL
INTO TBL_A(FIELD1, FIELD2) VALUES('VALUE1', 'VALUE2')--like i said before, the trigger autoinsert the id with the NEXTVAL in MY_SEQ sequence.
INTO TBL_B(ID_A, FIELD) VALUES (MY_SEQ.CURRVAL, 'VALUE')
INTO TBL_C(ID_A, FIELD) VALUE (MY_SEQ.CURRVAL, 'VALUE')
SELECT * FROM DUAL
But, for some reason it says that sequence MY_SEQ.CURRVAL is not yet defined in this session . What can i do to solve it? Cant find a way to do it.
I need to keep the trigger because that insert can be huge.
PLZ help me and srry about my english btw ^^
This have worked fine for me:
INSERT ALL INTO TBL_A(FIELD1, FIELD2) VALUES('VALUE1', 'VALUE2')
INTO TBL_B(ID_A, FIELD) VALUES (MY_SEQ.CURRVAL, 'VALUE')
INTO TBL_C(ID_A, FIELD) VALUES (MY_SEQ.CURRVAL, 'VALUE')
SELECT * FROM DUAL;
Indeed this really works when I call at least once MY_SEQ.NEXTVAL. In all other runs this is working fine without call NEXTVAL. I think the sequence must be properly initialized
INSERT ALL
INTO TBL_A(ID, FIELD1, FIELD2) VALUES(id, val1, val2)
INTO TBL_B(ID_A, FIELD) VALUES(id, val)
INTO TBL_C(ID_A, FIELD) VALUES(id, val)
SELECT
MY_SEQ.NEXTVAL as id, 'VALUE' as val, 'VALUE1' val1, 'VALUE2' val2
FROM DUAL
You can't use a sequence in the subquery of a multi-table insert and you shouldn't use it in the values clause like that because it can give unpredictable results.
You might consider populating the data into a global temporary table with the sequence and then doing your multitable insert using the global temporary table. Of course this would require modifying the trigger.