I have a table lets say table_names which contains some table names ( table1 , table2 ,table3 etc ). Now I want to fetch the data from table1 , table2 , table3 etc by going through the table table_names.
I am unable to achieve this task. Could anyone please help me out of this situation ?
I am using Oracle DB.
Thank you in advance.
If from TableA you have to get value from Single table then use this,
SELECT * FROM (SELECT table_name FROM tableA WHERE id=3)AS b
Otherwise from getting from multiple table, I prefer below PHP-
while($a=mysql_fetch_assoc(mysql_query((SELECT table_name FROM tableA)))
{ $tablename=$a[table_name];
while($b=mysql_fetch_assoc(mysql_query((SELECT * FROM $tablename))))
{
echo $b[column_name];
}
}
try this query:
DECLARE
str VARCHAR2(30);
num number(5);
BEGIN
SELECT count(*) into num FROM table_names;
FOR i in 1..num loop
SELECT name into str FROM
(SELECT ROWNUM r,t.name FROM table_names t) where r = i;
EXECUTE IMMEDIATE 'SELECT * FROM '||str;
str:='';
end loop;
END;
Select t1.,t2.,t3.* from table1 t1,table2 t2, table3 t3 where (select name from tables)
Related
When I run that query in GBQ I receive this error:
Error running query
Resources exceeded during query execution: Not enough resources for
query planning - too many subqueries or query is too complex.. at
[1:1]
The code below is not developed by me so I have a hard time rewriting it. So would hear if you could help me get it running again.
execute immediate (select concat('create or replace table `ssmdw-208309.temp_piwik_and_ga.005_visitor_union` as ',query) from (select string_agg(CONCAT(' select substr(generate_uuid(),25,12) as rowid, ',columns,' from `piwik-272210.piwik.',table_name,'`'),'\nUnion all\n') query FROM (
SELECT
table_column.table_name,
STRING_AGG(CASE
WHEN x.column_name IS NULL THEN CONCAT('null as ',table_column.column_name)
ELSE
table_column.column_name
END
,', '
ORDER BY
table_column.column_name) columns
FROM (
SELECT
tables.table_name,
columns.column_name
FROM (
SELECT
DISTINCT column_name
FROM
`piwik-272210.piwik.INFORMATION_SCHEMA.COLUMNS` a where table_name like 'visitor_%') columns
FULL OUTER JOIN (
SELECT
DISTINCT table_name
FROM
`piwik-272210.piwik.INFORMATION_SCHEMA.COLUMNS` b where table_name like 'visitor_%') tables
ON
(1=1)) table_column
LEFT JOIN
`piwik-272210.piwik.INFORMATION_SCHEMA.COLUMNS` x
ON
(x.table_name = table_column.table_name
AND x.column_name = table_column.column_name)
GROUP BY
1 )))
Avoid "order by" clause wherever it is possible. "Order by" tries to pull result in one node that in general causes this issue.
Try this. then call the procedure.
create or replace procedure `dataset_name'.'procedure_name'()
BEGIN
create temp table TABL1 as (
SELECT DISTINCT column_name FROM `piwik-272210.piwik.INFORMATION_SCHEMA.COLUMNS` a where table_name like 'visitor_%');
create temp table TABL2 as (
SELECT DISTINCT table_name FROM `piwik-272210.piwik.INFORMATION_SCHEMA.COLUMNS` b where table_name like 'visitor_%');
create temp table TABL3 as
SELECT tables.table_name, columns.column_name FROM (select column_name from TABL1 columns
FULL OUTER JOIN
select table_name from TABL2 tables ON (1=1));
create or replace table dataset.TABL4 as
SELECT table_column.table_name,
STRING_AGG(CASE WHEN x.column_name IS NULL THEN CONCAT('null as ',table_column.column_name) ELSE table_column.column_name END) as columns
FROM (
SELECT table_name, column_name FROM TABL3
) table_column
left join `piwik-272210.piwik.INFORMATION_SCHEMA.COLUMNS` x
ON (x.table_name = table_column.table_name AND x.column_name = table_column.column_name)
))
drop table TABL1 ;
drop table TABL2 ;
drop table TABL3;
END;
I am trying to delete data from two tables at the same time using inner join. However when I tried to run my query, an error
SQL command not properly ended
error came out.
A brief background of what I am trying to do and some info on the tables, table1 and table2. So both tables has a same field, for instance "ABC". I would like to delete data from both tables using inner join but under the where condition of a field (XYZ) under table where it equals to a value.
This is my sql statment:
DELETE table1, table2
FROM table1
INNER JOIN table1 ON table1.ABC = table2.ABC
WHERE table1.XYZ = 'TESTIT';
You can't delete more than one table.
You must use two different DELETE statements.
For this you can create a temporary table to store IDs to delete, for example:
CREATE TABLE app (ABC varchar(100))
INSERT INTO app (ABC)
SELECT abc
FROM table1
INNER JOIN table1 ON table1.ABC = table2.ABC
WHERE table1.XYZ = 'TESTIT';
DELETE
FROM table1
WHERE table1.ABC IN (SELECT ABC FROM app);
DELETE
FROM table2
WHERE table2.ABC IN (SELECT ABC FROM app);
DROP TABLE app;
In Oracle you cannot delete from 2 tables in a single statement like you are doing. The syntax is wrong. You can use as below:
DELETE table1
where table1.ABC = (select table2.ABC
from table2
WHERE table2.ABC = table1.ABC
and table1.XYZ = 'TESTIT');
A PL/SQL solution might be something like this:
declare
type abc_tt is table of table1.abc%type index by pls_integer;
l_abc_collection abc_tt;
begin
select distinct t1.abc bulk collect into l_abc_collection
from table1 t1
join table2 t2 on t2.abc = t1.abc
where t1.xyz = 'TESTIT';
dbms_output.put_line('Stored ' || l_abc_collection.count || ' values for processing');
forall i in 1..l_abc_collection.count
delete table1 t
where t.xyz = 'TESTIT'
and t.abc = l_abc_collection(i);
dbms_output.put_line('Deleted ' || sql%rowcount || ' rows from table1');
forall i in 1..l_abc_collection.count
delete table2 t
where t.xyz = 'TESTIT'
and t.abc = l_abc_collection(i);
dbms_output.put_line('Deleted ' || sql%rowcount || ' rows from table2');
end;
Output:
Stored 1000 values for processing
Deleted 1000 rows from table1
Deleted 1000 rows from table1
Test setup:
create table table1 (abc, xyz) as
select rownum, 'TESTIT' from dual connect by rownum <= 1000
union all
select rownum, 'OTHER' from dual connect by rownum <= 100;
create table table2 as select * from table1;
After deletion there are 100 rows in each table. I have assumed we only want to delete the ones where xyz = 'TESTIT' even when abc values are common to both tables.
select distinct table1.ABC into Temptable
FROM table1
INNER JOIN table1 ON table1.ABC = table2.ABC
WHERE table1.XYZ = 'TESTIT'
delete table1 where ABC in (select ABC from Temptable)
delete table2 where ABC in (select ABC from Temptable)
drop table Temptable
In stored procedure i would like to give a set of inputs, in which i also have some tables.
i could use if condition to handle empty scalar inputs but i dont know how to handle empty table or if the input is left empty.
I would like to have some thing like this
count = select count(*) from input_table;
if :count = 0 or not given(~) --how to check if the input is not given?
then
tab = select * from table;
else
tab = input_table;
end if;
Thank you!
select * from Table t1
where exists(
select 1 from ChildTable t2
where t1.id = t2.parentid)
procedure
if exists(select 1 from table)
begin
-- do stuff
end
I am attempting to delete from multiple tables within a cursor in a DB2 system. However the terminator character ; is causing my cursor to exit prematurely. Currently I can not determine another way to set this query up to achieve the desired results.
I tried the below statement:
DECLARE C1 CURSOR FOR
DELETE FROM TABLE1 WHERE ACCOUNT_NUMBER = ACCOUNT_NUMBER_PARAM;
DELETE FROM TABLE2 WHERE ACCOUNT_NUMBER = ACCOUNT_NUMBER_PARAM;
DELETE FROM TABLE3 WHERE ACCOUNT_NUMBER = ACCOUNT_NUMBER_PARAM;
DELETE FROM TABLE4 WHERE ACCOUNT_NUMBER = ACCOUNT_NUMBER_PARAM;
DELETE FROM TABLE5 WHERE ACCOUNT_NUMBER = ACCOUNT_NUMBER_PARAM;
DELETE FROM TABLE6 WHERE ACCOUNT_NUMBER = ACCOUNT_NUMBER_PARAM;
DELETE FROM TABLE7 WHERE ACCOUNT_NUMBER = ACCOUNT_NUMBER_PARAM;
However this did not run as expected, meaning that it will not loop through a cursor, instead it just runs once.
This also fails to run:
--#SET TERMINATOR #
DECLARE C1 CURSOR FOR
SELECT * FROM TABLE1 WHERE ACCOUNT_NUMBER = ACCOUNT_NUMBER_PARAM#
SELECT * FROM TABLE2 WHERE ACCOUNT_NUMBER = ACCOUNT_NUMBER_PARAM#
SELECT * FROM TABLE3 WHERE ACCOUNT_NUMBER = ACCOUNT_NUMBER_PARAM#
SELECT * FROM TABLE4 WHERE ACCOUNT_NUMBER = ACCOUNT_NUMBER_PARAM#
SELECT * FROM TABLE5 WHERE ACCOUNT_NUMBER = ACCOUNT_NUMBER_PARAM#
SELECT * FROM TABLE6 WHERE ACCOUNT_NUMBER = ACCOUNT_NUMBER_PARAM#
SELECT * FROM TABLE7 WHERE ACCOUNT_NUMBER = ACCOUNT_NUMBER_PARAM#;
OPEN C1#
Here I tried to change the cursor terminator character so that it would get further down and actually open and run the cursor, however this threw a an error and did not run at all.
How can I rewrite this statement to run multiple select statements within a cursor?
After the OPEN C1 you need to loop through the results stored in the cursor.
with the
OPEN C1
FETCH NEXT FROM C1 INTO #Variable1,#Variable2,....( as many as fields in the table )
WHILE ##FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM C1 INTO #Variable1,#Variable2,....( as many as fields in the table )
END
CLOSE CURSOR
DEALLOCATE CURSOS
why do you need a cursor to delete data?
It is far better to do this:
DELETE FROM table1 WHERE colname = (SELECT colname FROM table1 WHERE condition);
DELETE FROM table2 WHERE colname = (SELECT colname FROM table2 WHERE condition);
DELETE FROM table3 WHERE colname = (SELECT colname FROM table3 WHERE condition);
DELETE FROM table4 WHERE colname = (SELECT colname FROM table4 WHERE condition);
There should be no problem running this in a stored procedure.
I've got a table in a testing DB that someone apparently got a little too trigger-happy on when running INSERT scripts to set it up. The schema looks like this:
ID UNIQUEIDENTIFIER
TYPE_INT SMALLINT
SYSTEM_VALUE SMALLINT
NAME VARCHAR
MAPPED_VALUE VARCHAR
It's supposed to have a few dozen rows. It has about 200,000, most of which are duplicates in which TYPE_INT, SYSTEM_VALUE, NAME and MAPPED_VALUE are all identical and ID is not.
Now, I could probably make a script to clean this up that creates a temporary table in memory, uses INSERT .. SELECT DISTINCT to grab all the unique values, TRUNCATE the original table and then copy everything back. But is there a simpler way to do it, like a DELETE query with something special in the WHERE clause?
You don't give your table name but I think something like this should work. Just leaving the record which happens to have the lowest ID. You might want to test with the ROLLBACK in first!
BEGIN TRAN
DELETE <table_name>
FROM <table_name> T1
WHERE EXISTS(
SELECT * FROM <table_name> T2
WHERE
T1.TYPE_INT = T2.TYPE_INT AND
T1.SYSTEM_VALUE = T2.SYSTEM_VALUE AND
T1.NAME = T2.NAME AND
T1.MAPPED_VALUE = T2.MAPPED_VALUE AND
T2.ID > T1.ID
)
SELECT * FROM <table_name>
ROLLBACK
here is a great article on that: Deleting duplicates, which basically uses this pattern:
WITH q AS
(
SELECT d.*,
ROW_NUMBER() OVER (PARTITION BY id ORDER BY value) AS rn
FROM t_duplicate d
)
DELETE
FROM q
WHERE rn > 1
SELECT *
FROM t_duplicate
WITH Duplicates(ID , TYPE_INT, SYSTEM_VALUE, NAME, MAPPED_VALUE )
AS
(
SELECT Min(Id) ID TYPE_INT, SYSTEM_VALUE, NAME, MAPPED_VALUE
FROM T1
GROUP BY TYPE_INT, SYSTEM_VALUE, NAME, MAPPED_VALUE
HAVING Count(Id) > 1
)
DELETE FROM T1
WHERE ID IN (
SELECT T1.Id
FROM T1
INNER JOIN Duplicates
ON T1.TYPE_INT = Duplicates.TYPE_INT
AND T1.SYSTEM_VALUE = Duplicates.SYSTEM_VALUE
AND T1.NAME = Duplicates.NAME
AND T1.MAPPED_VALUE = Duplicates.MAPPED_VALUE
AND T1.Id <> Duplicates.ID
)