How to TRUNCATE 100 TABLES At a time in SQL - sql

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;

Related

How to truncate all tables that start with the same prefix - Postgresql

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.

How to write a query which selects from a table that is returned by another query

I have table which has basically 2 rows containing the name of failure and the main table i want to write a query such that
Select main
from xyz
will return the table name like abc.
Now I want to get the data from the abc table
Select *
from
(select main
from xyz)
which returns abc.
How can I write it ?
You must use dynamic sql.
Note, that you can't use "SELECT to nowhere" in a compound statement in Db2. That is, the following code is erroneous.
BEGIN
SELECT * FROM MYTAB;
END#
This is why you need to store the result of SELECT somewhere. You may use Global Temporary Tables for that presuming, that USER TEMPORARY TABLESPASE is available to use for your user.
--#SET TERMINATOR #
BEGIN
DECLARE V_STMT VARCHAR (500);
SELECT
'DECLARE GLOBAL TEMPORARY TABLE SESSION.RESULT'
|| ' AS (SELECT * FROM '
|| MAIN
|| ') WITH DATA WITH REPLACE '
|| 'ON COMMIT PRESERVE ROWS NOT LOOGED'
INTO V_STMT
FROM XYZ
-- place your WHERE clause here if needed
FETCH FIRST 1 ROW ONLY
;
EXECUTE IMMEDIATE V_STMT;
END
#
SELECT * FROM SESSION.RESULT
#
dbfiddle link.
Here is a solution on stack that shows how to get the table names from your database
DB2 Query to retrieve all table names for a given schema
Then you could take your failure table and join into it based off of the table name, that should match your errors to the table that match on the table name. I'm not a 100% sure of your question but I think this is what you are asking.
The inner system query has schema and name. Type is T for table. See IBM link below for column reference. You could run the query wide open in the inner query to look for the tables you want. I would recommend using schema to isolate your search.
https://www.ibm.com/docs/en/db2-for-zos/11?topic=tables-systables
SELECT
ft.*
, st.*
FROM [FailureTable] as ft
INNER JOIN
(
select * from sysibm.systables
where CREATOR = 'SCHEMA'
and name like '%CUR%'
and type = 'T'
) st
ON st.[name] = ft.[tablename]
You can try
DECLARE #tableName VARCHAR(50);
SELECT #tableName = main
FROM xyx
EXEC('SELECT * FROM ' + 'dbo.' + #tableName)
Dont forget to add validation if #tableName doesnt get populated

How to show column names in a row

I want to write a SELECT statement to show the list of fields in the table.
COLUMN
column_1
column_2
column_3
You can use the information schema tables, particularly columns:
select column_name
from INFORMATION_SCHEMA.COLUMNS
where table_schema = #schema_name and table_name = #table_name;
Note that this metadata is stored per database. So if you want a table in another database, you need three part naming:
select column_name
from <database>.INFORMATION_SCHEMA.COLUMNS
where table_schema = #schema_name and table_name = #table_name;
Yet one more option: This will return results on any table,ad-hoc query or even a stored procedure.
(using spt_values as a demonstration)
Example
Select column_ordinal
,name
,system_type_name
From sys.dm_exec_describe_first_result_set('Select * From master..spt_values',null,null )
Returns
In SQL Server you can also highlight the tablename in a query then press ALT+F1 to show the highlighted table info.

Oracle | Select * besides <column_name>

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).

Get the DDL of all the tables in teradata as a resultset

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;