DB2 Temp Table - Retrieving inserted Data in DB2 Z OS - db2

I have created a temp table and Inserted in DB2 ZOS as mentioned below
CREATE GLOBAL TEMPORARY TABLE tmp2 (col1 INT)
INSERT INTO tmp2 (col1) VALUES (10687);
INSERT INTO tmp2 (col1) VALUES (10689);
INSERT INTO tmp2 (col1) VALUES (10691);
Inserted data with out any issues, where I'm trying to retrieve the data using select query Im unable see any values which I had inserted with the above values and getting.
select * from tmp2
I have an earlier experience in SQL Server and ran the below queries which work without any issues.
Drop table #tmp2
CREATE TABLE #tmp2 (col1 INT)
INSERT INTO #tmp2 (col1) VALUES (10687);
INSERT INTO #tmp2 (col1) VALUES (10689);
INSERT INTO #tmp2 (col1) VALUES (10691);
select * from #tmp2
How to get to see the inserted data?

Check the documentation for details, sometimes this is faster than waiting for an answer.
The CGTT (create global temporary table) object differs from a regular table when a COMMIT happens - it will empty the table if there are no with hold cursors open on the table. If you have autocommit enabled for your database connection then the result will be that your CGTT table may appear to be empty.
If you want more control over the commit behaviour (and rollback behaviour, and logging options etc. ) you can consider using DGTT (declare global temporary table) instead because that syntax lets you use additional non-default options like on commit preserve rows and on rollback preserve rows. But a DGTT object has more restrictions , including that its qualifier must always be SESSION and its definition is not catalogued so the table is invisible to any other session.

Thanks for all replies.
Below is the set of queries, actually I was looking for
DECLARE GLOBAL TEMPORARY TABLE SESSION.tmp2 (col1 INTEGER)
CCSID EBCDIC ON COMMIT PRESERVE ROWS;
INSERT INTO SESSION.tmp2 (col1) VALUES (10687);
INSERT INTO SESSION.tmp2 (col1) VALUES (10689);
INSERT INTO SESSION.tmp2 (col1) VALUES (10691);
select * from SESSION.tmp2;

Related

How to remove columns for real in postgresql?

