With the code:
SELECT *
FROM information_schema.tables
WHERE table_name LIKE 'prefix%';
I can obtain all the tables that I want to truncate, but how can I execute the TRUNCATE TABLE function for the results of that SELECT statement?
You cannot execute TRUNCATE from a SELECT statement. You could do something like
SELECT concat('TRUNCATE TABLE ',table_catalog,'.',table_schema,'.',table_name)
FROM information_schema.tables
WHERE table_name LIKE 'prefix%';
which would return a TRUNCATE statement for each table with the prefix. You could even look into using a stored procedure to execute each single row.
Related
I am running the following query:
select TABLE_SCHEMA ||'.'||TABLE_NAME AS Schema_Table
from qsys2.systables
where table_schema = 'PTLDBA'
AND TABLE_NAME LIKE'DAILY%'
AND DATE (LAST_ALTERED_TIMESTAMP) = CURRENT DATE;
The result set is schema.table name. I need to use the result set to query the actual table
The final query would look like this:
Select * from Schema_Table limit 10;
The table name changes depending on the day. I believe a SQL Procedure would work best but I am not sure how to write it.
How to select all columns from the table besides two or three?
I work with a lot of tables with more than 50 columns, so I can not list of column name...
I hope that it works, but it doesn't
SELECT(
SELECT column_name FROM all_tab_columns
WHERE table_name = <table_name>
AND column_name NOT IT (<columns_name>)
)
from <table_name>;
Could you help me please?
You can construct the query dynamically through a pl/sql procedure and then run it using "execute immediate"
I found some workaround (because PL/SQL to hard for simple select):
CREAT TABLE <tmp> AS SELECT * FROM <table_name>;
ALTER TALBE <tmp> DROP COLUMN <column_name>;
SELECT * FROM <tmp>;
DROP TALBE <tmp>;
It will be useful to simple query...
But for development PL/SQL will be more useful (universally, optimized for server, etc).
I just want to delete the data not the structure.
Generate a TRUNCATE TABLE statement for each table and execute it
You can do this is many ways, some of which are
Use a loop and dynamic SQL
Copy the table names to excel and use it to generate the statement
Generate a script.
Oracle example:
set head off
set pagesize 0
spool t.sql
select 'truncate table '||table_name||';' from all_tables
order by table_name
where rownum < 100
Or if SQL Server, or an ANSI approach is required, use sys.tables or INFORMATION_SCHEMA.tables
select top 100
'truncate table ' + table_name + ';'
from INFORMATION_SCHEMA.tables;
MySQL
select CONCAT("truncate table ", table_name, ";")
from INFORMATION_SCHEMA.tables
limit 100;
Is there a way that I can extract the DDL of all the tables in one single query.
select DatabaseName,TableName,RequestText from db.tables;
gives the database name , table name and the last DDL on the table. But is it possible to get DDL to create the tables in form of resultset to a query instead of querying show db.tableName several times
You can try this:
sel 'show table ' || databasename ||'.'|| tablename ||';'
from dbc.tables where databasename = 'dbname' and tablekind = 'T';
and then you can execute the output of this query to get the DDL like:
show table dbname.sometablename;
I am trying to get all the data from a table and then clear all the data using a stored procedure. I tried the following:
ALTER PROCEDURE "RD1DTA"."my_procedure"()
BEGIN
SELECT * FROM rd1dta.qrtsp
TRUNCATE TABLE rd1dta.qrtsp
END
This only does the SELECT but it doesn't do the TRUNCATE. If I do the TRUNCATE before the SELECT then it does the TRUNCATE but not the SELECT. How do I get it to do both?
I am using sybase to do this.
Daniel,
Use a temp table:
ALTER PROCEDURE "RD1DTA"."my_procedure"()
BEGIN
SELECT * INTO #XYZ FROM rd1dta.qrtsp
TRUNCATE TABLE rd1dta.qrtsp
SELECT * FROM #XYZ
END
Hope that helps.
-Dave