Need to select all tables from a database in SQL Server where the most recent date-timestamp is from a year or more ago - sql

I'm going through all tables in a database trying to determine which tables are old (have not been altered in a long time). I've been going through and flagging all tables with old DTS's as "old"
Is there a more efficient way to do this? Can I run a statement that scans all tables in a database for date-timestamp fields and then looks at the most recent ones?
Thank you in advance for any help you can provide!

You can use the INFORMATION_SCHEMA.COLUMNS view to retrieve all the tables having the timestamp column (assuming the column name is known and not used for other columns).
Use these to generate Dynamic SQL of the form
SELECT COUNT(*) ,#TableName FROM #TableName WHERE #TimeStampColumn < #TimestampToCheck
The tables where count(*) is 0 are the ones you need to look at

I would do something like this:
Show a COUNT(*) of rows written in the last year... if you get 0, you know the table isn't in use.
Use the information_schema to get the columns/tables.
SELECT 'SELECT COUNT(*) AS ['+TABLE_NAME+'] FROM ['+TABLE_CATALOG+'].['+TABLE_SCHEMA+'].['+TABLE_NAME+'] WHERE '+COLUMN_NAME+' > DATEADD(YY,-1,CONVERT(DATETIME,FLOOR(CONVERT(FLOAT,GETDATE()))))'
-- SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE DATA_TYPE='DATETIME'
--AND (COLUMN_DEFAULT='(getdate())' OR COLUMN_DEFAULT='CURRENT_TIMESTAMP')
You can add the last line in, if you only want columns with a default value.
Then take the output, copy to a new window, and run it!

Related

Query just runs, doesn't execute