I have a large system, and table schema updates quite offtenly, I noticed that after times of removing and recreating new cloumn, limitation "tables can have at most 1600 columns" is shown, but still there are few columns in information_schema.columns.
I've tried vacuum full analyze, still not working, any way to avoid this limitation?
DO $$
declare tbname varchar(1024);
BEGIN
FOR i IN 1..1599 LOOP
tbname := 'alter table vacuum_test add column test' || CAST(i AS varchar(8)) ||' int';
EXECUTE tbname;
END LOOP;
END $$;
alter table vacuum_test drop column test1;
VACUUM FULL ANALYZE vacuum_test;
alter table vacuum_test add column test1 int;
result:
alter table vacuum_test add column test1 int
> ERROR: tables can have at most 1600 columns
> 时间: 0.054s
Unfortunately vacuum full does not remove dropped columns from the table (i.e. entries that have attisdropped = true in `pg_attribute). I would have expected that, but apparently this does not happen.
The only way to get rid of the hidden columns is to create a brand new table and copy the data to the new table.
Something along the lines:
create table new_table (like old_table including all);
insert into new_table
select *
from old_table;
Then drop the old table and rename the new one to the old name. Constraint and index names will be named differently, so you might want to rename them as well.
You will have the re-create all foreign keys (incoming and outgoing) manually as they are not included when using CREATE TABLE (LIKE ...).
Another option is to use pg_repack which does this transparently in the background without locking the table.

sqldeveloper exports integer inserts as strings

It seems like a bug in Version 19.4 which is fixed in 20+
I exported the content of my tables in sqldeveloper and the insert sql statements all have number as strings.
Example:
Insert into testtable(id,stuff) values ('1','Hello')
ID 1 becomes '1' in the export and I have trouble reading it in.
This is the case for every table. Is there a way to avoid the two ' ?
The DDL is:
create table TESTTABLE(
ID INTEGER not null
);
after executing its this in sqldeveloepr:
create table testtable{
"ID" Number(*,0) NOT NULL ENABLE
}
I noticed that I'm able to add such a line, if the constraints are not active. It seems like sqldeveloper convertes the string to a number internally.
create table testtable(
"ID" Number(*,0) NOT NULL ENABLE
);
insert into testtable values (1);
commit;
select /*insert*/ * from testtable;
Running this, i get
Table TESTTABLE created.
1 row inserted.
Commit complete.
REM INSERTING into TESTTABLE
SET DEFINE OFF;
Insert into TESTTABLE (ID) values (1);
No quotes on the number value/field. I did this with version 20.2 of SQL Developer.

Insert Into: Returns inserted records

I'm trying to insert some new records into a table and return the inserted records.
DECLARE #newRecords TABLE
(
Id INT NULL,
Col1 VARCHAR(MAX) NOT NULL,
Col2 VARCHAR(MAX) NOT NULL,
ForeignKey VARCHAR(MAX) NOT NULL
)
INSERT INTO #newRecords
SELECT ColA AS Col1, ColB AS Col2, Key AS ForeignKey
FROM SomeTable;
INSERT INTO PrimaryTable(Col1, Col2)
OUTPUT inserted* INTO #newRecords
SELECT Col1, Col2
FROM #newRecords;
The problem is that outputting the inserted records via the OUTPUT statement causes an error because the column ForeignKey does not exist in PrimaryTable. I am worried about outputting the records to a second temp table because I have no good means of joining the data from PrimaryTable to the #newRecords table.
Ideally what I would like to see happen is:
The new records are populated into the #newRecords temp table from SomeOtherTable
The appropriate columns from the #newRecords temp table are then inserted into the PrimaryTable
Finally the primary keys that were assigned to the newly inserted records in the PrimaryTable are updated into the #newRecords.Id column so that I can go on and do some additional record creations
I've tried messing around with the MERGE statement as well as seeing if I could add some additional statements to the OUTPUT statement but either it's not possible or (more likely) I am not using the syntax correctly.
Any ideas would be greatly appreciated.

is there any issue when creating two temporary tables in one stored procedure

I have written below stored procedure. In which i need to create Two Temporary Tables.
On first temporary table i am taking id's using cursors and then based on some query need to insert in 2nd temporary table. But i get the below error. .
I need some assistance over this situation
There is already an object named 'TempTable' in the database.
ALTER PROCEDURE [dbo].[GetDocumentByTrackID]
#idList nvarchar(50)
AS
BEGIN
SET NOCOUNT ON;
DECLARE #TrackId INT
--Temprary Table 1
CREATE TABLE TempTable(
ID INT
)
DECLARE #GetNextRecord cursor
INSERT INTO TempTable SELECT Value from dbo.fn_sqllist_to_table(#idList,';')
--Temprary Table 2
Create TABLE TracksNameID
(
ID INT,
Name nvarchar(100)
)
SET #GetNextRecord = cursor for select ID from TempTable
open #GetNextRecord
FETCH FROM #GetNextRecord into #TrackId
while ##FETCH_STATUS = 0
BEGIN
INSERT INTO TracksNameID
SELECT ID ,Name FROM dbo.TestTable
WHERE dbo.TestTable.TrackID = #TrackId
END
FETCH FROM #GetNextRecord into #TrackId
DROP TABLE TempTable
END
These aren't actually temporary tables, you're creating real table objects. If the stored procedure fails somewhere, the tables won't be removed. Better stick with table variables:
DECLARE #tableName TABLE (
id int, ... )
and use #tableName like a table (which it is). This way everything will be bound to the local scope of the stored procedure.
The drawback of table variables over temporary tables are the lack of clustered indexes and you can't ALTER table variables.
You creating regular tables, temp table names should begin with #
Try to create #TempTable and #TracksNameID

Is there a way to quickly duplicate record in T-SQL?

I need to duplicate selected rows with all the fields exactly same except ID ident int which is added automatically by SQL.
What is the best way to duplicate/clone record or records (up to 50)?
Is there any T-SQL functionality in MS SQL 2008 or do I need to select insert in stored procedures ?
The only way to accomplish what you want is by using Insert statements which enumerate every column except the identity column.
You can of course select multiple rows to be duplicated by using a Select statement in your Insert statements. However, I would assume that this will violate your business key (your other unique constraint on the table other than the surrogate key which you have right?) and require some other column to be altered as well.
Insert MyTable( ...
Select ...
From MyTable
Where ....
If it is a pure copy (minus the ID field) then the following will work (replace 'NameOfExistingTable' with the table you want to duplicate the rows from and optionally use the Where clause to limit the data that you wish to duplicate):
SELECT *
INTO #TempImportRowsTable
FROM (
SELECT *
FROM [NameOfExistingTable]
-- WHERE ID = 1
) AS createTable
-- If needed make other alterations to the temp table here
ALTER TABLE #TempImportRowsTable DROP COLUMN Id
INSERT INTO [NameOfExistingTable]
SELECT * FROM #TempImportRowsTable
DROP TABLE #TempImportRowsTable
If you're able to check the duplication condition as rows are inserted, you could put an INSERT trigger on the table. This would allow you to check the columns as they are inserted instead of having to select over the entire table.