My goal is to create a spreadsheet of all the tables in the current database that will display the table name and the number of rows in each table. A bonus would be to also be able to display when anything was last entered or changed in the table. I'm not sure if the last part is possible though.
This is what I have so far... its nothing much but I just can't beat my head around how to add the per table row count to it.
SELECT name, type, type_desc
FROM [scheme].sys.all_objects
WHERE type = 'U'
You can use this query against the system catalog views:
SELECT
t.NAME AS TableName,
p.rows AS RowCounts
FROM
sys.tables t
INNER JOIN
sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN
sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
WHERE
t.NAME NOT LIKE 'dt%'
AND t.is_ms_shipped = 0
AND i.OBJECT_ID > 255
GROUP BY
t.Name, p.Rows
ORDER BY
t.Name
to get the desired result - all tables in the current database, and the number of rows in each.
CREATE TABLE #T
(TABLENAME VARCHAR(100) ,
COUNT INT)
INSERT INTO #T
EXEC SP_MSFOREACHTABLE 'SELECT ''?'' ,COUNT(*) FROM ?'
SELECT * FROM #T
DROP TABLE #T
Related
My goal is to get row counts from the temp tables using code similar to below and quickly validate that the temp tables are working without typing each of them individually. I can see all of the local temp tables from the session using this code:
SELECT *
FROM tempdb.sys.tables
WHERE name LIKE '#%'
The results I want is something like:
name
row_count
#temp1
2000
#temp2
0
#temp3
0
Thank you for your time.
The names are not going to match exactly, but if you're using this to eyeball things, that shouldn't matter too much.
SELECT t.name, p.rows
FROM tempdb.sys.tables as t
join tempdb.sys.partitions as p
on p.object_id = t.object_id
and p.index_id in (0, 1)
WHERE name LIKE '#%';
I have 1000+ tables, SPs, and Views in my database.
How do I get a list of all "Column Names" across whole database that are being referenced in one particular table only; and not in any other tables or SPs or Views in my database?
i.e. I have Database A. which has Tables: B, C, D; Views: E, F, G; and SPs: H, I, J. Table B has 500 columns; and for example 250 columns are being referenced in other Views or SPs but 250 are not being used anywhere else. How do I get the list of Columns Names that are being used in Database B only and not anywhere else?
This is an interesting question. This will show you all fields in all tables.
SELECT T.name AS Table_Name ,
C.name AS Column_Name ,
P.name AS Data_Type ,
P.max_length AS Size ,
CAST(P.precision AS VARCHAR) + '/' + CAST(P.scale AS VARCHAR) AS Precision_Scale
FROM sys.objects AS T
JOIN sys.columns AS C ON T.object_id = C.object_id
JOIN sys.types AS P ON C.system_type_id = P.system_type_id
WHERE T.type_desc = 'USER_TABLE';
This will show you all field names in a specific view.
SELECT *
FROM sys.views
SELECT *
FROM sys.columns
WHERE object_id = OBJECT_ID('dbo.View_1')
Can you start with that? Post back if you have additional questions.
I am trying to achieve finding the number of rows in a given column, I understand that we can achieve that by doing something like
SELECT DISTINCT s.name as SchemaName,OBJECT_NAME(o.OBJECT_ID) AS TableName,p.row_count
FROM
SYS.objects o JOIN SYS.schemas s
ON o.schema_id=s.schema_id
JOIN sys.dm_db_partition_stats p
ON o.object_id=p.object_id
WHERE o.type LIKE 'U'
AND s.name LIKE '%dbo%' -- schema name
ORDER BY TableName
However I could not find a join between the Sys.Databases and the aforementioned query. Suggestions? Or is there any efficient way to achieve the same?
Hi I am working on SQL query, a total novice.
So I only have column name, and trying to find which table it is belong to.
How do I find that in toad? Can someone help?
Most databases have tables that describe the contents of the databases. If you are using toad, then I might surmise that you are using Oracle.
If so, you can use:
select *
from syscolumns
where columnname = <whatever you are looking for>
And then lookup the referenceid in systables.
In many other databases, you can use:
select *
from INFORMATION_SCHEMA.columns
where column_name = <whatever you are looking for>
Something like this should work, it will return the column name you search and the associated table
SELECT c.name as columnname, t.name as tablename from sys.columns c
join sys.tables t on c.object_id = T.object_id
where c.name =' put the column you want to find here'
Is there a way to determine what the maximum size of a record would be in SQL Server past doing it by hand? For example:
CREATE TABLE test (
id INT PRIMARY KEY IDENTITY(1, 1),
name VARCHAR(256),
test_date DATETIME
)
so, if I'm not mistaken, when calculating that by hand that record could be a maximum of 272 bytes. However, I have a table with a lot more columns than that, and I need to do this for more than one table, so I wanted to know if I could do this with a simple query.
I can't find any information in INFORMATION_SCHEMA.TABLES or even INFORMATION_SCHEMA.COLUMNS where I figured I could do a simple SUM for example. Further, sysobjects and syscolumns don't seem to have the necessary information. The syscolumns table does have a length field but that's not the actual storage size.
Thanks all!
Try this:
Select schema_name(T.schema_id) As SchemaName,
T.Name As TableName,
Sum(C.max_length) As RowSize
From sys.tables T
Inner Join sys.columns C
ON T.object_id = C.Object_ID
INNER JOIN sys.types S
On C.system_type_id = S.system_type_Id
Group By schema_name(T.schema_id),
T.Name
Order By schema_name(T.schema_id),
T.Name