Mechanism of a insert into select
Hi
I am using a insert with select in a batch program which is supposed to run just a little before midnight.
My question is, when a insert into a table occurs, does oracle select all the rows before the insert starts, OR, does the insert occur simultaneously with the select.
eg. table B has 4 rows
data is being inserted into A from B
does oracle select row1 from B and insert row1 into table A and then move to row2 in B and insert row2 into A
OR
does oracle select rows1 thro 4 in B before the first insert into A starts?
If anybody could point me to a document/reference about that would be aweosme.
--vj
There is no need to know this kind of internals. A statement is atomic, so it either completes in total or not at all. Other sessions cannot see intermediate results, even your own session is not able to. If you would try, for example by using database triggers, you'll get a mutating table error to prevent that.
Regards,
Rob.
Similar Messages
-
Commit for every 1000 records in Insert into select statment
Hi I've the following INSERT into SELECT statement .
The SELECT statement (which has joins ) has around 6 crores fo data . I need to insert that data into another table.
Please suggest me the best way to do that .
I'm using the INSERT into SELECT statement , but i want to use commit statement for every 1000 records .
How can i achieve this ..
insert into emp_dept_master
select e.ename ,d.dname ,e.empno ,e.empno ,e.sal
from emp e , dept d
where e.deptno = d.deptno ------ how to use commit for every 1000 records .ThanksSmile wrote:
Hi I've the following INSERT into SELECT statement .
The SELECT statement (which has joins ) has around 6 crores fo data . I need to insert that data into another table.Does the another table already have records or its empty?
If its empty then you can drop it and create it as
create your_another_table
as
<your select statement that return 60000000 records>
Please suggest me the best way to do that .
I'm using the INSERT into SELECT statement , but i want to use commit statement for every 1000 records .That is not the best way. Frequent commit may lead to ORA-1555 error
[url http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:275215756923]A nice artical from ASKTOM on this one
How can i achieve this ..
insert into emp_dept_master
select e.ename ,d.dname ,e.empno ,e.empno ,e.sal
from emp e , dept d
where e.deptno = d.deptno ------ how to use commit for every 1000 records .
It depends on the reason behind you wanting to split your transaction into small chunks. Most of the time there is no good reason for that.
If you are tying to imporve performance by doing so then you are wrong it will only degrade the performance.
To improve the performance you can use APPEND hint in insert, you can try PARALLEL DML and If you are in 11g and above you can use [url http://docs.oracle.com/cd/E11882_01/appdev.112/e25788/d_parallel_ex.htm#CHDIJACH]DBMS_PARALLEL_EXECUTE to break your insert into chunks and run it in parallel.
So if you can tell the actual objective we could offer some help. -
Urgent : Performance Issue DELETE , INSERT INTO SELECT, UPDATE
Hi,
NEED ASSISTANCE TO OPTIMIZE THE INSERT STATEMENT (insert into select):
=================================================
We have a report.
As per current design following steps are used to populate the custom table whcih is used for reporting purpose:
1) DELETE all the recods from the custom table XXX_TEMP_REP.
2) INSERT records in custom table XXX_TEMP_REP (Assume all the records related to type A)
using
INSERT..... INTO..... SELECT.....
statement.
3) Update records in XXX_TEMP_REP
using some custom logic for the records populated .
4) INSERT records in custom table XXX_TEMP_REP (Records related to type B)
using
INSERT..... INTO..... SELECT.....
statement.
Stats gathered related to Insert statement are:
Event Wait Information
SID 460 is waiting on event : db file sequential read
P1 Text : file#
P1 Value : 20
P2 Text : block#
P2 Value : 435039
P3 Text : blocks
P3 Value : 1
Session Statistics
redo size : 293.84 M
parse count (hard) : 34
parse count (total) : 1217
user commits : 3
Transaction and Rollback Information
Rollback Used : 35.1796875 M
Rollback Records : 355886
Rollback Segment Number : 12
Rollback Segment Name : _SYSSMU12$
Logical IOs : 1627182
Physical IOs : 136409
RBS Startng Extent ID : 14
Transaction Start Time : 09/29/10 04:22:11
Transaction_Status : ACTIVE
Please suggest how this can be optimized.
Regards,
NarenderHello,
Is there any relation with the Oracle Forms tool ?
Francois -
How to use "insert into & select" in format search
Hello,
I am just wonderring whether you can help me solve this issue.
I want to change the value of a field in the title area for a sales quotation. however, this field is not shown up on the interface.
For example, there is a field in the title area, "OQUT.Ref1". You actually cannot see this field from the quotation interface or any other doc. Now I want to update the value of this field.
What I am now trying to do is to create an field named "update" in the title area, use format search to update. the code for the command will be something like
Insert into $[OQUT.Ref1.0]
select $[OQUT.U_MFG#]
here, U_MFG# is UDF. as you may understand, I want to copy the value in the U_MFG# to the field of "Ref1".
However, when I run "Execute", it gives me error. I believe there is something wrong with the code of "Insert into $[OQUT.Ref1.0]
Does anyone know how to write the code?
many thanks
StanleyThanks both Suda and sagar. The reason I wanted to do this is because I wanted to have UDF info be shown on the MS word-templated quotation document.
As you know, when you click the word symbol, a word-templated doc will be generated. The client needs two completely different format of quotation printout. thus I plan to print one type from PLD and other type by clicking the Word symbol. but later, I found out that the UDF field cannot be selected on the MS word template, or only system fields.
Thus, the only way I can do is to copy the value from udf to some unused sytem fields and then show that system fields on the MS word template.
any idea do you have?
I wanted to tell SAP that It is not useful if the udf fields cannot be inserted into word template.
thanks
Stanley -
Procedure or function for insert into select ...
Hi!
We need to know if we can create a procedure or function that will run the following script:
insert into agent.train_schedule
select 4000, '06:25' ,trunc(sysdate, 'year')+t.n
from agent.trains, (select rownum -1 n from dual
connect by level <= 365) t
where agent.trains.id = 4000 and
agent.trains.weekday like '%'||to_char(trunc(sysdate, 'year')+t.n, 'd',
'nls_date_language=AMERICAN')||'%';
The script inserts the train schedule dates into the train schedule table in accordance to the trains table.
Any help would be appreciated.
Thanks!Try the below:
Create Or Replace procedure test_proc(p_train_no number, p_train_time varchar2) as
begin
insert into agent.train_schedule
select p_train_no, p_train_time ,trunc(sysdate, 'year')+t.n
from agent.trains, (select rownum -1 n from dual
connect by level <= 365) t
where agent.trains.id = p_train_no and
agent.trains.weekday like '%'||to_char(trunc(sysdate, 'year')+t.n, 'd',
'nls_date_language=AMERICAN')||'%';
end;
Call the procedure from the gui also pass the train number and the train time.
Regards,
Samujjwal Basu -
How to insert the select query result into table?
How to insert the select query result into table?
SELECT top 20 creation_time
,last_execution_time
,total_physical_reads
,total_logical_reads
,total_logical_writes
, execution_count
, total_worker_time
, total_elapsed_time
, total_elapsed_time / execution_count avg_elapsed_time
,SUBSTRING(st.text, (qs.statement_start_offset/2) + 1,
((CASE statement_end_offset
WHEN -1 THEN DATALENGTH(st.text)
ELSE qs.statement_end_offset END
- qs.statement_start_offset)/2) + 1) AS statement_text
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st
ORDER BY total_elapsed_time / execution_count DESC;
Thanks,
Tirumala1. SELECT INTO
Below method will create table when data is inserted from one table to another table. Its useful when you need exactly same datatype as source table.
Use AdventureWorks2008R2;
Go
---Insert data using SELECT INTO
SELECT AddressLine1, City
INTO BothellAddresses
FROM Person.Address
where City = 'Bothell';
GO
---VERIFY DATA
Select AddressLine1, City
FROM BothellAddresses
---DROP TABLE
DROP TABLE BothellAddresses
GO
2. INSERT INTO SELECT
Below method will need table to be created prior to inserting data. Its really useful when table is already created and you want insert data from
another table.
Use AdventureWorks2008R2;
Go
---Create Table
CREATE TABLE BothellAddresses (AddressLine1 NVARCHAR(60), City NVARCHAR(30))
---Insert into above table using SELECT
INSERT INTO BothellAddresses(AddressLine1, City)
SELECT AddressLine1, City
FROM Person.Address
where City = 'Bothell';
---VERIFY DATA
Select AddressLine1, City
FROM BothellAddresses
---DROP TABLE
DROP TABLE BothellAddresses
GO
Regards,
Vishal Patel
Blog: http://vspatel.co.uk
Site: http://lehrity.com -
Insert into multiple tables; grab id from first
I apologize if this has been answered elsewhere, but I tried
searching and got no results.
I'm trying to insert the contents of one form into two
tables. After inserting some fields into one table, I want to grab
the key/id from the first table into the second table with the
other fields.
I've tried a few different things, including trying to use
SCOPE_IDENTITY(), but just kept getting lots of errors.
The current version of my code works perfectly, *except for
the fact that it doesn't insert the ID*!
Can anyone tell me what I'm doing wrong? or offer another way
of doing this?
Thanks for your help, as usual.MarianneStone wrote:
> The current version of my code works perfectly, *except
for the fact that it doesn't insert > the ID*!
> Can anyone tell me what I'm doing wrong?
The EmployeeID is not inserted because the getMyID query
cannot find the newly inserted record. When using a UUID to
identify the new records ..
<cfquery name="qAddDeptHead" datasource="#db#"
result="result">
INSERT INTO Employee(... , EmpUUID)
VALUES ( ... , '#CreateUUID()#')
</cfquery>
You have to use that _same_ UUID to find the record, _not_
create a new one.
<cfquery name="getMyID" datasource="#db#">
SELECT EmployeeID, EmpUUID
FROM Employee
WHERE Employee.EmpUUID='#UseTheSameUUIDFromTheFirstQuery#'
</cfquery>
As an aside, you could probably do this in two queries not
three. Just drop the second SELECT query and use an INSERT
INTO/SELECT .. FROM Table statement instead. Plus you should also
consider using cfqueryparam for your query parameters. It has many
benefits.
http://livedocs.adobe.com/coldfusion/8/htmldocs/Tags_p-q_18.html#1102474
> Can anyone ... offer another way of doing this?
There are a few options. For ColdFusion 8, you could use the
result attribute. After running the INSERT you can grab the new
EmployeeID by using #yourResult.IDENTITYCOL#
<cfquery name="qAddDeptHead" datasource="#db#"
result="yourResult">
INSERT INTO Employee ( Columns here )
VALUES (... values here ...)
</cfquery>
For earlier versions, you can either use SCOPE_IDENTITY() or
a UUID value - as you are currently doing now.
> I've tried a few different things, including trying to
use SCOPE_IDENTITY(),
> but just kept getting lots of errors.
If you use SCOPE_IDENTITY() turn off the rowcount, or the
query variable "qAddDeptHead" may be undefined when you try and use
it.
<cfquery name="qAddDeptHead" datasource="#db#"
result="yourResult">
SET NOCOUNT ON;
INSERT INTO Employee ( Columns here )
VALUES (... values here ...)
SELECT EmployeeID AS NewEmployeeID
SET NOCOUNT OFF;
</cfquery>
<cfquery name="qNewEmpPosition" datasource="#db#">
INSERT INTO EmployeePositions(EmployeeID, .... )
VALUES ( #qAddDeptHead.NewEmployeeID#, ....)
</cfquery> -
Subquery in insert into clause
what is the use of using subquery in insert into clause.
e.g. insert into (select empno from emp)
values(1234)
regards
kiranNot really of common use, I think, but WITH CHECK OPTION option could be one reason for using that. Let's see an example :
SQL> INSERT INTO
2 (SELECT empno, ename, deptno FROM emp WHERE deptno < 20)
3* VALUES (1111, 'Brown', 30)
SQL> /
1 row created.That's the same as
SQL> insert into emp (empno, ename, deptno)
2 values(1111, 'Brown', 30);But
SQL> INSERT INTO
2 (SELECT empno, ename, deptno FROM emp WHERE deptno < 20 WITH CHECK OPTION)
3* VALUES (2222, 'Green', 30)
SQL> /
(SELECT empno, ename, deptno FROM emp WHERE deptno < 20
ERROR at line 2:
ORA-01402: view WITH CHECK OPTION where-clause violation
SQL>Paul -
Long time on buffer sort with a insert and select through a dblink
I am doing a fairly simple "insert into select from" statement through a dblink, but something is going very wrong on the other side of the link. I am getting a huge buffer sort time in the explain plan (line 9) and I'm not sure why. When I try to run sql tuning on it from the other side of the dblink, I get an ora-600 error "ORA-24327: need explicit attach before authenticating a user".
Here is the original sql:
INSERT INTO PACE_IR_MOISTURE@PRODDMT00 (SCHEDULE_SEQ, LAB_SAMPLE_ID, HSN, SAMPLE_TYPE, MATRIX, SYSTEM_ID)
SELECT DISTINCT S.SCHEDULE_SEQ, PI.LAB_SAMPLE_ID, PI.HSN, SAM.SAMPLE_TYPE, SAM.MATRIX, :B1 FROM SCHEDULES S
JOIN PERMANENT_IDS PI ON PI.HSN = S.SCHEDULE_ID
JOIN SAMPLES SAM ON PI.HSN = SAM.HSN
JOIN PROJECT_SAMPLES PS ON PS.HSN = SAM.HSN
JOIN PROJECTS P ON PS.PROJECT_SEQ = PS.PROJECT_SEQ
WHERE S.PROC_CODE = 'DRY WEIGHT' AND S.ACTIVE_FLAG = 'C' AND S.COND_CODE = 'CH' AND P.WIP_STATUS IN ('WP','HO')
AND SAM.WIP_STATUS = 'WP';
Here is the sql as it appears on proddmt00:
INSERT INTO "PACE_IR_MOISTURE" ("SCHEDULE_SEQ","LAB_SAMPLE_ID","HSN","SAMPLE_TYPE","MATRIX","SYSTEM_ID")
SELECT DISTINCT "A6"."SCHEDULE_SEQ","A5"."LAB_SAMPLE_ID","A5"."HSN","A4"."SAMPLE_TYPE","A4"."MATRIX",:B1
FROM "SCHEDULES"@! "A6","PERMANENT_IDS"@! "A5","SAMPLES"@! "A4","PROJECT_SAMPLES"@! "A3","PROJECTS"@! "A2"
WHERE "A6"."PROC_CODE"='DRY WEIGHT' AND "A6"."ACTIVE_FLAG"='C' AND "A6"."COND_CODE"='CH' AND ("A2"."WIP_STATUS"='WP' OR "A2"."WIP_STATUS"='HO') AND "A4"."WIP_STATUS"='WP' AND "A3"."PROJECT_SEQ"="A3"."PROJECT_SEQ" AND "A3"."HSN"="A4"."HSN" AND "A5"."HSN"="A4"."HSN" AND "A5"."HSN"="A6"."SCHEDULE_ID";
Here is the explain plan on proddmt00:
PLAN_TABLE_OUTPUT
SQL_ID cvgpfkhdhn835, child number 0
INSERT INTO "PACE_IR_MOISTURE" ("SCHEDULE_SEQ","LAB_SAMPLE_ID","HSN","SAMPLE_TYPE","MATRIX","SYSTEM_ID")
SELECT DISTINCT "A6"."SCHEDULE_SEQ","A5"."LAB_SAMPLE_ID","A5"."HSN","A4"."SAMPLE_TYPE","A4"."MATRIX",:B1
FROM "SCHEDULES"@! "A6","PERMANENT_IDS"@! "A5","SAMPLES"@! "A4","PROJECT_SAMPLES"@! "A3","PROJECTS"@! "A2"
WHERE "A6"."PROC_CODE"='DRY WEIGHT' AND "A6"."ACTIVE_FLAG"='C' AND "A6"."COND_CODE"='CH' AND
("A2"."WIP_STATUS"='WP' OR "A2"."WIP_STATUS"='HO') AND "A4"."WIP_STATUS"='WP' AND
"A3"."PROJECT_SEQ"="A3"."PROJECT_SEQ" AND "A3"."HSN"="A4"."HSN" AND "A5"."HSN"="A4"."HSN" AND
"A5"."HSN"="A6"."SCHEDULE_ID"
Plan hash value: 3310593411
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time | Inst |IN-OUT|
| 0 | INSERT STATEMENT | | | | | 5426M(100)| | | |
| 1 | HASH UNIQUE | | 1210K| 118M| 262M| 5426M (3)|999:59:59 | | |
|* 2 | HASH JOIN | | 763G| 54T| 8152K| 4300M (1)|999:59:59 | | |
| 3 | REMOTE | | 231K| 5429K| | 3389 (2)| 00:00:41 | ! | R->S |
| 4 | MERGE JOIN CARTESIAN | | 1254G| 61T| | 1361M (74)|999:59:59 | | |
| 5 | MERGE JOIN CARTESIAN| | 3297K| 128M| | 22869 (5)| 00:04:35 | | |
| 6 | REMOTE | SCHEDULES | 79 | 3002 | | 75 (0)| 00:00:01 | ! | R->S |
| 7 | BUFFER SORT | | 41830 | 122K| | 22794 (5)| 00:04:34 | | |
| 8 | REMOTE | PROJECTS | 41830 | 122K| | 281 (2)| 00:00:04 | ! | R->S |
| 9 | BUFFER SORT | | 380K| 4828K| | 1361M (74)|999:59:59 | | |
| 10 | REMOTE | PROJECT_SAMPLES | 380K| 4828K| | 111 (0)| 00:00:02 | ! | R->S |
Predicate Information (identified by operation id):
2 - access("A3"."HSN"="A4"."HSN" AND "A5"."HSN"="A6"."SCHEDULE_ID")Please use code tags... your formatted message is below:
From the looks of your explain plan... these entries :
Id Operation Name Rows Bytes TempSpc Cost (%CPU) Time Inst IN-OUT
4 MERGE JOIN CARTESIAN 1254G 61T 1361M (74) 999:59:59
5 MERGE JOIN CARTESIAN 3297K 128M 22869 (5) 00:04:35 Are causing extensive cpu processing, probably due to the cartesian join (includes sorting)... does "61T" mean 61 terabytes? Holy hell
From the looks of the explain plan these tables don't look partitioned.... can you confirm?
Why are you selecting distinct? If this is for ETL or data warehouse related procedure it ain't a good idea to use distinct... well ever... it's horrible for performance.
INSERT INTO PACE_IR_MOISTURE@PRODDMT00 (SCHEDULE_SEQ, LAB_SAMPLE_ID, HSN, SAMPLE_TYPE, MATRIX, SYSTEM_ID)
SELECT DISTINCT S.SCHEDULE_SEQ, PI.LAB_SAMPLE_ID, PI.HSN, SAM.SAMPLE_TYPE, SAM.MATRIX, :B1 FROM SCHEDULES S
JOIN PERMANENT_IDS PI ON PI.HSN = S.SCHEDULE_ID
JOIN SAMPLES SAM ON PI.HSN = SAM.HSN
JOIN PROJECT_SAMPLES PS ON PS.HSN = SAM.HSN
JOIN PROJECTS P ON PS.PROJECT_SEQ = PS.PROJECT_SEQ
WHERE S.PROC_CODE = 'DRY WEIGHT' AND S.ACTIVE_FLAG = 'C' AND S.COND_CODE = 'CH' AND P.WIP_STATUS IN ('WP','HO')
AND SAM.WIP_STATUS = 'WP';
Here is the sql as it appears on proddmt00:
INSERT INTO "PACE_IR_MOISTURE" ("SCHEDULE_SEQ","LAB_SAMPLE_ID","HSN","SAMPLE_TYPE","MATRIX","SYSTEM_ID")
SELECT DISTINCT "A6"."SCHEDULE_SEQ","A5"."LAB_SAMPLE_ID","A5"."HSN","A4"."SAMPLE_TYPE","A4"."MATRIX",:B1
FROM "SCHEDULES"@! "A6","PERMANENT_IDS"@! "A5","SAMPLES"@! "A4","PROJECT_SAMPLES"@! "A3","PROJECTS"@! "A2"
WHERE "A6"."PROC_CODE"='DRY WEIGHT' AND "A6"."ACTIVE_FLAG"='C' AND "A6"."COND_CODE"='CH' AND ("A2"."WIP_STATUS"='WP' OR "A2"."WIP_STATUS"='HO') AND "A4"."WIP_STATUS"='WP' AND "A3"."PROJECT_SEQ"="A3"."PROJECT_SEQ" AND "A3"."HSN"="A4"."HSN" AND "A5"."HSN"="A4"."HSN" AND "A5"."HSN"="A6"."SCHEDULE_ID";
Here is the explain plan on proddmt00:
PLAN_TABLE_OUTPUT
SQL_ID cvgpfkhdhn835, child number 0
INSERT INTO "PACE_IR_MOISTURE" ("SCHEDULE_SEQ","LAB_SAMPLE_ID","HSN","SAMPLE_TYPE","MATRIX","SYSTEM_ID")
SELECT DISTINCT "A6"."SCHEDULE_SEQ","A5"."LAB_SAMPLE_ID","A5"."HSN","A4"."SAMPLE_TYPE","A4"."MATRIX",:B1
FROM "SCHEDULES"@! "A6","PERMANENT_IDS"@! "A5","SAMPLES"@! "A4","PROJECT_SAMPLES"@! "A3","PROJECTS"@! "A2"
WHERE "A6"."PROC_CODE"='DRY WEIGHT' AND "A6"."ACTIVE_FLAG"='C' AND "A6"."COND_CODE"='CH' AND
("A2"."WIP_STATUS"='WP' OR "A2"."WIP_STATUS"='HO') AND "A4"."WIP_STATUS"='WP' AND
"A3"."PROJECT_SEQ"="A3"."PROJECT_SEQ" AND "A3"."HSN"="A4"."HSN" AND "A5"."HSN"="A4"."HSN" AND
"A5"."HSN"="A6"."SCHEDULE_ID"
Plan hash value: 3310593411
Id Operation Name Rows Bytes TempSpc Cost (%CPU) Time Inst IN-OUT
0 INSERT STATEMENT 5426M(100)
1 HASH UNIQUE 1210K 118M 262M 5426M (3) 999:59:59
* 2 HASH JOIN 763G 54T 8152K 4300M (1) 999:59:59
3 REMOTE 231K 5429K 3389 (2) 00:00:41 ! R->S
4 MERGE JOIN CARTESIAN 1254G 61T 1361M (74) 999:59:59
5 MERGE JOIN CARTESIAN 3297K 128M 22869 (5) 00:04:35
6 REMOTE SCHEDULES 79 3002 75 (0) 00:00:01 ! R->S
7 BUFFER SORT 41830 122K 22794 (5) 00:04:34
8 REMOTE PROJECTS 41830 122K 281 (2) 00:00:04 ! R->S
9 BUFFER SORT 380K 4828K 1361M (74) 999:59:59
10 REMOTE PROJECT_SAMPLES 380K 4828K 111 (0) 00:00:02 ! R->S
Predicate Information (identified by operation id):
2 - access("A3"."HSN"="A4"."HSN" AND "A5"."HSN"="A6"."SCHEDULE_ID")Edited by: TheDudeNJ on Oct 13, 2009 1:11 PM -
Strange result from insert into...select query
Hello guys,
I need your preciuos help for a question maybe simple, but that I can't explain by myself!
I have a query of "insert into...select" that, as I have explained in the title, returns a strange result. In facts, If I execute ONLY the SELECT statement the query returns the expected result (so 2 rows); instead If I execute the entire statement, that is the "insert into...select", the query returns 0 rows inserted!!
Following an example of the query:
INSERT
INTO TITOLI_ORI
COD_TITOLO_RICCONS ,
D_ESTRAZIONE ,
COD_SOCIETA ,
COD_PIANO_CONTABILE ,
COD_CONTO_CONTABILE ,
COD_RUBRICATO_STATISTICO_1 ,
COD_NDG ,
NUM_ESEGUITO ,
CUR_IMPORTO_RICCONS ,
CUR_IMPORTO_BICO ,
FLG_MODIFICATO ,
CUR_NON_ASSEGNATO ,
FLG_QUOTATO ,
COD_CATEG ,
TIP_COPERTURA ,
TIPTAS_TITOLO
SELECT NEWID,
'28-feb-2111',
COD_SOCIETA,
COD_PIANO_CONTABILE,
COD_CONTO_CONTABILE,
COD_RUBRICATO_STATISTICO_1,
COD_NDG,
NUM_ESEGUITO,
CUR_VAL_IMPEGNI,
'ABC' as CUR_IMPORTO_BICO,
0 as FLG_MODIFICATO,
NULL as CUR_NON_ASSEGNATO,
FLG_QUOTATO,
COD_CATEG,
TIP_COPERTURA,
TIP_TASSO
FROM
(SELECT S.COD_SOC AS COD_SOCIETA,
S.TIP_PIANO_CNTB AS COD_PIANO_CONTABILE,
S.COD_CONTO_CNTB AS COD_CONTO_CONTABILE,
S.COD_RUBR_STAT AS COD_RUBRICATO_STATISTICO_1,
TRC.COD_RAGGR_IAS AS COD_RAGGRUPPAMENTO_IAS,
TRC.COD_NDG AS COD_NDG,
TRC.COD_ESEG AS NUM_ESEGUITO,
CAST((TRC.IMP_PLUS_MINUS_VAL/TRC.IMP_CAMB) AS FLOAT) AS CUR_VAL_IMPEGNI,
TRC.TIP_QUOTAZ AS FLG_QUOTATO,
TRC.COD_CAT_TIT AS COD_CATEG,
TIP_COP AS TIP_COPERTURA,
T.TIP_TASSO AS TIP_TASSO
FROM S_SLD_CNTB S
INNER JOIN
(SELECT DISTINCT COD_SOC,
TIP_PIANO_CNTB,
COD_CONTO_CNTB,
COD_RUBR_STAT ,
COD_INTER_TIT AS COD_INTER
FROM S_COLLEG_CONTO_CNTB_TIT
WHERE COD_SOC = 'ME'
) CCC
ON S.COD_SOC = CCC.COD_SOC
AND S.TIP_PIANO_CNTB = CCC.TIP_PIANO_CNTB
AND S.COD_CONTO_CNTB = CCC.COD_CONTO_CNTB
AND S.COD_RUBR_STAT = CCC.COD_RUBR_STAT
INNER JOIN S_TIT_RICCONS TRC
ON CCC.COD_INTER = TRC.COD_INTER_TIT
AND CCC.COD_SOC = TRC.COD_SOC
AND TRC.COD_RAGGR_IAS = RTRIM('VALUE1 ')
AND TRC.COD_RAGGR_IAS NOT IN ('VALUE2')
AND TRC.DES_TIP_SLD_TIT_RICCONS IN ('VALUE3')
AND TRC.DES_MOV_TIT = RTRIM('VALUE4 ')
AND TRC.COD_CAT_TIT = RTRIM('VALUE4 ')
AND TRC.COD_INTER_TIT = RTRIM('VALUE5')
AND '28-feb-2011' = TRC.DAT_RIF
LEFT JOIN S_TIT T
ON T.COD_INTER_TIT = TRC.COD_INTER_TIT
AND T.COD_SOC = TRC.COD_SOC
AND '28-feb-2011' = T.DAT_RIF
INNER JOIN S_ANAG_SOGG AG
ON TRC.COD_NDG = AG.COD_NDG
AND AG.COD_SOC = TRC.COD_SOC
AND '28-feb-2011' = AG.DAT_RIF
WHERE S.DAT_RIF = '28-feb-2011'
AND (S.FLG_ANULL_BICO = 0
OR S.FLG_ANULL_BICO IS NULL)
AND S.COD_SOC = 'V6'
AND LENGTH(RTRIM(S.COD_CONTO_CNTB)) = 10
AND S.TIP_PIANO_CNTB = 'V7'
AND TRC.IMP_PLUS_MINUS_VAL < 0
AND SUBSTR(S.COD_CONTO_CNTB,1,7) IN (RTRIM('VALUE8 '))
Thanks a lotRight, I have executed this steps:
- I have changed the query with the select count(*)
- Changed the insert into with the select count(*)
- Executed the insert into
These are the result:
SQL> select count(*) from TITOLI_ORI2;
COUNT(*)
1
BUT:
SQL> select * from TITOLI_ORI2;
A
0
The insert into that I've modified is this:
INSERT INTO bsc.TITOLI_ORI2
select count(*)
FROM
(SELECT bsc.NEWID,
TO_DATE('28-feb-2111','DD-MON-YYYY') as data,
COD_SOCIETA,
COD_PIANO_CONTABILE,
COD_CONTO_CONTABILE,
COD_RUBRICATO_STATISTICO_1,
COD_NDG,
NUM_ESEGUITO,
CUR_VAL_IMPEGNI,
'ABC' AS CUR_IMPORTO_BICO,
0 AS FLG_MODIFICATO,
NULL CUR_NON_ASSEGNATO,
FLG_QUOTATO,
COD_CATEG,
TIP_COPERTURA,
TIP_TASSO
FROM
(SELECT S.COD_SOC AS COD_SOCIETA,
S.TIP_PIANO_CNTB AS COD_PIANO_CONTABILE,
S.COD_CONTO_CNTB AS COD_CONTO_CONTABILE,
S.COD_RUBR_STAT AS COD_RUBRICATO_STATISTICO_1,
TRC.COD_RAGGR_IAS AS COD_RAGGRUPPAMENTO_IAS,
TRC.COD_NDG AS COD_NDG,
TRC.COD_ESEG AS NUM_ESEGUITO,
CAST((TRC.IMP_PLUS_MINUS_VAL/TRC.IMP_CAMB) AS FLOAT) AS CUR_VAL_IMPEGNI,
TRC.TIP_QUOTAZ AS FLG_QUOTATO,
TRC.COD_CAT_TIT AS COD_CATEG,
TIP_COP AS TIP_COPERTURA,
T.TIP_TASSO AS TIP_TASSO
FROM bsc.S_SLD_CNTB S
INNER JOIN
(SELECT DISTINCT COD_SOC,
TIP_PIANO_CNTB,
COD_CONTO_CNTB,
COD_RUBR_STAT ,
COD_INTER_TIT AS COD_INTER
FROM bsc.S_COLLEG_CONTO_CNTB_TIT
WHERE COD_SOC = 'ME'
) CCC
ON S.COD_SOC = CCC.COD_SOC
AND S.TIP_PIANO_CNTB = CCC.TIP_PIANO_CNTB
AND S.COD_CONTO_CNTB = CCC.COD_CONTO_CNTB
AND S.COD_RUBR_STAT = CCC.COD_RUBR_STAT
INNER JOIN bsc.S_TIT_RICCONS TRC
ON CCC.COD_INTER = TRC.COD_INTER_TIT
AND CCC.COD_SOC = TRC.COD_SOC
AND TRC.COD_RAGGR_IAS = RTRIM('HFT ')
AND TRC.COD_RAGGR_IAS NOT IN ('GPO')
AND TRC.DES_TIP_SLD_TIT_RICCONS IN ('DISPONIBILI')
AND TRC.DES_MOV_TIT = RTRIM('CONSEGNARE ')
AND TRC.COD_CAT_TIT = RTRIM('OBBLIGAZIONE ')
AND TRC.COD_INTER_TIT = RTRIM('334058')
AND '28-feb-2011' = TRC.DAT_RIF
LEFT JOIN bsc.S_TIT T
ON T.COD_INTER_TIT = TRC.COD_INTER_TIT
AND T.COD_SOC = TRC.COD_SOC
AND '28-feb-2011' = T.DAT_RIF
INNER JOIN bsc.S_ANAG_SOGG AG
ON TRC.COD_NDG = AG.COD_NDG
AND AG.COD_SOC = TRC.COD_SOC
AND '28-feb-2011' = AG.DAT_RIF
WHERE S.DAT_RIF = '28-feb-2011'
AND (S.FLG_ANULL_BICO = 0
OR S.FLG_ANULL_BICO IS NULL)
AND S.COD_SOC = 'ME'
AND LENGTH(RTRIM(S.COD_CONTO_CNTB)) = 10
AND S.TIP_PIANO_CNTB = 'IS'
AND TRC.IMP_PLUS_MINUS_VAL < 0
AND SUBSTR(S.COD_CONTO_CNTB,1,7) IN (RTRIM('P044C11 '))
Another time the strange result returns!!
And I've created the table TITOLI_ORI2 as create table TITOLI_ORI2 (a number); to contain the number result of the query. -
T-SQL - PL/SQL conversion: insert into ... select problem
Hi!
If I have an insert into ... select statement in T-SQL without a 'from' clause:
bq. insert into sometable (foo, bar) \\ select 5, case when @bar=5 then 4 else 3 end
its translation is:
bq. INSERT INTO sometable \\ ( foo, bar ) \\ VALUES ( 5, CASE \\ WHEN 5 = 5 THEN 4 \\ ELSE 3 \\ END col );
and I got: ORA-00917: "missing comma" for that. I'm not sure if it's a bug in the migration code, or is there a trick so that I can use 'CASE' in an insert into .. values statement somehow?
ZoliYou have to remove the column name. I just simplified your test case to check and it's working:
CREATE TABLE test(
id NUMBER
INSERT
INTO test(id)
VALUES(CASE WHEN 5=5 THEN 4 ELSE 3 END)
SELECT *
FROM test
;C. -
Insert into using a select and dynamic sql
Hi,
I've got hopefully easy question. I have a procedure that updates 3 tables with 3 different update statements. The procedure goes through and updates through ranges I pass in. I am hoping to create another table which will pass in those updates as an insert statement and append the data on to the existing data.
I am thinking of using dynamic sql, but I am sure there is an easy way to do it using PL/SQL as well. I have pasted the procedure below, and what I'm thinking would be a good way to do it. Below I have pasted my procedure and the bottom is the insert statement I want to use. I am faily sure I can do it using dynamic SQL, but I am not familiar with the syntax.
CREATE OR REPLACE PROCEDURE ACTIVATE_PHONE_CARDS (min_login in VARCHAR2, max_login in VARCHAR2, vperc in VARCHAR2) IS
BEGIN
UPDATE service_t SET status = 10100
WHERE poid_id0 in
(SELECT poid_id0 FROM service_t
WHERE poid_type='/service/telephony'
AND login >= min_login AND login <= max_login);
DBMS_OUTPUT.put_line( 'Service Status:' || sql%rowcount);
UPDATE account_t SET status = 10100
WHERE poid_id0 IN
(SELECT account_obj_id0 FROM service_t
WHERE poid_type = '/service/telephony'
AND login >= min_login AND login <= max_login);
DBMS_OUTPUT.put_line( 'Account Status:' || sql%rowcount);
UPDATE account_nameinfo_t SET title=Initcap(vperc)
WHERE obj_id0 IN
(SELECT account_obj_id0 FROM service_t
WHERE poid_type='/service/telephony'
AND login >=min_login AND login <= max_login);
DBMS_OUTPUT.put_line('Job Title:' || sql%rowcount);
INSERT INTO phone_card_activation values which = 'select a.status, s.status, s.login, to_char(d.sysdate,DD-MON-YYYY), ani.title
from account_t a, service_t s, account_nameinfo_t ani, dual d
where service_t.login between service_t.min_login and service_t.max_login
and ani.for_key=a.pri_key
and s.for_key=a.pri_key;'
END;
Thanks for any advice, and have a good weekend.
GeordieCorrect my if I am wrong but aren't these equal?
UPDATE service_t SET status = 10100
WHERE poid_id0 in
(SELECT poid_id0 FROM service_t
WHERE poid_type='/service/telephony'
AND login >= min_login AND login <= max_login);
(update all the records where there id is in the sub-query that meet the WHERE Clause)
AND
UPDATE service_t SET status = 10100
WHERE poid_type='/service/telephony'
AND login >= min_login AND login <= max_login);
(update all the records that meet the WHERE Clause)
This should equate to the same record set, in which case the second update would be quicker without the sub-query. -
Hi,
I should remember this but . . . bug with t-sql using Insert Into on a temp table. It is unhappy with fields I'm trying to insert.
It's probably the fact that I'm trying to using some default data w/ aliases for a couple of the fields.
Have a look at code please, and tell me how to correct. Thanks!
DECLARE @OrderNo varchar(6)
USE tempdb;
set @OrderNo = '909773' ;
IF OBJECT_ID ('#tmpNCGLITEMS') IS NOT NULL --how to get this to work with SSMS w/o reloading the qry window. i.e. how to release to delete????
DROP TABLE #tmpNCGLItems
use DEV_ORDERS;
SELECT distinct od.lline as OrdLine, od.ldrcolor as ClrCode, od.ldrspec as SpecCode, ad.Addon_Mod_GL_Acct as ncGLCode
INTO #tmpNCGLItems
FROM OODETL od
INNER JOIN [Dev_Products].[dbo].[Addon_Master] ad on od.lfg = ad.Addon_Mod_Mod_Code_ID AND ad.Addon_Mod_A_C_D_ID = 'C'
WHERE od.LOrder = @OrderNo and ad.Addon_Mod_GL_Acct is not null
select * from #tmpNCGLItems
USE tempdb;
IF OBJECT_ID ('#tmpNCGLITEMS1') IS NOT NULL
DROP TABLE #tmpNCGLItems1
USE DEV_ORDERS;
CREATE TABLE [dbo].[#tmpNCGLItems1](
[OrdLine] [char](4) NOT NULL,
[ClrCode] [char](6) NULL,
[SpecCode] [varchar](6) NULL,
[ClrUPorDiscAmt] money NULL, [SpecUPorDiscAmt] money NULL,
[ncGLCode] [varchar](10) NULL, [lPrice] money Null
) ON [PRIMARY]
--insert into below is failing. Says fields don't match----Msg 213, Level 16, State 1, Line 33
--Insert Error: Column name or number of supplied values does not match table definition.
--but. . . I think I have 7 fields and I think the datatypes match?
INSERT INTO #tmpNCGLItems1
SELECT ordLine, ClrCode, SpecCode, 0 as [ClrUPorDiscAmt], 0 as [SpecUPorDiscAmt], ncGLCode,
SUM(od.lextendprice) as lPrice
FROM #tmpNCGLItems tmp JOIN OODETL od on tmp.OrdLine = od.lline
WHERE od.lorder = @OrderNo
GROUP BY ordLine, ClrCode, SpecCode, NcGLCode>> I should remember this but . . . bug with T-SQL using Insert Into on a temp table. It is unhappy with fields [sic] I'm trying to insert. It's probably the fact that I'm trying to using some default data w/ aliases for a couple
of the fields [sic: columns are not fields]. <<
Temp tables are how a bad programmer fakes a scratch tape so his SQL can look like magnetic tape files. We do not like to materialize data on the disk unless we have to.
And data element do not change names from table to table in a schema! Get rid of the aliases, even tho they are closer to ISO-11179 than the base table crap.
But you are an old FORTRAN programmer! Your "OODETL” is six uppercase letters as required by FORTRAN I; an SQL programmer would have used something like “Order_Details” instead. The mix of camelCase, FORTRAN and several other non-SQL languages is scary.
Why did you use MONEY? It is not portable and the math is wrong. Google it! Why do you think that “<something>_code_id” makes any sense? A data element can be a “<something>_code” or a “<something>_id” but never that hybrid disaster.
I would do this with VIEWs so that I know the data is always current.
CREATE VIEW NC_GL_Items (..)
AS
SELECT DISTINCT OD.l_line, OD.ldr_color, OD.ldr_spec,
AD.addon_mod_gl_acct
FROM Oodetl AS OD,
Addon_Master AS AD
WHERE ..;
The SELECT DISTINCT should not be there. An account should not be NULL, etc. Why do you have more nulls in one table than entire accounting systems? Why do you set integer zero to an amount, which should be a decimal for money?
This is a crazy quilt that is clearly a bitch to maintain or even read. Can you clean it up or are you screwed?
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL -
Hi,
I am using Access 2013 and I have the following VBA code,
strSQL = "INSERT INTO Master SELECT * from Master WHERE ID = 1"
DoCmd.RunSQL (strSQL)
when the SQL statement is run, I got this error.
SELECT * cannot be used in an INSERT INTO query when the source or destination table contains a multivalued field
Any suggestion on how to get around this?
Please advice and your help would be greatly appreciated!Rather than modelling the many-to-many relationship type by means of a multi-valued field, do so by the conventional means of modelling the relationship type by a table which resolves it into two one-to-many relationship types. You give no indication
of what is being modelled here, so let's assume a generic model where there is a many-to-many relationship type between Masters and Slaves, for which you'd have the following tables:
Masters
....MasterID (PK)
....Master
Slaves
....SlaveID (PK)
....Slave
and to model the relationship type:
SlaveMastership
....SlaveID (FK)
....MasterID (FK)
The primary key of the last is a composite one of the two foreign keys SlaveID and MasterID.
You appear to be trying to insert duplicates of a subset of rows from the same table. With the above structure, to do this you would firstly have to insert rows into the referenced table Masters for all columns bar the key, which, presuming this to be
an autonumber column, would be assigned new values automatically. To map these new rows to the same rows in Slaves as the original subset you would then need to insert rows into SlaveMastership with the same SlaveID values as those in Slaves referenced
by those rows in Slavemastership which referenced the keys of the original subset of rows from Masters, and the MasterID values of the rows inserted in the first insert operation. This would require joins to be made between the original and the new subsets
of rows in two instances of Masters on other columns which constitute a candidate key of Masters, so that the rows from SlaveMastership can be identified.
You'll find examples of these sort of insert operations in DecomposerDemo.zip in my public databases folder at:
https://onedrive.live.com/?cid=44CC60D7FEA42912&id=44CC60D7FEA42912!169
If you have difficulty opening the link copy its text (NB, not the link location) and paste it into your browser's address bar.
In this little demo file non-normalized data from Excel is decomposed into a set of normalized tables. Unlike your situation this does not involve duplication of rows into the same table, but the methodology for the insertion of rows into a table which
models a many-to-many relationship type is broadly the same.
The fact that you have this requirement to duplicate a subset of rows into the same table, however, does make me wonder about the validity of the underlying logical model. I think it would help us if you could describe in detail just what in real world
terms is being modelled by this table, and the purpose of the insert operation which you are attempting.
Ken Sheridan, Stafford, England -
Using combination of insert into and select to create a new record in the table
Hello:
I'm trying to write a stored procedure that receives a record locator parameter
and then uses this parameter to locate the record and then copy this record
into the table with a few columns changed.
I'll use a sample to clarify my question a bit further
-- Create New Amendment
function create_amendment(p_mipr_number in mipr.mipr_number%TYPE, p_new_amendment_number in mipr.amendment_number%TYPE)
return integer is
new_mipr_id integer;
begin
THIS is causing me grief See comments after this block of code
insert into mipr
(select mipr_id from mipr where mipr_number=p_mipr_number),
(select fsc from mipr where mipr_number=p_mipr_number),
45,
(select price from mipr where mipr_number=p_mipr_number),
practical,
(select part_number from mipr where mipr_number=p_mipr_number);
THe above will work if I say the following
insert into mipr
(select * from mipr where mipr_number=p_mipr_number);
BUt, Of course this isn't what I want to do... I want to duplicate a record and change about 3 or 4 fields .
How do I use a combination of more than one select and hard coded values to insert a new record into the table.
/** Ignore below this is fine... I just put a snippet of a function in here ** The above insert statement is what I need help with
select (mipr_id) into new_mipr_id from mipr where mipr_number=p_mipr_number + amendment_number=(select max(amendment_number) + 1);
return new_mipr_id;
end;
THANK YOU IN ADVANCE!
KTfunction create_amendment(p_mipr_number in mipr.mipr_number%TYPE)
return integer is
new_mipr_id integer;
tmp_number number;
tmp_mipr_id integer;
begin
tmp_number :=(select max(amendment_number) from mipr where mipr_number=p_mipr_number);
Question:
tmp_number :=1; works..
tmp_number doesn't work with the select statement?
Obviously I'm a novice! I can't find anything in my book regarding tmp variables... What should I look under is tmp_number a
variable or what? In my Oracle book, variable means something different.
Thanks!
KT
I have the following code in my stored procedure:
Good luck,
Eric Kamradt
Maybe you are looking for
-
XML parser for C 2.0.2
I would like to get the parser for C 2.2 for NT from somewhere. On the product listing on technet it is liste, but when I go to the download page, I can only find version 2.0.1!! Where can I find the latest version please? null
-
Error when executing report with large data selection in Infoview
Hello, This is regarding Error we are facing in BO system. We have installed: Business Object Enterprise XI 3.1 Crystal report 2008 When we are trying to execute report in BO INFOVIEW on production system, we are facing following error (After 10 min
-
Can you transfer single iweb page b/t computers?
i know about the 'domain' file to move one iweb site from one computer to another, but is there a way to import one individual/single iweb page created on one computer into iweb on another computer? Message was edited by: ketie Message was edited by:
-
How an application knows that it runs on Exadata ?
Hi We have same sources code which run on 10gR2 and Exadata. So, to use new features (like EHCC) of Exadata, our applications have to know automaticaaly (without setting a environment variable) on which system they run. Is there a Oracle view or some
-
Adobe LiveCycle Designer & Mac?
Please help me understand this: I have a Mac. I have Acrobat Pro XI via the Creative Cloud. There is a button within Acrobat Pro XI that says "Edit Text & Images" When I press this button it says "This form cannot be edited in Acrobat. Please use Ado