Find filegroup of a heap table programmatically - sql-server

I have a table say, My_Table, which does not have any indexes. I need to find under which file group it belongs to programmatically.
All the solution I searched on the internet requires an index to be present in the table.
somewhat like below..
SELECT o.[name], o.[type], i.[name], i.[index_id], f.[name]
FROM sys.indexes i
INNER JOIN sys.filegroups f
ON i.data_space_id = f.data_space_id
INNER JOIN sys.all_objects o
ON i.[object_id] = o.[object_id]
WHERE i.data_space_id = f.data_space_id
AND i.data_space_id = 2 -- Filegroup
GO
How to know the same if the table doesn't have any indexes?
version : Microsoft SQL Server 2016 (SP1-GDR)

Related

How to list all database objects with no dependencies?

I would like to list all database objects in a Microsoft SQL database that have no dependencies - e.g. there are no other database objects that depend on them.
I could find this for each table in SQL Server Management Studio, but this is very time consuming (right click on a table, "Check Dependencies"):
(example of a table with no dependencies)
I'm looking how to do this programatically for all database objects in a database - in T-SQL.
You can use the following script to get all objects with no dependendcies.
SELECT
schema_name = s.name,
o.name,
FROM sys.objects o
JOIN sys.schemas s ON s.schema_id = o.schema_id
WHERE NOT EXISTS (SELECT 1
FROM sys.objects o2
WHERE o2.parent_object_id = o.object_id
)
AND NOT EXISTS (SELECT 1
FROM sys.sql_expression_dependencies sed
WHERE sed.referenced_id = o.object_id
);
Note that the first NOT EXISTS will exclude all objects with keys, indexes or defaults. If you only want to look at procedures, functions and views, then remove that.
SELECT
schema_name = s.name,
o.name,
FROM sys.objects o
JOIN sys.schemas s ON s.schema_id = o.schema_id
WHERE NOT EXISTS (SELECT 1
FROM sys.sql_expression_dependencies sed
WHERE sed.referenced_id = o.object_id
);
Note also that dependency checking is not perfect. In particular, dynamic SQL obviously doesn't work, and it won't check application code.
But it's also not reliable for cases when the schema is not specified in the reference.
Charlieface, thanks again for your answer!
I just needed this again and I revisited your answer. I have a small update to your solution / query.
The query returns system tables too, which is something one probably doesn't want to have in the results list.
Updated query:
SELECT
schema_name = s.name,
o.name,
o.type_desc
FROM
sys.objects o
INNER JOIN
sys.schemas s ON s.schema_id = o.schema_id
WHERE
NOT EXISTS (SELECT 1 FROM sys.objects o2 WHERE o2.parent_object_id = o.object_id)
AND NOT EXISTS (SELECT 1 FROM sys.sql_expression_dependencies sed WHERE sed.referenced_id = o.object_id)
AND o.type_desc<>'SYSTEM_TABLE'

SQL Server database roles - tables

I try to to find a SQL statement to get an overview of all database roles to table connections. So I am searching for an n:m connection between sys.tables and sys.database_principals where type_desc='DATABASE_ROLE'.
The goal is to have an overview which roles are on which tables. Can anyone help?
I am using Microsoft SQL Server 2019.
Kind regards
sys.database_permissions is what you are looking for. You can join that to sys.tables and sys.database_principals and get all the info you need
SELECT
schema_name = s.name,
t.name,
per.permission_name,
prin.name,
prin.state_desc
FROM sys.tables t
JOIN sys.schemas s ON s.schema_id = t.schema_id
JOIN sys.database_permissions ON per.major_id = t.object_id
AND per.class = 1 -- object or column
AND per.minor_id = 0 -- table only, not column
JOIN sys.database_principals prin ON prin.principal_id = per.grantee_principal_id
AND prin.type = 'R'; -- same as DATABASE_ROLE

Check if any database table has any rows

I am trying to understand how to check if any table in my db has data using entity framework. I can check for one table but how can I check for all tables at once? DO we have any option with ef6?
using (var db = new CreateDbContext())
{
if(!db.FirstTable.Any())
{
// The table is empty
}
}
Any pointers on how to loop through entities would be helpful.
Here is one way you could do this with t-sql. This should be lightning fast on most systems. This returned in less than a second on our ERP database. It stated 421 billion rows in more than 15,000 partition stats.
select sum(p.row_count)
from sys.dm_db_partition_stats p
join sys.objects o on o.object_id = p.object_id
where o.type not in ('S', 'IT') --excludes system and internal tables.
Similar to #SeanLange, but shows schema name and table name for tables without any rows.
SELECT Distinct OBJECT_SCHEMA_NAME(p.object_id) AS [Schema],
OBJECT_NAME(p.object_id) AS [Table]
FROM sys.partitions p
INNER JOIN sys.indexes i
ON p.object_id = i.object_id
AND p.index_id = i.index_id
WHERE OBJECT_SCHEMA_NAME(p.object_id) != 'sys'
And p.Rows = 0
ORDER BY [Schema], [Table]

