I want to load a data in temp table in DB2. in syntax when i use ON COMMIT PRESERVE ROWS compiler throws an error:
Lookup Error - DB2 Database Error: ERROR [42601] [IBM][DB2/AIX64] SQL0104N An unexpected token "ON" was found following "P BY ACCOUNT_NUMBER)". Expected tokens may include: "".
When I replaced On commit preserve rows by definition only temp table is successfully created but I don't see my sub query data inserted in the temp table. Can you please help me where I am making mistake?
DECLARE GLOBAL TEMPORARY TABLE SESSION.TEMP_TABLE AS
(SELECT ACCOUNT_NUMBER, NET_AMOUNT
FROM SCHEMA.TABLE_NAME WHERE 1=1 AND COLUMN1='a') ON COMMIT PRESERVE ROWS;
Related
I'm trying to create a temporary table to save some codes, but when I try to insert a code it throws me the following error as if the table did not exist:
can't format message 13:796 -- message file C:\Windows\firebird.msg
not found. Dynamic SQL Error. SQL error code = -204. Table unknown.
TEMPCODES. At line 1, column 13.
These are the lines that I try to run:
create global temporary table TEMPCODES
(
codigo varchar(13)
)
on commit delete rows;
insert into TEMPCODES values('20-04422898-0');
Why can't it find the table if I'm creating it before?
In Firebird, you cannot use a database object in the same transaction that created it. You need to commit before you can use the table.
In other words, you should use:
create global temporary table TEMPCODES
(
codigo varchar(13)
)
on commit delete rows;
commit;
insert into TEMPCODES values('20-04422898-0');
Also, it is important to realise that global temporary tables (GTT) are intended as permanent objects. The idea is to create a GTT once, and then use it whenever you need it. The content of a GTT is only visible to the current transaction (on commit delete rows) or to the current connection (on commit preserve rows). Creating a GTT on the fly is not the normal usage pattern for GTTs.
I have list of Alter Table statements:
BEGIN
ALTER TABLE TABLE1 ALTER COLUMN AA INT -- Error Here
ALTER TABLE TABLE1 ALTER COLUMN BB INT
PRINT('CONTINUE AFTER ERROR')
END
After error its stopped execution and skipped other statements.
In output it shows only 1 error.
But in 2nd case where i have a list of DROP INDEX Statements
BEGIN
DROP INDEX TABLE1.INDEX1 -- Error Here
DROP INDEX TABLE2.INDEX2
PRINT('CONTINUE AFTER ERROR')
END
Here after error, it continues execution and prints error log and the text 'CONTINUE AFTER ERROR'.
Why this difference ?
The difference in behavior is because the first batch of ALTER TABLE statements is a compilation error whereas the second batch of DROP INDEX statements is a runtime error.
When a compilation error occurs on a batch, no code executes and only the compilation error is returned. Also, since no code executes with a compilation error, the error cannot even be caught with structured error handling:
BEGIN TRY
ALTER TABLE TABLE1 ALTER COLUMN AA INT -- Error Here
ALTER TABLE TABLE1 ALTER COLUMN BB INT
PRINT('CONTINUE AFTER ERROR')
END TRY
BEGIN CATCH
PRINT 'CAUGHT ERROR';
END CATCH;
Msg 4902, Level 16, State 1, Line 4 Cannot find the object "TABLE1"
because it does not exist or you do not have permissions.
When compilation is successful and a runtime error happens, subsequent statements in the same batch may or may not execute after an error depending the error severity and XACT_ABORT setting.
Most likely because the ALTER TABLE statements actually touch the data in the given tables. Removing the index does not have that impact, so I guess SQL decides it is OK to continue with the next statement.
I made a sequence in Net Beans. But when I tried to insert data in the table I get an error.
My code is:
CREATE SEQUENCE seq_person
MINVALUE 1
START WITH 1
INCREMENT BY 1
INSERT INTO Persons (ID,FirstName,LastName)
VALUES (seq_person.nextval,'Lars','Monsen');
The error is:
[Exception, Error code 30,000, SQLState 42X04] Column
'SEQ_PERSON.NEXTVAL' is either not in any table in the FROM list or
appears within a join specification and is outside the scope of the
join specification or appears in a HAVING clause and is not in the
GROUP BY list. If this is a CREATE or ALTER TABLE statement then
'SEQ_PERSON.NEXTVAL' is not a column in the target table.
The error message Error code 30,000, SQLState 42X04 indicates you are using Derby DB not Oracle. That being the case you need to use Derby syntax for getting the next value. So your insert should look like this:
INSERT INTO Persons (ID,FirstName,LastName)
VALUES (NEXT VALUE FOR seq_person,'Lars','Monsen')
How can rows be inserted into a table from a table in a remote database?
We currently have a stored procedure which does this using a database link. However, we are having to delete the link because our company policy doesn't allow their usage.
begin
...
execute immediate 'insert into '|| table_name
|| ' (select * from schema_name.'|| table_name ||'#link)';
...
end;
I'm unable to use the COPY command as it appears to be not recognized.
COPY FROM username/pwd#SID
insert into table_name using (select *
from table_name);
Error report:
SQL Error: ORA-00926: missing VALUES keyword
00926. 00000 - "missing VALUES keyword"
*Cause:
*Action:
According to this SQL Plus page, COPY command is now obsolete.
Your Query syntax is slightly wrong, and you just need to specify INSERT/REPLACE/CREATE .. and INTO is NOT needed.
COPY is not obsolete, but it ends up with some encoding issues.
You would use the line continuation character to get around that.
COPY
FROM username/pwd#SID
TO username/pass#SID2
insert
table_name
using
select * from schema_name.table_name;
You can also, download the table data into a text file, and use SQL*Loader to load the data into the another Database.
I prefer the SQL*Loader option! Since maintenance is easy!
For download,
- you can either use SPOOL with delimiter option as mentioned Here
- Write a ProC/PLSQL to output the data into a File (Method 4 in ProC, OR select column names from dba_columns))
I am getting an error when I attempt to run this query:
create table New_Table as
select NAme
from File_name, FileType
where File_name.name = FileType.Name
Here is the error I am getting:
"ORA-00604: error occurred at recursive SQL level 2
ORA-01422: exact fetch returns more than requested number of rows"
Any idea why?
Is NAme a separate field from file_name.name and filetype.name? If not, you would need to specify file_name.name or filetype.name in your SELECT statement.