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;
Related
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.
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
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.
This is my first time posting on SO, so please go easy!
I'm attempting to write a SQL script that queries the same table for a list of databases in a single SQL Server instance.
I have successfully queried the list of databases that I required using the following, and inserting this data into a temp table.
Select name Into #Versions
From sys.databases
Where name Like 'Master%'
Master is suffixed with numerical values to identify different environments.
Select * From #Versions
Drop Table #Versions
The table name I am trying to query, is the same in each of the databases, and I want to extract the newest value from this table and insert it into the temp table for each of the database names returned.
I have tried researching this but to no avail. I am fairly comfy with SQL but I fear I could be out of my depth here.
You can do the following. Once you have the list of your databases, you can build up the query (you need to edit it for your purpose).
Select name Into #Versions
From sys.databases
Where name Like 'test%'
declare #sql as varchar(max) = ''
select #sql = #sql + 'INSERT INTO sometable SELECT TOP 1 * FROM ' + name + '..sourcetable ORDER BY somedate DESC; '
FROM #Versions
exec (#sql)
Drop Table #Versions
Look at The undocumented sp_MSforeachdb procedure and here
I have question about copy tables from a query into other database.
I use this query in SQL Server:
SELECT * FROM information_schema.tables WHERE TABLENAME = '2000'
This query returns tables. And I would like to copy all the returned tables into my other database.
Thank you in advance.
have a look at sp_MSforeachtable and feed your table names into it from information_schema.tables
there is a good example here to get you started sp_MSforeachtable example
Another example that can be modified is here
This could be amended to suit your needs:
Exec sp_MSforeachtable
#command1 = "SELECT COUNT(*) AS [?] FROM ?",
#whereand = "and uid = (SELECT schema_id FROM sys.schemas WHERE name = 'dbo')
and o.name LIKE 'IIN%'"
Replace #command1 with your copy code (something like SELECT * INTO ... and replace #whereand with your filter for your tables or an IN statement if you have a list.