How to Alter Table column to Collection in PL/SQL? - plsql

I want to store value in column of table as list (Nested Table or Table).
How can i do it ?Thank for your help !
I'm use Oracle Database (PL/SQL) and my IDE is SQL developer.

You can refer below snippet to add nested table column in Oracle table. Hope this helps.
CREATE OR REPLACE type number_ntt
IS
TABLE OF NUMBER;
CREATE TABLE WITH_NESTED_COL
(
SR NUMBER,
NUM_TAB NUMBER_NTT
)
nested table NUM_TAB store as NUMBER_TYPE;

Related

Inserting data into Object type table from another existing table..Oracle 11g

I am trying to insert data into an object type table from another existing table in a stored procedure.
I created the "object" type and it's type "table" as below example.
CREATE OR REPLACE TYPE EXAMPLE AS OBJECT
(
ALL THE COLUMNS,
);
/
CREATE OR REPLACE TYPE EXAMPLE_TABLE AS TABLE OF EXAMPLE;
trying to use this type table to get the data inserted from another table.
---
BEGIN
INSERT INTO EXAMPLE_TABLE (ALL THE COLUMS FROM EXAMPLE)
VALUES (SELECT * FROM EMP_TABLE);
is this possible? let me know, or should I use the "CURSOR" to insert one by one..
please let me know how to achieve this,in the form of syntax
--thank you in advance
Example table is a type so you can't insert anything into it, first you would need to define a variable that would be an instance of your example_table. Then you can use a bulk collect to actually populate your table.
declare
example_tab_inst EXAMPLE_TABLE;
begin
select
EXAMPLE(e.col1,e.col2,e.col3)
bulk collect into example_tab_inst
from emp_table e;
.
.
.

Indexes in Teradata

How to Enable/Disable Indexes in TERADATA Database?
I want to disable indexes and do update and then enable the indexes in Teradata.
If Enable/Disable option not available in Teradata, in the sense How can I achieve this ? If I use DROP Indexes, how can I recreate the indexes for all the tables?
Teradata gives you a way to create a table without choosing for primary index (if you are sure of any column).
You can create table with No primary Index. Here is a sample of how to do so:
Create table <table name>
(<columnname> <datatype>,
<columnname> <datatype>)
no primary index ;
Teradata does not have a disable index feature.
All tables have a Primary Index (PI) which is chosen by the RDBMS unless you specify one.
CREATE INDEX <index name> (<column list>) ON table name;
CREATE UNIQUE INDEX (department) ON tbl_employee;
DROP INDEX ind_dept ON tbl_employee;
DROP INDEX (department,emp_number) ON tbl_employee;
In Teradata you can't drop Primary index of a table. The primary index defines where data will reside and which AMP receives the row.
To alter the primary index of a table you need to delete all the records from the table ( As data is already distributed by the row hash value of the PI) then only you can change the primary index of a table by using below command:-
Alter table table_name modify primary index index_name (column list);
Steps to achieve your goal
You can crate a new table with your desired index ( temp, wrk, intermediate table) insert the records from the original table and update the wrk table.
delete the original table and insert the wrk table data.
And you are done.
Create & Drop index is the only option you have here
Simple answer - you can't disable and re-enable indexes in Teradata.
However, there some workarounds.
Drop Index
If you talking about PI (primary index) - you can't drop it. All you can do is to make a copy of a table without an index.
You can drop secondary index though. Then simply create it again when you need it.
Drop-Create Table
This doesn't fit all cases, but often this is the fastest way to do the work, especially if the issue you have is with PI.
BTW: it is not clear, why would you need to do that? Performance or logic or something else? That probably will affect recommendation.
Since you did not specify what kind of index you want to disable/enable, below are the approach you can follow on either cases.
PRIMARY INDEX
CREATE a new table with the same PI
INSERT the updated data to new table
DROP the old table - DROP TABLE <OldTable>;
RENAME the new table same as the old one. - RENAME TABLE <NewTable> TO <OldTable>;
Above recommendation for Primary Index is only applicable if you are going to update a primary index column value. If you will update other columns (not the PI column) then you can just issue an UPDATE Statement directly.
SECONDARY INDEX
DROP the SI - DROP INDEX <IndexName> ON <TableName>;
UPDATE table data
RECREATE the SI - CREATE INDEX <IndexName> (<ColumnList>) ON <TableName>;

I have to update one table field as another field of different table in sql

I have to update one table field as another field of different table in sql. There are two tables, create_account and Trans_Details. I have to update amount field of create_account table and set it into current_balance of Trans_Details table. How can I join these two tables? please write a query for this.
You can utilize the sub queries to do this...
Example:
I do not know what kind of data base r u using i am just giving a pseudo code..
UPDATE create_account SET (amount) = ( SELECT current_balance FROM Trans_Details
WHERE filter = some_value) WHERE filter = some_value.
I have added the where clause in the query, please ignore if u want to update the entire row in the column amount.

create table with constraints from another table in sqlite?

I want create table from another table with constraint?
I used this query "create table destination as select * from source;" fro table creation.
But its copy only the column name in table without column constraint.
There is a special table named sqlite_master, holding the full CREATE TABLE statement for each table (it's modified as appropriate during ALTER TABLE).
I would make my application retrieve that CREATE TABLE statement:
SELECT sql FROM sqlite_master WHERE type='table' AND name='source';
Then I would replace the table name right after CREATE TABLE tokens, and execute the result as a new sqlite query.
I don't think that it's possible to do in sqlite's pure SQL without extensions.

Using temporary tables for update in stored procedure mysql

Can any one depict a simple example for using temporary tables in stored procedure for updating two tables in mysql?
Two kind of temporary tables available. One is session based and the other one is global temporary table.
Below is the simple example:
Select A,b,c into #MyTemp From MyDbTable
In the above example, #myTemp is the temporary table that you are creating. MyDbTable is the one that exist in your database. You can create multiple temporary tables.
I would suggest reading the article from here: Link
--Create a temp table and insert all these counts in it
Create Table #OperatorReportCount(Id int identity,Particulars varchar(100),NoOfArticles int)
--Insert these values in table
Insert Into #OperatorReportCount(Particulars,NoOfArticles) Values('Articles processed',#ProcessedArticleCount)
Insert Into #OperatorReportCount(Particulars,NoOfArticles) Values('Articles approved',#ArticlesApproved)
Insert Into #OperatorReportCount(Particulars,NoOfArticles) Values('Articles rejected',#ArticleRejectedCount)
Insert Into #OperatorReportCount(Particulars,NoOfArticles) Values('Rejections recieved',#RejectionsRecievedCount)
Insert Into #OperatorReportCount(Particulars,NoOfArticles) Values('Articles put on hold',#ArticlesOnHoldCount)
--Select the operator count table
Select Particulars,NoOfArticles From #OperatorReportCount

Resources