my query just runs and doesnt execute, what is wrong. work on oracle sql developer, company server
CREATE TABLE voice2020 AS
SELECT
to_char(SDATE , 'YYYYMM') as month,
MSISDN,
SUM(CH_MONEY_SUBS_DED)/100 AS AIRTIME_VOICE,
SUM(CALLDURATION/60) AS MIN_USAGE,
sum(DUR_ONNET_OOB/60) as DUR_ONNET_OOB,
sum(DUR_ONNET_IB/60) as DUR_ONNET_IB,
sum(DUR_ONNET_FREE/60) as DUR_ONNET_FREE,
sum(DUR_OFFNET_OOB/60) as DUR_OFFNET_OOB,
sum(DUR_OFFNET_IB/60) as DUR_OFFNET_IB,
sum(DUR_OFFNET_FREE/60) as DUR_OFFNET_FREE,
SUM(case when sdate < to_date('20190301','YYYYMMDD')
then CH_MONEY_PAID_DED-nvl(CH_MONEY_SUBS_DED,0)-REV_VOICE_INT-REV_VOICE_ROAM_OUTGOING-REV_VOICE_ROAM_Incoming
else (CH_MONEY_OOB-REV_VOICE_INT-REV_VOICE_ROAM_OUTGOING-REV_VOICE_ROAM_Incoming) end)/100 AS VOICE_OOB_SPEND
FROM CCN.CCN_VOICE_MSISDN_MM#xdr1
where MSISDN IN ( SELECT MSISDN FROM saayma_a.BASE30112020) --change date
GROUP BY
MSISDN,
to_char(SDATE , 'YYYYMM')
;
This is a performance issue. Clearly the query driving your CREATE TABLE statement is taking too long to return a result set.
You are querying from a table in a remote database (CCN.CCN_VOICE_MSISDN_MM#xdr1) and then filtering against a local table (saayma_a.BASE30112020) . This means you are going to copy all of that remote table across the network, then discard the records which don't match the WHERE clause.
You know your data (or at least you should know it): does that sound efficient? If you're actually discarding most of the records you should try to filter CCN_VOICE_MSIDN_MM in the remote database.
If you need more advice you need to provide more information. Please read this post about asking Oracle tuning questions on this site, then edit your question to include some details.
You are executing CTAS (CREATE TABLE AS SELECT) and the purpose of this query is to create the table with data which is generated via this query.
If you want to just execute the query and see the data then remove first line of your query.
-- CREATE TABLE voice2020 AS
SELECT
.....
Also, the data of your actual query must be present in the voice2020 table if you have already executed it once.
Select * from voice2020;
Looks like you are trying to copying the data from one table to another table, Can you once create the table if it's not created and then try this statement.
insert into target_table select * from source_table;

SQL Server 2008 - SELECT query

I have a database with over a 150 tables. I need to be able to find every table that has a column called EmployeeID. Is there a way for me to find all tables that have this column? It's kind of a long process if I go through each table and try to find if it has that column.
Use INFORMATION_SCHEMA.COLUMNS:
select c.*
from INFORMATION_SCHEMA.COLUMNS c
where column_name = 'EmployeeID';

Row with unknown values to column SQL Server 2005

I'm new to SQL Server programming, so this may or may not be a stupid question.
So, first I do not know what my input table is (my task is supposed to work with ANY table).
Second, I get the column names using sp_help and then I select only that column into another table. Now I need the rows from COLUMN_NAME to be the names of god knows how many columns into some new table.
I tried something using PIVOT, but I can't seem to make it work.
It sounds like you want to access the metadata tables. Something like this may put you in the right direction:
insert into column_name(name)
select column_name
from information_schema.columns
where table_name = XXX <----- YOUR TABLE GOES HERE

Query to count the exact number of records in all tables

IHello,
I'm trying to count for each table in my MySQL database how many records there are in it.
All of my tables happen to be in InnoDB and this query
SELECT TABLE_ROWS FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'myschema';
is way much too much of an estimate (There are +846 records and it tells me there are only +-400)
Is there a way to count a more exact number of rows with a similar query? It doen't matter how long the query takes tot run. (P.S. not using php or a similar language)
If the length of the query doesn't matter then what about doing the following for each table in your database:
select Count(*) from MyTable
If your are going to use the system views then you need to make sure your database statiscs are updated or the counts of rows will potentially be incorrect in the system schema views such as information_schema.x.
Use the
select count(*) yourtable;
as stated in the answer above to get the exact number
Analyze table yourtable;
to update the statisitcs then check the count in the system view they should match at ths point for number of rows.
see this website for more details and a way to do this for your entire database:
MySQL reference manual
As posted in this Answer
create table #rowcount (tablename varchar(128), rowcnt int)
exec sp_MSforeachtable
'insert into #rowcount select ''?'', count(*) from ?'
select * from #rowcount
order by tablename
drop table #rowcount

SQL/JDBC : select query on variable tablenames

I'm using Oracle DB and I would like to write a SQL query that I could then call with JDBC. I'm not very familiar with SQL so if someone can help me, that could be great ! Here is the problem. I have a table MY_TABLE wich contains a list of another tables, and I would like to keep only the nonempty tables and those that their names start by a particular string.
The query I wrote is the following :
select TABLE_NAME
from MY_TABLE
where TABLE_NAME like '%myString%'
and (select count(*) from TABLE_NAME where rownum=1)<>0
order by TABLE_NAME;`
The problem comes from the second SELECT, but I don't know how can I do to use the TABLE_NAME value.
Does someone have an idea ?
Thanks.
[Added from comments]
Actually, I need to test the V$ views contained in the ALL_CATALOG table. But if I can find another table where all these views are contained too and with a NUM_ROWS column too, it would be perfect !
Standard versions of SQL do not allow you to replace 'structural elements' of the query, such as table name or column name, with variable values or place-holders.
There are a few ways to approach this.
Generate a separate SQL statement for each table name listed in MY_TABLE, and execute each in turn. Brute force, but effective.
Interrogate the system catalog directly.
Investigate whether there are JDBC metadata operations that allow you to find out about the number of rows in a table without being tied to the system catalog of the specific DBMS you are using.
Can you use oracle view USER_TABLES? then query will be much easier
select TABLE_NAME
from USER_TABLES
where TABLE_NAME like '%myString%'
and Num_ROWS > 0
order by TABLE_NAME;`