user permission list - sql-server

Can anybody help me on this?
How to get the database level permission users list for a particular database in SQL Server 2005?

Simple Google search, with query taken from the first result:
select dp.NAME AS principal_name,
dp.type_desc AS principal_type_desc,
o.NAME AS object_name,
p.permission_name,
p.state_desc AS permission_state_desc
from sys.database_permissions p
left OUTER JOIN sys.all_objects o
on p.major_id = o.OBJECT_ID
inner JOIN sys.database_principals dp
on p.grantee_principal_id = dp.principal_id
Modified slightly to include the database name as well:
select dp.NAME AS principal_name,
dp.type_desc AS principal_type_desc,
o.NAME AS object_name,
p.permission_name,
p.state_desc AS permission_state_desc,
d.[name] as database_name
from sys.database_permissions p
left OUTER JOIN sys.all_objects o on p.major_id = o.OBJECT_ID
inner JOIN sys.database_principals dp on p.grantee_principal_id = dp.principal_id
left outer JOIN sys.sysdatabases d on d.sid = dp.sid

Related

how to get table's schema name

I'm using SQL Server 2008 and have the following query:
SELECT SO1.name AS Tab,
SC1.name AS Col,
SO2.name AS RefTab,
SC2.name AS RefCol,
FO.name AS FKName
FROM dbo.sysforeignkeys FK
INNER JOIN dbo.syscolumns SC1 ON FK.fkeyid = SC1.id AND FK.fkey = SC1.colid
INNER JOIN dbo.syscolumns SC2 ON FK.rkeyid = SC2.id AND FK.rkey = SC2.colid
INNER JOIN dbo.sysobjects SO1 ON FK.fkeyid = SO1.id
INNER JOIN dbo.sysobjects SO2 ON FK.rkeyid = SO2.id
INNER JOIN dbo.sysobjects FO ON FK.constid = FO.id
How do you retrieve the table's schema name?
Thanks for your help
Use OBJECT_SCHEMA_NAME
SELECT
OBJECT_SCHEMA_NAME(f.parent_object_id) AS TableNameSchema, -- this
OBJECT_NAME(f.parent_object_id) AS TableName,
COL_NAME(fc.parent_object_id,fc.parent_column_id) AS ColumnName,
OBJECT_SCHEMA_NAME(f.referenced_object_id) AS ReferenceTableNameSchema,
OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
COL_NAME(fc.referenced_object_id,fc.referenced_column_id) AS ReferenceColumnName,
f.name AS ForeignKey
FROM
sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc ON f.OBJECT_ID = fc.constraint_object_id
INNER JOIN sys.objects AS o ON o.OBJECT_ID = fc.referenced_object_id
As per Sql Server 2008:
SELECT Object_name(f.parent_object_id)
AS
TableName,
Col_name(fc.parent_object_id, fc.parent_column_id)
AS ColumnName,
Object_name (f.referenced_object_id)
AS ReferenceTableName,
Col_name(fc.referenced_object_id, fc.referenced_column_id)
AS
ReferenceColumnName,
f.name
AS ForeignKey,
Quotename(Schema_name(f.schema_id)) + '.' + Quotename(
Object_name(f.parent_object_id)) AS
schemaname,
Quotename(Schema_name(o.schema_id)) + '.' + Quotename(
Object_name(f.referenced_object_id))
AS ReferenceSchemaName
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id
INNER JOIN sys.objects AS o
ON o.OBJECT_ID = fc.referenced_object_id
If to want to know schema name on basis of object_id then use OBJECT_SCHEMA_NAME(), if you want to get schema name on basis of schema_id then use SCHEMA_NAME().

What is the SQL Server system table that contains information about stored procedure parameters?

What is the SQL Server system table that contains information about stored procedure parameters with it's information like datatype, name, lenght, null or not?
thanks
You can query sys.procedures and sys.parameters...
select pr.name, p.*
from sys.procedures pr
inner join sys.parameters p on pr.object_id = p.object_id
And join to types too...
select pr.name, p.*, t.name, t.max_length
from sys.procedures pr
inner join sys.parameters p on pr.object_id = p.object_id
inner join sys.types t on p.system_type_id = t.system_type_id
You can also use
select * from INFORMATION_SCHEMA.PARAMETERS
You can also use
select * from INFORMATION_SCHEMA.ROUTINES \G;
SELECT
p.name AS [SP Name], qs.execution_count,
ISNULL(qs.execution_count/DATEDIFF(Second, qs.cached_time, GETDATE()), 0) AS [Calls/Second],
qs.total_worker_time/qs.execution_count AS [AvgWorkerTime], qs.total_worker_time AS [TotalWorkerTime],
qs.total_elapsed_time, qs.total_elapsed_time/qs.execution_count AS [avg_elapsed_time],
qs.cached_time,
isr.ROUTINE_DEFINITION
FROM sys.procedures AS p
INNER JOIN sys.dm_exec_procedure_stats AS qs
ON p.[object_id] = qs.[object_id]
Inner Join INFORMATION_SCHEMA.ROUTINES as ISR
on p.name = isr.SPECIFIC_NAME
WHERE qs.database_id = DB_ID()
and
isr.ROUTINE_DEFINITION like '%XXXX%' -- name of something you are looking for in the stored procedure
ORDER BY qs.execution_count DESC OPTION (RECOMPILE);
For those using SQL 2000
SELECT * FROM [dbName]..sysobjects WHERE xtype = 'P'
and
SELECT * FROM [dbName]..sysComments
Join them by id
This shows information about stored procedures and user-defined functions. It will include procedures/scalar functions without parameters, but will include parameter information if available.
SELECT
O.Name ProcedureName,
P.parameter_id Position,
P.Name ParameterName,
P.max_length MaxLen,
P.is_output IsOutput,
P.has_default_value HasDefaultValue,
P.default_value DefaultValue,
P.is_nullable IsNullable,
T.name TypeName,
T.max_length TypeMaxLen,
O.type_desc TypeDescription
FROM SYS.OBJECTS O
LEFT JOIN SYS.PARAMETERS P ON O.object_id = P.object_id
LEFT JOIN SYS.TYPES T ON P.system_type_id = T.system_type_id
WHERE type_desc LIKE 'SQL_%'
ORDER BY O.Name, P.parameter_id, P.Name