How to enumerate all the objects belonging to a file group using SQL query?

How to enumerate all the objects belonging to a file group using SQL query?
I can look up the SSMS > Object Explorer > Properties > Storage on the each table and other objects to see what file group they belong to, but it might not be a feasible solution with hundreds of objects in the database.
From http://blog.sqlauthority.com/2009/06/01/sql-server-list-all-objects-created-on-all-filegroups-in-database/
SELECT o.[name], o.[type], i.[name], i.[index_id], f.[name] FROM sys.indexes i
INNER JOIN sys.filegroups f
ON i.data_space_id = f.data_space_id
INNER JOIN sys.all_objects o
ON i.[object_id] = o.[object_id] WHERE i.data_space_id = f.data_space_id
AND o.type = 'U' -- User Created Tables
You can alter the o.type to control what object types get returned.

list of tables without indexes in sql 2008

How do I list tables without indexes in my SQL 2008 database?
Edit
I want the Schema name and the Table name.
This should cover what your looking for. i.e. tables that are heaps (no clustered index) and do not have any non-clustered indexes. It uses the new sys. table objects used in 2005/2008.
in addition, you probably want to look for tables that do have a clustered index, but have no nonclustered indexes (this is the 2nd part of the statement which I've left commented out.
SELECT
schemaname = OBJECT_SCHEMA_NAME(o.object_id)
,tablename = o.NAME
FROM sys.objects o
INNER JOIN sys.indexes i ON i.OBJECT_ID = o.OBJECT_ID
-- tables that are heaps without any nonclustered indexes
WHERE (
o.type = 'U'
AND o.OBJECT_ID NOT IN (
SELECT OBJECT_ID
FROM sys.indexes
WHERE index_id > 0
)
)
-- OR
-- table that have a clustered index without any nonclustered indexes
--(o.type='U'
-- AND o.OBJECT_ID NOT IN (
-- SELECT OBJECT_ID
-- FROM sys.indexes
-- WHERE index_id>1))
Here's an example:
select SCHEMA_NAME(schema_id), name from sys.tables
where OBJECTPROPERTY(object_id, 'IsIndexed')= 0
In addition to #Philip Fourie's suggestion you might want to think about which indexes to create.
Once you have been accessing your data, SQL Server 2008 keeps track of places where it thinks indexes will be helpful (it refers to these as "missing indexes." There are a hand full of new Dynamic Managed Views which can show these missing indexes and some info about them.
From MSSQlTips:
sys.dm_db_missing_index_details - Returns detailed information about a missing index
sys.dm_db_missing_index_group_stats - Returns summary information about missing index groups
sys.dm_db_missing_index_groups - Returns information about a specific group of missing indexes
sys.dm_db_missing_index_columns(index_handle) - Returns information about the database table columns that are missing for an index. This is a function and requires the index_handle to be passed.
select shema = s.name, table_name = o.name
from sys.objects o
join sys.schemas s on o.schema_id = s.schema_id
where type = 'U'
and not exists (select i.index_id
from sys.indexes i
where i.type <> 0 --ignore default heap index row
and o.object_id = i.object_id )
Edit:
I have updated the SQL to include the Schema name as requested. (Note I had to sys.objects instead of sysobjects to cater for schemas that were introduced in SQL 2005)
The catalog tables are documented in the SQL Server documentation, see this link.
This FAQ contains more samples and might also be useful.
Note that these are system tables and can change between SQL server versions, where possible rather use the system table-independent views called Information Schema Views.
This code gives all the details about the indexes for all the tables:
SELECT
sch.name AS [Schema],
obj.name AS TableName,
indx.name AS IndexName,
CASE
WHEN indx.type_desc = 'HEAP' THEN 'N/A'
ELSE indx.type_desc
END AS IndexType
FROM sys.objects obj
JOIN sys.indexes indx ON indx.object_id = obj.object_id
JOIN sys.schemas AS sch ON sch.schema_id = obj.schema_id
WHERE
obj.type = 'U'
ORDER BY
obj.name

Resources