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

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

Related

Retrieve definition of Sequence object in SQL Server

How can I get the definition of any Sequence objects in SQL Server? For instance if I want to get the definition of View/Function/Procedure I would use below query
SELECT OBJECT_DEFINITION(tab.OBJECT_ID)
FROM SYS.OBJECTS tab
WHERE tab.[type] = 'V' /*FOR VIEW*/
SELECT OBJECT_DEFINITION(tab.OBJECT_ID)
FROM SYS.OBJECTS tab
WHERE tab.[type] = 'P' /*FOR PROCEDURE*/
SELECT OBJECT_DEFINITION(tab.OBJECT_ID)
FROM SYS.OBJECTS tab
WHERE tab.[type] = 'TR' /*FOR TRIGGER*/
Please let me know if we have similar options available to get the details for Sequence objects
A SEQUENCE doesn't have the same type of definition as an object like a VIEW or PROCEDURE, however, you could generate your own:
CREATE SEQUENCE dbo.YourSEQUENCE
START WITH 7
INCREMENT BY 4;
GO
SELECT NEXT VALUE FOR dbo.YourSEQUENCE;
GO
SELECT *
FROM sys.sequences
GO
SELECT CONCAT(N'CREATE SEQUENCE ' + QUOTENAME(s.[name]) + N',' + QUOTENAME(sq.[name]),NCHAR(13) + NCHAR(10),
N' START WITH ',CONVERT(int,sq.start_value), NCHAR(13) + NCHAR(10),
N' INCREMENT BY ',CONVERT(int,sq.increment),N';')
FROM sys.schemas s
JOIN sys.sequences sq ON s.schema_id = sq.schema_id
WHERE s.[name] = N'dbo'
AND sq.[name] = N'yourSEQUENCE';
GO
DROP SEQUENCE dbo.YourSEQUENCE;
If this is so you have a repository of all your definitions, that should already be in your Source Control Software.
Your Above Query is right.....
i.e.'V' -- FOR VIEW
'P' -- FOR PROCEDURE
'TR' -- FOR TRIGGER
SELECT sm.object_id, OBJECT_NAME(sm.object_id) AS object_name, o.type, o.type_desc, sm.definition
FROM sys.sql_modules AS sm
JOIN sys.objects AS o ON sm.object_id = o.object_id
ORDER BY o.type;
Use this Query...you will get all the data in single set just refer type Column Name.
Objects of type P, RF, V, TR, FN, IF, TF, and R have an associated SQL
module.
The SQL Server Database Engine assumes that object_id is in the current database context.
The collation of the object definition always matches that of the calling database context.
OBJECT_DEFINITION applies to the following object types:
C = Check constraint
D = Default (constraint or stand-alone)
P = SQL stored procedure
FN = SQL scalar function
R = Rule
RF = Replication filter procedure
TR = SQL trigger (schema-scoped DML trigger, or DDL trigger at either the database or server scope)
IF = SQL inline table-valued function
TF = SQL table-valued function
V = View
For better info ...use this link...
https://learn.microsoft.com/en-us/sql/relational-databases/system-catalog-views/sys-sql-modules-transact-sql?view=sql-server-ver15

Collation Conflict in SSRS

Currently using SQL Server 2014 and having an issue with the Collation on a stored procedure. I currently have a report that runs a stored procedure, the code works fine when run in SSMS but I have noticed that while it is being run as part of a SSRS report there is a collation issue between Latin1_General_CI_AS_KS_WS and Latin1_General_CI_AS.
My database is set at Latin1_General_CI_AS as well as all my table columns so I am at a loss as to where it is pulling this Latin1_General_CI_AS_KS_WS inconsistency, can anyone offer any suggestions?
Thanks
Latin1_General_CI_AS_KS_WS is the default collation for SSRS.
Either you write your SQL queries with the COLLATE property on all your string comparisons or reinstall SSRS with the correct default collation.
Keep in mind that if you use ALTER DATABASE to change the SSRS database collation, it won't affect already created objects, just new ones from that point onwards.
If this is a problem with a particular column (and not the whole database), you can run this query to check all column's current collation.
SELECT
ColumnName = QUOTENAME(s.name) + '.' + QUOTENAME(t.name) + '.' + QUOTENAME(c.name),
Collation = c.collation_name
FROM
sys.schemas s
INNER JOIN sys.tables t ON t.schema_id = s.schema_id
INNER JOIN sys.columns c ON c.object_id = t.object_id
WHERE
C.collation_name is not null
ORDER BY
1
Then use ALTER TABLE <tablename> ALTER COLUMN <columnname> <type> COLLATE <newCollate> to change it.

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

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

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