how to find the list of stored procedures with public execute permission - sql

I have a db with more than 1000s of stored procedures.
Need a query to find the list of stored procedures which has public execute permission.
Query I tried which didn't give me clear output.
select name,
has_perms_by_name(name, 'OBJECT', 'EXECUTE') as has_execute,
has_perms_by_name(name, 'OBJECT', 'VIEW DEFINITION') as has_view_definition
from sys.procedures

Not sure why you got voted down, seems like a reasonable question to me.
You'll need something along the lines of:
DECLARE #principal_id int
SELECT #principal_id = principal_id
FROM sys.database_principals
WHERE name = 'public'
SELECT o.name,
dp.permission_name,
dp.state_desc
FROM sys.objects o
JOIN sys.database_permissions dp
ON dp.class = 1
AND dp.major_id = o.object_id
AND dp.grantee_principal_id = #principal_id
AND dp.type = 'EX' -- execute
AND dp.state IN ('G', 'W')
WHERE o.type = 'P' -- stored procedures

Related

tSQLt How can I tell if a table has been faked

I am just starting creating some unit tests for my database.
If I have faked a table,
EXEC tSQLt.FakeTable
#TableName = 'dbo.[My Table]',
#Identity = 0,
#ComputedColumns = 0,
#Defaults = 0
Can I check if it has been faked?
Note that documentation on the FakeTable SP can be found here.
Motivation
I want to be able to do this as I imagine creating several stored procedures which populate these faked tables so I can perform tests.
However I do not want to handle faking the tables in the stored procedures (so I can call them multiple times entering different info each time).
I don't want to have the possibility that I forget to fake the table before adding the data (as would almost certainly cause me to fail my test).
tSQLt adds an extended property to a fake table to track the table it fakes. This is easily tested using the function tSQLt.Private_GetOriginalTableName:
SELECT tSQLt.Private_GetOriginalTableName('dbo','[My Table]')
This will return NULL if the table isn't faked.
If you want to do something more complex, you can query sys.extended_properties directly. See the contents of the tSQLt.class.sql script (in the tSQLt distribution) for the definition of tSQLt.Private_GetOriginalTableName.
You can check for the existence of the table at the beginning of your stored procedure(s).
If Not Exists ( Select 1 From Sys.Objects Where [Name] = 'YOURTABLENAME' And [Type] = 'U')
Begin
-- Your Create Table Statement Here
ENd
Based on your comments, the tool has to be doing something like this, using schema:
Create table dbo.MisterPositive ( test int )
Create table developers.MisterPositive (test Int )
-- Both statements below work
Select * From dbo.MisterPositive
Select * From developers.MisterPositive
-- Use this to look for existence prior
Select 1 from sys.objects
Inner join sys.schemas ON sys.objects.schema_id = sys.schemas.schema_id
where sys.objects.[Name] = 'MisterPositive' And sys.schemas.name = 'dbo'
Select 1 from sys.objects
Inner join sys.schemas ON sys.objects.schema_id = sys.schemas.schema_id
where sys.objects.[Name] = 'MisterPositive' And sys.schemas.name = 'Developers'
So yours would be
If Not Exists ( Select 1 from sys.objects
Inner join sys.schemas ON sys.objects.schema_id = sys.schemas.schema_id
where sys.objects.[Name] = 'YOURTABLE' And sys.schemas.[Name] = 'tSQLt' )
Begin
-- create table here
End

How to find all stored procedures and table from a single stored procedure?

