This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
SQL Server: Is it possible to insert into two tables at the same time?
Select from one table and insert to another two table
I have two tables with one-to-one relationship
according to the result of a select statement I need to insert the resultant values in those tables
I really need to avoid cursors, do you know how I could insert in both tables with one select statement , or do you know some other solution that is better in performance, the table I'm querying is expected to get really huge and looping through it would be bad
thanks in advance
Related
This question already has answers here:
Exclude a column using SELECT * [except columnA] FROM tableA?
(47 answers)
Closed 5 years ago.
I was reading SQL queries and got stuck with a doubt. Is there any SQL query to exclude a single column while using select statement?
For example, I have a table which contains data about student (s_name, address, email_id, ..... many more).
I want to select all the columns except email_id.
I don't think there is a way to do it directly in a Select statement. But you can use this alternative:
How to select all the columns of a table except one column?
This question already has answers here:
Insert, on duplicate update in PostgreSQL?
(18 answers)
How to UPSERT (MERGE, INSERT ... ON DUPLICATE UPDATE) in PostgreSQL?
(7 answers)
Closed 9 years ago.
i'm manually updating table in my DB using import functionality in PostgreSQL. Working with large number of data i came across an issue of duplicating primary keys. I am looking for a script to upload only values that do not violate primary key assumption, and those that to violate are to be ignored (not uploaded or updated).
I have already seen a code that would kind of do what i need however not quite sure if it will work for me.
Columns i am working with are:
acc_ph_id (primary_key);acc_ph_no;ph_type;ph_flag
Any suggestions will be highly appreciated as i am rather new to Postgresql in general.
Upload the table into a staging table with no constraints.
Then load the table into the full table eliminating duplicates:
insert into real_table(acc_ph_id, acc_ph_no, ph_type, ph_flag)
select distinct on (acc_ph_id) acc_ph_id, acc_ph_no, ph_type, ph_flag
from staging_table
order by acc_ph_id;
EDIT:
Oh, if the problem is the keys that already exist, then do:
insert into real_table(acc_ph_id, acc_ph_no, ph_type, ph_flag)
select distinct on (acc_ph_id) acc_ph_id, acc_ph_no, ph_type, ph_flag
from staging_table st
where not exists (select 1
from real_table rt
where rt.acc_ph_id = st.acc_ph_id
)
order by acc_ph_id;
This question already has answers here:
How to put more than 1000 values into an Oracle IN clause [duplicate]
(11 answers)
Closed 9 years ago.
I have a file SRQ which is having 10000 SRQ_ID which are unique.
I have one table(TABLE1) which is having 2 columns namely SRQ_ID,WORK_ID .
I needs to write a query which will search the table(TABLE1) for all the SRQ_ID's in the file SRQ and will display the output with corresponding WORK_ID.
I tried the below code. But IN clause is only applicable for 1000 records. How to run the same if I have 100k records?
select WO_ID
from TABLE1
where SRQ_ID in ('B6512DF0','5838FABC','EC5D804C','074DD65C')
Is there a reason you can't just do a join between the tables using SRQ_ID?
select wo_id from table1 join srq using srq_id
This will give you the work id for all rows that have a srq_id value in the srq table.
If the file s on the database server then you could access it as an external table, and join the two. Otherwise, I'd suggest bulk loading the codes into a global temporary table and performing the join against that.
In case you don't like to create a temporary table, you can use a nested table:
CREATE OR REPLACE TYPE VARCHAR_TABLE_TYPE AS TABLE OF VARCHAR2(20);
select WO_ID
from TABLE1
where SRQ_ID MEMBER OF VARCHAR_TABLE_TYPE('B6512DF0','5838FABC','EC5D804C','074DD65C');
But I don't know the limit for initializing a nested table. Oracle documentation says: "Because a nested table does not have a declared size, you can put as many elements in the constructor as necessary."
This question already has answers here:
Is there a "Default Order By Column" in SQL Server?
(2 answers)
How to SELECT the last 10 rows of an SQL table which has no ID field?
(13 answers)
Closed 9 years ago.
I need to write a SQL query that returns records in the order in which they were entered in to a table.
I can't add a column to the table or change the table in any way. I can insert into and select from the table.
Say I execute three insert queries like
insert into x values(a,1);
insert into x values(d,4);
insert into x values(u,42);
when I select from the table x I need to get the records in this order.
a 1
d 4
u 42
The table has only two columns, both have nothing to do with date.
You can't do it without changing the table in some way.
When you select data from a table the order in which it is returned is non-deterministic on all the sql database engines I know and certainly in MSSQL server 2000+. To get the rows in a defined order you must include an ORDER BY clause and there is nothing you can specify to give the desired order.
Since you cannot change the schema, then this is game over.
Okay, (almost) nothing is impossible. You could periodically analyse the physical database file for changes and decode those into the information you require but, this would likely fail when multiple rows were inserted by one transaction.
I doubt you have the access or the inclination to do that.
You can't. The order returned is not deterministic without an order by clause, and you don't have any thing to order by
This question already has answers here:
Possible to perform cross-database queries with PostgreSQL?
(9 answers)
PostgreSQL cross server query? [closed]
(2 answers)
Closed 9 years ago.
I've one problem regarding manage information from two different Postgres servers.
In the one server I've one table a containing id bigserial and phone varchar(200)
On the other server I've table b that contains id bigserial and tell varchar(200)
Is there a way that I can compare witch rows from table a are present in the rows of table b, comparing by phone = tell?
You can use dblink to fetch data from remote server. Once you have a cursor open you can treat data from dblink_fetch as data returned from local table and just outer join it with the local table. If there are many rows in the remote table you might want to use pl/pgSQL to fetch it in parts.