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?
Zoli
You 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.
Similar Messages
-
Best Practice to fetch SQL Server data and Insert into Oracle Tables
Hello,
I want to read sqlserver data everry half an hour and write into oracle tables ( in two different databases). What is the best practice for doing this?
We do not have any database dblinks from oracle to sqlserver and vice versa.
Any help is highly appreciable?
ThanksWell, that's easy:
use a TimerTask to do the following every half an hour:
- open a connection to sql server
- open two connections to the oracle databases
- for each row you read from the sql server, do the inserts into the oracle databases
- commit
- close all connections -
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 -
MS SQL Server 2014: Error inserting into Temp table with index and identity field
In this thread, I mentioned a problem with SQL Server 2014:
SQL Server 2014: Bug with IDENTITY INSERT ON
The question was answered, it is a bug. To keep you informed on this issue, I open this discussion.
Problem:
The code below works perfectly fine on MS SQL Server 2008 R2 and MS SQL Server 2012, but gives an error every second time the proc is executed on MS SQL Server 2014. If I do not define any index on the temp table, the problem disappears. Defining the index
after the insert, does not help.
SET NOCOUNT ON
GO
IF EXISTS (SELECT 1 FROM sys.procedures WHERE name = 'usp_Test') DROP PROC dbo.usp_Test;
GO
CREATE PROC dbo.usp_Test AS
BEGIN
SET NOCOUNT ON
CREATE TABLE #Source(ID integer NOT NULL);
INSERT INTO #Source VALUES (1), (2), (3);
CREATE TABLE #Dest (ID integer IDENTITY(1,1) NOT NULL);
CREATE INDEX #IDX_Dest ON #Dest (ID);
PRINT 'Check if the insert might cause an identity crisis';
SELECT 'Source' AS SourceTable, * FROM #Source;
SELECT 'Destination' AS DestTable, * FROM #Dest;
SET IDENTITY_INSERT #Dest ON;
PRINT 'Do the insert';
INSERT INTO #Dest (ID) SELECT ID FROM #Source;
PRINT 'Insert ready';
SET IDENTITY_INSERT #Dest OFF;
SELECT * FROM #Dest;
DROP TABLE #Source;
DROP TABLE #Dest;
END;
GO
PRINT 'First execution of the proc, everything OK';
EXEC dbo.usp_Test;
PRINT '';
PRINT 'Second execution of the proc, the insert fails.';
PRINT 'Removing the index #IDX_Dest causes the error to disappear.';
EXEC dbo.usp_Test;
GO
DROP PROC dbo.usp_Test;
GOThere is some progress. Communication from a former Microsoft employee tells us this:
Shivendra Vishal
Engineer at Microsoft
I am no longer with MS, and I do not have code access, however from the public symbols, I could make out following:
sqlmin!SetidentI2I4+0x1f3:
000007fe`f4d865d3 488b10 mov rdx,qword ptr [rax] ds:00000000`00000000=????????????????
ExceptionAddress: 000007fef4d865d3 (sqlmin!SetidentI2I4+0x00000000000001f3)
ExceptionCode: c0000005 (Access violation)
ExceptionFlags: 00000000
NumberParameters: 2
Parameter[0]: 0000000000000000
Parameter[1]: 0000000000000000
Attempt to read from address 0000000000000000
This is a read AV and from registers it is clear that we were trying to move the value of location pointed by qword of register rax which is not valid:
rax=0000000000000000 rbx=0000000000000038 rcx=0000000000001030
rdx=0000000000000006 rsi=00000001f55def98 rdi=00000000106fd070
rip=000007fef4d865d3 rsp=00000000106fcf40 rbp=00000000106fcfe9
r8=0000000000000000 r9=00000001f55def60 r10=00000001f55defa0
r11=00000000106fcd20 r12=0000000000000000 r13=0000000000000002
r14=00000001f49c3860 r15=00000001f58c0040
iopl=0 nv up ei pl nz na po nc
cs=0033 ss=002b ds=002b es=002b fs=0053 gs=002b efl=00010206
The stack is:
# Child-SP RetAddr Call Site
00 00000000`106fcf40 000007fe`f30c1437 sqlmin!SetidentI2I4+0x1f3
01 00000000`106fd050 000007fe`f474e7ce sqlTsEs!CEsExec::GeneralEval4+0xe7
02 00000000`106fd120 000007fe`f470e6ef sqlmin!CQScanUpdateNew::GetRow+0x43d
03 00000000`106fd1d0 000007fe`f08ff517 sqlmin!CQueryScan::GetRow+0x81
04 00000000`106fd200 000007fe`f091cebe sqllang!CXStmtQuery::ErsqExecuteQuery+0x36d
05 00000000`106fd390 000007fe`f091ccb9 sqllang!CXStmtDML::XretDMLExecute+0x2ee
06 00000000`106fd480 000007fe`f08fa058 sqllang!CXStmtDML::XretExecute+0xad
07 00000000`106fd4b0 000007fe`f08fb66b sqllang!CMsqlExecContext::ExecuteStmts<1,1>+0x427
08 00000000`106fd5f0 000007fe`f08fac2e sqllang!CMsqlExecContext::FExecute+0xa33
09 00000000`106fd7e0 000007fe`f152cfaa sqllang!CSQLSource::Execute+0x86c
0a 00000000`106fd9b0 000007fe`f152c9e8 sqllang!CStmtExecProc::XretLocalExec+0x25a
0b 00000000`106fda30 000007fe`f152a1d8 sqllang!CStmtExecProc::XretExecExecute+0x4e8
0c 00000000`106fe1e0 000007fe`f08fa058 sqllang!CXStmtExecProc::XretExecute+0x38
0d 00000000`106fe220 000007fe`f08fb66b sqllang!CMsqlExecContext::ExecuteStmts<1,1>+0x427
0e 00000000`106fe360 000007fe`f08fac2e sqllang!CMsqlExecContext::FExecute+0xa33
0f 00000000`106fe550 000007fe`f0902267 sqllang!CSQLSource::Execute+0x86c
10 00000000`106fe720 000007fe`f0909087 sqllang!process_request+0xa57
11 00000000`106feee0 000007fe`f2bf49d0 sqllang!process_commands+0x4a3
12 00000000`106ff200 000007fe`f2bf47b4 sqldk!SOS_Task::Param::Execute+0x21e
13 00000000`106ff800 000007fe`f2bf45b6 sqldk!SOS_Scheduler::RunTask+0xa8
14 00000000`106ff870 000007fe`f2c136ff sqldk!SOS_Scheduler::ProcessTasks+0x279
15 00000000`106ff8f0 000007fe`f2c138f0 sqldk!SchedulerManager::WorkerEntryPoint+0x24c
16 00000000`106ff990 000007fe`f2c13246 sqldk!SystemThread::RunWorker+0x8f
17 00000000`106ff9c0 000007fe`f2c13558 sqldk!SystemThreadDispatcher::ProcessWorker+0x3ab
18 00000000`106ffa70 00000000`775d59ed sqldk!SchedulerManager::ThreadEntryPoint+0x226
19 00000000`106ffb10 00000000`7780c541 kernel32!BaseThreadInitThunk+0xd
1a 00000000`106ffb40 00000000`00000000 ntdll!RtlUserThreadStart+0x21
Unassembling the function:
000007fe`f4d8658e 4c8b10 mov r10,qword ptr [rax]
000007fe`f4d86591 4533e4 xor r12d,r12d
000007fe`f4d86594 410fb7d5 movzx edx,r13w
000007fe`f4d86598 4533c9 xor r9d,r9d
000007fe`f4d8659b 4533c0 xor r8d,r8d
000007fe`f4d8659e 488bc8 mov rcx,rax
000007fe`f4d865a1 4489642420 mov dword ptr [rsp+20h],r12d
000007fe`f4d865a6 41ff5230 call qword ptr [r10+30h]
000007fe`f4d865aa 8b5597 mov edx,dword ptr [rbp-69h]
000007fe`f4d865ad 4c8b10 mov r10,qword ptr [rax]
000007fe`f4d865b0 4489642438 mov dword ptr [rsp+38h],r12d
000007fe`f4d865b5 4489642430 mov dword ptr [rsp+30h],r12d
000007fe`f4d865ba 458d442401 lea r8d,[r12+1]
000007fe`f4d865bf 4533c9 xor r9d,r9d
000007fe`f4d865c2 488bc8 mov rcx,rax
000007fe`f4d865c5 c644242801 mov byte ptr [rsp+28h],1
000007fe`f4d865ca 4488642420 mov byte ptr [rsp+20h],r12b
000007fe`f4d865cf 41ff5250 call qword ptr [r10+50h]
000007fe`f4d865d3 488b10 mov rdx,qword ptr [rax] <=================== AV happened over here
000007fe`f4d865d6 488bc8 mov rcx,rax
000007fe`f4d865d9 4c8bf0 mov r14,rax
000007fe`f4d865dc ff5268 call qword ptr [rdx+68h]
000007fe`f4d865df 488d55e7 lea rdx,[rbp-19h]
000007fe`f4d865e3 4c8b00 mov r8,qword ptr [rax]
000007fe`f4d865e6 488bc8 mov rcx,rax
000007fe`f4d865e9 41ff5010 call qword ptr [r8+10h]
000007fe`f4d865ed f6450a04 test byte ptr [rbp+0Ah],4
I remember few issues with scan2ident function, I am not sure if they have fixed it however it appears that this is intoduced to SQL 2014 and we need help from MS to get this resolved as it needs code analysis.
It is not getting simulated for other versions of SQL apart from SQL 2014.
Also to add, interestingly, the value of rax is not visibly changed and it was successfully passed on to rcx, which has a valid value, so something should have changed the value of rax inside call to function using call qword ptr [r10+50h], and looking at this
it appears that it might be a list of functions and we are going at particular offset [50h]. So, bottom line is that the call to function qword ptr [r10+50h], should be changing something in rax, and debugging/analyzing this code might give us some more idea. -
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 -
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.
--vjThere 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. -
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 -
Hy,
i have a little question. Try this:
create table strangetab (
id varchar2(20),
textfield varchar2(200)
CREATE OR REPLACE PROCEDURE writestrangetab
id VARCHAR2,
textfield varchar2 default null)
IS
pragma autonomous_transaction;
BEGIN
INSERT INTO strangetab
(id, textfield
) VALUES
(id, textfield
COMMIT;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line(sqlerrm);
rollback;
END;
After table and procedure creation, try to run this script (simple insert into in autonompus_transaction):
set serveroutput on
declare
vstr varchar2(10000);
begin
for i in 1..2000
loop
vstr := vstr||'r';
end loop;
dbms_output.put_line('write a');
writestrangetab( 'idx' , 'a');
dbms_output.put_line('write big string');
writestrangetab( 'idx' , vstr);
dbms_output.put_line('write b');
writestrangetab('idx' , 'b');
end;
The script generate this output:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
write a
write big string
ORA-12899: value too large for column "STRANGETAB"."TEXTFIELD" (actual:
2000, maximum: 200)
write b
As i expect, the second write goes wrong but first and third no and it put string a and b into srangetable.
Now try to run this:
set serveroutput on
declare
vstr varchar2(10000);
begin
for i in 1..5000
loop
vstr := vstr||'r';
end loop;
dbms_output.put_line('write a');
writestrangetab( 'idx' , 'a');
dbms_output.put_line('write big string');
writestrangetab( 'idx' , vstr);
dbms_output.put_line('write b');
writestrangetab('idx' , 'b');
end;
The only difference from the previous is the vstr dimension: first 2000 now 5000.
In my db (10.2.0.4 on linux) the output of this, is:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
write a
write big string
ORA-01461: can bind a LONG value only for insert into a LONG column
write b
ORA-01001: invalid cursor
If i query the strangetable i am non able to find string b (Only string a was registered)!
As if that were not enough, If you try to insert others writestrangetab, anything after wath that produce the error will be stored!
It is normal?
By
StefanoTanks for your reply Jhon, but:
1) "The second run you are trying to pass 5000 characters to a varchar, which caused your error when trying to call your procedure"
Why you say "which caused". When i call a procedure can't pass more than 4000 Chars? I know that pl/sql varchar2 is up to 32000 Chars even in procedure calls!
2) "That is why your cursor because invalid"
Witch cursor? The strange thing is that there isn't any cursor in the autonomous procedure! The anly statement is an insert into!
The problem of the second miss in the second run is ok: that wont register the string. The main problem is the miss of the third in the seconf run.
Why it not register the third writestrangetab? In that case i pass only a 'b' char. Writestarngetab Is an autonomous transaction so i expect a miss if it fail, but the next, if ok, will be registerd. why not?
By
Stefano -
Large insert into tables, problems with rollback
I have applications that inserts or updates a large number of records. After each insert or update a commit is issued, it would release rollback area. But after some time running, i receive an error message about no space in rollback area. How can i force a write into disk to release rollback area ?
Thanks.Sounds like your still running out of space in your rollback segment with your transaction being too big - as Oracle should always write the rollback segment to disk upon commit. What might help is to specify a rollback segment you want to use for the transaction or use the 'no logging' option - though this means you can't rollback - and if anything bad happens you can end up with an unhappy database. My suggestion would be to create a much larger rollback segment and pin the statement to that rollback segment, you can even switch the rollback segment on and off again just for this transaction.
Hope that helps -
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 -
How do i insert into more than one table from temp table?
Hi,
I have three tables such as temp,stck and stol
temp table contains data as below. It has 22 columns.
STOCK STOCKDESC ALIAS1 ALIAS2 ALIAS3 ALIAS4 ALIAS5 ALIAS6 ALIAS7 ALIAS8 ALIAS9 ALIAS10 ALIAS11 ALIAS12 ALIAS13 ALIAS14 ALIAS15 ALIAS16 ALIAS17 ALIAS18 ALIAS19 ALIAS20
bmg667691031 NOWE FINANSE LTD yy zz B282DV3 TESTICKER te te1 bmg667691031BM te 707943W ex IR-PRIME IR-ALTID IR-BLOOM NT-PRIME NT-ALTID NT-BLOOM AU-PRIME AU-ALTID AU-BLOOM mm
AA0098000181 UFACEX HOLDINGS VAR RT DUE 06-30-2010 kk yy mm TESTICKER aa ff AA0098000181GB bb 031969W cc IR-PRIME IR-ALTID IR-BLOOM NT-PRIME NT-ALTID NT-BLOOM AU-PRIME AU-ALTID AU-BLOOM ba
AC1350Z7M923 CDA GOVT TREAS BILLS CDS- 08-05-2010 ee ff gg TESTICKER hh ij AC1350Z7M923CA mn 1A1MTLU op IR-PRIME IR-ALTID IR-BLOOM NT-PRIME NT-ALTID NT-BLOOM AU-PRIME AU-ALTID AU-BLOOM op
stck table contains as below.It has six columns. But always first three columns will have values.
stock_id stock_code stock_description stock_type terriory_code preferred code
1185072 AED
1185073 ARA CURRENCY ARGENTINA PESO
1185074 ATS CURRENCY AUSTRIAN SCHS
stol table contains as below. It has 6 columns.Terriory_code is always empty.
stock_code territory_code stol_type stock_id stck_code_type sys_entry_date
AED 0 1185072 0 6/22/2007 3:59:13.000 PM
ARA 0 1185073 0 6/22/2007 3:59:13.000 PM
ATS 0 1185074 0 6/22/2007 3:59:13.000 PM
Now, i want to insert into stck and stol table based on temp table records. constraints to insert into stck and stol tables are as below
In temp table first column is called stock. This stock has to compare with stck table stock_code. If it is not present in stck table, then it has to insert into stck table with stock_id and stock_description. Here, I need to generate stock_id my self in the code.
In temp table, column 3 to column 22, i have alias. Each column has to check with stol table stock_code column. For instance, column3 check with stock_code column. Then column4 check with stock_code. If stock_code is not present in stol table, then i have to insert into stol table.
I need to generate stock,id in the code. How do i perform this insertion?
Edited by: user12852882 on Jun 12, 2010 2:37 AMIt can be done using SQL (no loops required)
insert into stock_table (stock_id,stock_code,stock_description)
select stock_id,get_stock_code,stockdesc /* get_stock_code is a function providing stock_code - usually a sequence value */
from (select t.stock stock_id,t.stockdesc,s.stock_id stock_stock_id
from temp_table t,stock_table s
where t.stock = s.stock_id(+)
where s.stock_id is null;
insert into stol_table (stock_code,sys_entry_date) /* not clear where other values to be inserted will come from */
select stock_code,sysdate
from (select t.stock_code,s.stock_code stol_stock_code
from (select distinct stock_code
from (select alias1 stock_code from temp_table union all
select alias2 from temp_table union all
select alias3 from temp_table union all
select alias4 from temp_table union all
select alias5 from temp_table union all
select alias6 from temp_table union all
select alias7 from temp_table union all
select alias8 from temp_table union all
select alias9 from temp_table union all
select alias10 from temp_table union all
select alias11 from temp_table union all
select alias12 from temp_table union all
select alias13 from temp_table union all
select alias14 from temp_table union all
select alias15 from temp_table union all
select alias16 from temp_table union all
select alias17 from temp_table union all
select alias18 from temp_table union all
select alias19 from temp_table union all
select alias20 from temp_table
) /* use unpivot instead if you are 11g */
where stock_code is not null
) t,stol_table s
where t.stock_code = s.stock_code(+)
where s.stock_code is null;
and think about damorgan's post, you'll never regret it (especially when you will not just think)
Regards
Etbin -
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 -
Creating a button for inserting into a table
What I can't do, because I've never needed to, is to create in a page (where I have a Interactive report) a button that when is clicked performs a simple sql (for example an insert into a table using some page item values).
How can I do it?
ThanksHere are some of the steps:
1) Create the button.
2) Then right click on the button name and select Create Dynamic Action
3) Give the DA a name, click next
4) On the "When" Step, Event should already be "Click" and the button name should be filled in
5) For Condition, you would have a condition for when you want the button display. Say, if your page items have a certain range of values then display this button. No condition means the button is always displayed.
6) For the "True Action" Step, for Action select "Execute PL/SQL Code"
[There are other ways to do this, but this is straight-forward for me.]
7) The in the PL/SQL Code block enter say:
Begin
INSERT INTO EMP (EMPNO, ENAME, HIREDATE)
VALUES (:P6_EMPNO, 'MARK1970', :P6_HIREDATE);
COMMIT;
END; --- You would likely make them all page item variables
8) Here the part I'm not 100% sure of.
For "Page Items to Submit", name the page items used in YOUR QUERY -- in my example these are P6_EMPNO,P6_HIREDATE (Note no use of & or : or . here)
Hmm. Start by putting nothing in the "Page Items to Return". If that fails, then try the same page items P6_EMPNO,P6_HIREDATE there as well.
[ I'm too lazy to go run this down at the moment.]
9) I think that's it.
Howard -
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
Maybe you are looking for
-
New to Flash: I have a simple two frame doc. I want the movie to stop at the first frame, wait until the button "jackets_btn" is clicked, then go to second frame and stop. This is the code I wrote which doesn't stop at first frame: stop(); function j
-
Authentication Scheme for sample application in Oracle Express
All, I recently installed Oracle Express on linux and I was browsing the sample application and when I look up the Authentication Scheme for this app I get the message No authentication schemes have been defined. You can create a new authentication s
-
Detach a mask from the original image
Hello everyone, I just started using after effects and I can't find a solution to my first problem. Here is my 'issue': Let's say I have a composition with a PNG image. I created a mask, dividing the picture in two parts. Now I'd like to animate thes
-
My daughter forgot her password and we took it to Apple for a restore. When they did that it wiped it clean except the email address and password attached. Now, she can not remember her id and password for login to even go through set up. So, does an
-
No Price Upate in Inbound ORDRSP
Inbound ORDRSP-->Price Change is not working, even if price is within the tolerance limit. 1) I configured OMGZ-->Adopt Price change flag is active. 2) EDI1 and EDI2 condition type is active. Looks like all my configuration is correct, but price is n