I am working on a database which has does a lot of stored procedures and tables.
One brute force way to know all the stored procedures and tables called by a single stored procedure is by scanning and reading through the code. However, it takes a lot of time.
what is the query to find out all the stored procedures and tables that are called by a main stored procedure?
You can query sys.objects and sys.sql_expression_dependencies tables like this:
SELECT sys.objects.name, sys.objects.type_desc
FROM sys.sql_expression_dependencies
INNER JOIN sys.objects ON(referenced_id = object_id)
WHERE referencing_id = OBJECT_ID('<Your stored procedure name here>')
Don't forget to replace <Your stored procedure name here> with the actual name of the stored procedure.
Update:
A stored procedure called from another stored procedure will still have a row in sys.sql_expression_dependencies, but will have a null value in the referenced_id column. you can use something like this:
DECLARE #StoredProcedureName sysname = '<Your stored procedure name here>'
SELECT sys.objects.name, sys.objects.type_desc
FROM sys.sql_expression_dependencies
INNER JOIN sys.objects ON(referenced_id = object_id)
WHERE referencing_id = OBJECT_ID(#StoredProcedureName)
UNION ALL
SELECT procedures.name , 'Stored Procedure' As type_desc
FROM sys.sql_expression_dependencies
INNER JOIN sys.procedures ON(referenced_entity_name = sys.procedures.name)
WHERE referencing_id = OBJECT_ID(#StoredProcedureName)
AND referenced_id IS NULL

Query to get the information of Databases used by Stored Procedure in SQL server 2005

Is there any query in SQL server 2005 that returns the list of Stored procedures in the particular database along with the name of databases whose objects are getting used in the stored procedure.
That is how to get all procedure names:
select *
from DatabaseName.information_schema.routines
where routine_type = 'PROCEDURE'
I will check now, if there is any way to check their code for table names.
you can use this query
it will show all dependencies even to the columns
SELECT
--SP, View, or Function
ReferencingName = o.name,
ReferencingType = o.type_desc,
--Referenced Field
ref.referenced_database_name, --will be null if the DB is not explicitly called out
ref.referenced_schema_name, --will be null or blank if the DB is not explicitly called out
ref.referenced_entity_name,
ref.referenced_minor_name
FROM sys.objects AS o
cross apply sys.dm_sql_referenced_entities('dbo.' + o.name, 'Object') ref
where o.type = 'p'
-- for other database object types use below line
-- o.type in ('FN','IF','V','P','TF')
works for single database
select *
from information_schema.routines
where routine_type = 'PROCEDURE'
This is not a simple thing to do reliably in SQL Server 2005. You might want to look at commercial products such as ApexSQL Clean or SQL Dependency Tracker.
In SQL Server 2008 you could try using the sys.sql_expression_dependencies dynamic management view. For example,
select
quotename(s.name) + N'.' + quotename(o.name) as ProcedureName,
ed.referenced_server_name,
ed.referenced_database_name,
ed.referenced_schema_name,
ed.referenced_entity_name
from sys.sql_expression_dependencies ed
inner join sys.objects o on o.object_id = ed.referencing_id
inner join sys.schemas s on s.schema_id = o.schema_id
where
o.type = 'P'
Hope this helps,
Rhys

Find and replace content in stored procedures ms sql server

I want to rename tables and views which are used in stored procedures. Is there any way to find and replace table names in stored procedures, maybe there is tool for ms sql server (i'm using ms sql server 2012).
SQL Server might not allow you to directly UPDATE the object definitions (Views and Stored Proceduress in your case) present in the System catalogs even after setting the 'Allow Updates' option to 1.
The following code will generate the required ALTER Script and you can run them manually after reviewing the definitions ([ModifiedDefinition] )or u can loop through each value of [ModifiedDefinition] and run it using sp_executesql.
SELECT
b.Name AS [ObjectName],
CASE WHEN b.type ='p' THEN 'Stored Procedure'
WHEN b.type ='v' THEN 'View'
ELSE b.TYPE
END AS [ObjectType]
,a.definition AS [Definition]
,Replace ((REPLACE(definition,'OLD Value','New Value')),'Create','ALTER') AS [ModifiedDefinition]
FROM sys.sql_modules a
JOIN
( select type, name,object_id
from sys.objects
where type in (
'p' -- procedures
,'v'--views
)
and is_ms_shipped = 0
)b
ON a.object_id=b.object_id
And as always, be careful with production data and take backups before performing bulk changes on object definitions!!
You can use DBvisualizer .. it pretty much works with all databases and with ms sql too, you can do all you mentioned by using this.
I answered this on another topic (https://stackoverflow.com/a/67728039/11165834) , I do it using the following script:
DECLARE #queryDef NVARCHAR(max)
WHILE EXISTS (
SELECT 1
FROM sys.sql_modules sm
JOIN sys.objects o ON sm.object_id = o.object_id
WHERE sm.definition LIKE '%TEXT_TO_REPLACE%'
AND o.type = 'V'
)
BEGIN
-- TO ALTER THE VIEW AUTOMATICALLY
SET #queryDef = ( SELECT TOP 1 Replace (Replace (sm.definition, 'CREATE VIEW', 'ALTER VIEW'),
'TEXT_TO_REPLACE',
'NEW_TEXT')
FROM sys.sql_modules sm
JOIN sys.objects o ON sm.object_id = o.object_id
WHERE sm.definition LIKE '%TEXT_TO_REPLACE%'
AND o.type = 'V')
EXEC (#queryDef)
END
I use it to replace procedures/views when I restore a backup from production into tests databases.
As #S.A said, be verry careful because is not a verry safe way.
Change the "o.type" and "Replace (sm.definition, 'CREATE VIEW', 'ALTER VIEW'" accordingly to your need

How can I get the 'External name' of a SQL CLR trigger?

I have created a SQL CLR trigger with the follow SQL:
GO
CREATE TRIGGER AuditAccountsTable
ON [dbo].[Accounts]
FOR INSERT,DELETE,UPDATE
AS
EXTERNAL NAME namespace.Triggers.AuditTrigger
I am trying to query:
select * from sys.triggers
Is there a way to find the: EXTERNAL NAME namespace.Triggers.AuditTrigger on the trigger from querying in the DB?
I can't be sure as I don't have a place to test this, but does the text column returned below get you close to what you're looking for?
select t.name, c.text
from sys.triggers t
inner join sys.syscomments c
on t.object_id = c.id
where t.type_desc = 'CLR_TRIGGER'
Unlike T-SQL "modules" such as Stored Procedures and Functions, the SQLCLR T-SQL wrapper objects do not have their CREATE statements stored in the database. This is why you cannot access them via sys.sql_modules, OBJECT_DEFINITION, or the deprecated-since-SQL-Server-2005-and-should-not-be-used sys.syscomments. This is why SQLCLR Stored Procedures and Functions need to have their parameter default values stored in sys.parameters
Instead, CREATE statements for SQLCLR T-SQL wrapper objects are inferred from meta-data, just like Indexes, Primary Keys, Foreign Keys, etc.
You can get all of the parts of the CREATE TRIGGER statement from the following query:
SELECT OBJECT_SCHEMA_NAME(st.[object_id]) AS [SchemaName],
st.[name] AS [TriggerName],
OBJECT_SCHEMA_NAME(st.parent_id) AS [ParentSchemaName],
OBJECT_NAME(st.parent_id) AS [ParentName],
st.is_instead_of_trigger,
SUBSTRING((
SELECT N', ' + ste.[type_desc]
FROM sys.trigger_events ste
WHERE ste.[object_id] = st.[object_id]
FOR XML PATH ('')
), 3, 500) AS [Actions],
QUOTENAME(sa.name) AS [AssemblyName],
QUOTENAME(sam.assembly_class) AS [AssemblyClass],
QUOTENAME(sam.assembly_method) AS [AssemblyMethod]
FROM sys.triggers st
INNER JOIN sys.assembly_modules sam
ON sam.[object_id] = st.[object_id]
INNER JOIN sys.assemblies sa
ON sa.[assembly_id] = sam.[assembly_id]
WHERE st.parent_class = 1; --- OBJECT_OR_COLUMN