How To Exclude System Table When Querying Sys.Tables?

I'm running this SQL :
SELECT S.name as Owner, T.name as TableName FROM sys.tables AS T
JOIN sys.schemas AS S ON S.schema_id = T.schema_id
And the result is:
Owner TableName
------------------------
dbo Person
dbo Customer
dbo sysdiagrams
sysdiagrams is a system table but showed in result.
Update:
Thanks all for your answers and comments, I'm using Nate Bolam & vmvadivel answers:
SELECT S.name as Owner, T.name as TableName
FROM
sys.tables AS T
INNER JOIN sys.schemas AS S ON S.schema_id = T.schema_id
LEFT JOIN sys.extended_properties AS EP ON EP.major_id = T.[object_id]
WHERE
T.is_ms_shipped = 0 AND
(EP.class_desc IS NULL OR (EP.class_desc <>'OBJECT_OR_COLUMN' AND
EP.[name] <> 'microsoft_database_tools_support'))
SSMS uses an extended property to mark the sysdiagrams table as a sort of pseudo system table.
Try this:
SELECT S.name as Owner, T.name as TableName FROM sys.tables AS T
INNER JOIN sys.schemas AS S ON S.schema_id = T.schema_id
LEFT JOIN sys.extended_properties AS EP ON EP.major_id = T.[object_id]
WHERE (EP.class_desc IS NULL
OR (EP.class_desc <> 'OBJECT_OR_COLUMN'
AND EP.[name] <> 'microsoft_database_tools_support'))
You can make use of IS_MS_SHIPPED for this. Hope the below script is what you are looking for
SELECT
S.[name] AS Owner,
T.[name] AS TableName
FROM
sys.tables AS T JOIN sys.schemas AS S
ON S.schema_id = T.schema_id
WHERE
T.is_ms_shipped = 0
AND T.[name] <> 'sysdiagrams'
Would you please try with below query, thanks
SELECT S.name as Owner, T.name as TableName FROM sys.tables AS T
JOIN sys.schemas AS S ON S.schema_id = T.schema_id
WHERE T.name <> 'sysdiagrams'
Just for completeness, I'll give a variation on Nate Bolam's answer
SELECT S.name as Owner, T.name as TableName
FROM sys.tables AS T
INNER JOIN sys.schemas AS S ON S.schema_id = T.schema_id
WHERE NOT EXISTS (SELECT * FROM sys.extended_properties EP WHERE EP.major_id = T.[object_id] AND EP.name = 'microsoft_database_tools_support')

List which columns have a full-text index in SQL Server 2005

How do I list all tables / columns in my database that have a full-text index?
select distinct
object_name(fic.[object_id]) table_name,
[name] column_name
from
sys.fulltext_index_columns fic
inner join sys.columns c
on c.[object_id] = fic.[object_id]
and c.[column_id] = fic.[column_id]
This one gives you more information
SELECT
t.name AS TableName,
c.name AS FTCatalogName ,
i.name AS UniqueIdxName,
cl.name AS ColumnName,
cdt.name AS DataTypeColumnName
FROM
sys.tables t
INNER JOIN
sys.fulltext_indexes fi
ON
t.[object_id] = fi.[object_id]
INNER JOIN
sys.fulltext_index_columns ic
ON
ic.[object_id] = t.[object_id]
INNER JOIN
sys.columns cl
ON
ic.column_id = cl.column_id
AND ic.[object_id] = cl.[object_id]
INNER JOIN
sys.fulltext_catalogs c
ON
fi.fulltext_catalog_id = c.fulltext_catalog_id
INNER JOIN
sys.indexes i
ON
fi.unique_index_id = i.index_id
AND fi.[object_id] = i.[object_id]
LEFT JOIN
sys.columns cdt
ON
ic.type_column_id = cdt.column_id
AND fi.object_id = cdt.object_id;

Finding a dependency of a SQL Server server data type

Is there a command, or a set of tables I can look at to determine which tables, stored procedures and views in SQL Server server 2005 have a certain user defined data type?
For tables and views:
Select *
From Information_Schema.Columns
Where DOMAIN_NAME = 'YourUserDefinedTypeName'
For procedures and functions:
Select *
From Information_Schema.PARAMETERS
Where USER_DEFINED_TYPE_NAME = 'YourUserDefinedTypeName'
Tables are relatively easy, sys.columns and sys.types allow you to link columns to types. The query below will get this out.
select s.name
,o.name
,c.name
,t.name
from sys.schemas s
join sys.objects o
on o.schema_id = s.schema_id
join sys.columns c
on c.object_id = o.object_id
join sys.types t
on c.user_type_id = t.user_type_id
where t.name = 'Foo'
EDIT: as G Mastros has shown above, you can get parameters with a similar query.
select s.name
,o.name
,p.name
,t.name
from sys.schemas s
join sys.objects o
on o.schema_id = s.schema_id
join sys.parameters p
on p.object_id = o.object_id
join sys.types t
on p.user_type_id = t.user_type_id
where t.name = 'Foo'

Resources