List schema name and owners in sql server 2012 - sql-server

I wanted to list all the table names, column names , schema names and owner in all databases and wrote the code below . I am not sure where to get the schema owners details to add to my query . Please help .
select c.name as colomn_name , t.name as table_name , s.name as schema_name
from sys.columns c
inner join sys.tables t on c.object_id=t.object_id
INNER JOIN sys.schemas AS s ON t.[schema_id] = s.[schema_id]

The column principal_id in sys.schemas contains the ID of the schema owner, so to get the name you can simply use:
USER_NAME(s.principal_id) AS Schema_Owner
Alternatively, if you want more information you can join to sys.sysusers:
SELECT s.Name, u.*
FROM sys.schemas s
INNER JOIN sys.sysusers u
ON u.uid = s.principal_id

Here is a more portable solution that works under SQL Server:
SELECT schema_name, schema_owner
FROM information_schema.schemata

Related

SQL Server query to find tables across all databases, that references a column in a table?

I want to find the tables from all the databases that references a column in a table in one of the database. Can anyone help me here?
Not sure how to proceed
this should do, please see that i have used like operator (where col.name like '%COLUMN_NAME_HERE%'), so it will find matching patterns, if you want to find exact match then please use (where col.name = 'COLUMN_NAME_HERE')
select
schema_name(tab.schema_id) as schema_name
,tab.name as table_name
, col.column_id,col.name as column_name
, t.name as data_type, col.max_length, col.precision
from sys.tables as tab
inner join sys.columns as col on tab.object_id = col.object_id
left join sys.types as t on col.user_type_id = t.user_type_id
where col.name like '%COLUMN_NAME_HERE%'
order by schema_name,table_name, column_id*

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

Filter table in long format i.e. keep rows if condition in group is satisfied

I want to query and see which table do not have a column called hello.
I first use a query from this question: Find all tables containing column with specified name - MS SQL Server
SELECT c.name AS 'ColumnName'
,t.name AS 'TableName'
,c.object_id
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
WHERE t.name in (select colname from someTable) -- table with one column containing table names.
So this gives me a table in long format i.e. every table has many rows. Now I want figure out which table does not have a column called hello.
Here an example for the query with a cte. However, you have to consider that the same table name might exist in different schemas, so if you work with schemas you should add a join to sys.schemas and include this in your query:
WITH cteColTabs AS(
SELECT t.name AS TableName
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
WHERE t.name in (select colname from someTable) -- table with one column containing table names.
AND c.name = N'hello'
)
SELECT t.Name AS TableName
FROM sys.tables t
LEFT JOIN cteColTabs ct ON ct.TabName = t.Name
WHERE ct.TableName IS NULL

List down the column names in a Temporary Table in SQL Server

I am a beginner to SQL Server Database. I have a basic question.
How to retrieve the column names of a temporary table in SQL Server?
I have tried querying the sys.objects, but the table is not listing there.
Temp tables are stored in the tempdb database so you have to query the views from the tempdb database. Something like this:
SELECT c.name
FROM tempdb.sys.objects o
INNER JOIN tempdb.sys.columns c ON c.object_id = o.object_id
WHERE o.name LIKE '#TempTableName%';
SELECT Obj.NAME
,Col.NAME
FROM tempdb.sys.objects Obj
INNER JOIN tempdb.sys.columns Col ON Obj.object_id = Col.object_id
WHERE Obj.NAME LIKE '#tmp%'
But please note that the local temporary table names will not be unique. We can have the same names from different sessions. So be careful with the query.
Here's how to get the temp table for just the current session.
SELECT c.name
FROM tempdb.sys.objects o
INNER JOIN tempdb.sys.columns c ON c.object_id = o.object_id
WHERE o.object_id = object_id('tempdb..#TempTableName)
`SELECT STUFF(
`(SELECT ',' + LTRIM(RTRIM(ISNULL(name,'')))
`FROM tempdb.sys.columns
`WHERE Object_ID = Object_ID(N'tempdb..#TEMP')
`FOR XML PATH(''), TYPE).value('.','varchar(max)'), 1, 1, '')
`AS comma_separated_list

Resources