Determine Which Objects Reference a Table in SQL Server - sql-server

I need to rename a table that has many columns and stored procedures that process against that table. How can one get all Items in database that have a relation to a table in such a scenario?

Using sys.dm_sql_referencing_entities:
SELECT
referencing_schema_name, referencing_entity_name, referencing_id,
referencing_class_desc, is_caller_dependent
FROM
sys.dm_sql_referencing_entities ('mySchemaName.myTableName', 'OBJECT');
GO
where 'mySchemaName.myTableName' is your schema.table, for example 'dbo.MyTable'

If you need to find database objects (e.g. tables, columns, triggers) by name - have a look at the FREE Red-Gate tool called SQL Search which does this - it searches your entire database for any kind of string(s).
It's a great must-have tool for any DBA or database developer - did I already mention it's absolutely FREE to use for any kind of use??

if you want of ref of DB item like table, column, procedure, etc..
You can use visual-expert tools, we can analyse the code SqlServer code
more info: https://www.visual-expert.com/EN/visual-expert-documentation/code-cross-references/

Related

SQL - How to check if altering a table will break any stored procedures

Is there a nice way before I alter a table (e.g. remove a column), to see if that this will break any stored procedures?
I am trying to do this in MS SQL Server
Use the query here to search all stored procedures for the table and column name. You will probably still want to look at the code for each one you find to verify that it will or won't break.
you can use the following query to search for the table name in any stored procedures:
SELECT name
FROM sys.procedures
WHERE Object_definition(object_id) LIKE '%Your_Table_Name%'
I suggest you:
Make sure you have a separate environment (DEV)
Use the sample code from here to create a proc that confirms all objects in the database can be recompiled
How to Check all stored procedure is ok in sql server?
Use it - I can guarantee you will already have failing objects before you remove your column
Remove your column and use it again to see if more things broke
The more mature approach to this is to put your database into a database project and build that. But you can't do this until your database is valid.

Copy Data from a table in one Database to another database

Basically I have a two databases on SQL Developer. I want to take the table data FOR A PARTICULAR RECORD from one database and copy it to another database's table. What should be the query? I don't want to use a restore to avoid data loss... Any ideas?
I got a query from google:
INSERT INTO dbo.ELLIPSE_PFPI.T_ANTENNE
(COLUMNS)
SELECT COLUMNS_IN_SAME_ORDER FROM dbo.ELLIPSE_PFPI.T_ANTENNE
What should be written in the query instead of dbo?
Try this
I havent tested it but i think it works
select * into [databaseTo].dbo.tablename from [databaseFrom].dbo.tablename

postgresql-simple: get a list of table names in a database

I am connected with postgresql-simple and need to get a list of tables in the current db.
psql has \d
haskelldb has 'tables', which yields the dbTables attribute in the connection object (i think that's what it was called)
HDBC has getTables
what is the way to do this in postgresql-simple?
thanks,
m.
as already answered in a comment above (due to lack of privileges): don't use postgresql-simple at all, but plain SQL on the internal postgresql tables:
select schemaname, tablename from pg_tables;
sorry for the noise.

Finding all views that are using linked server

I am updating url for linked servers. Before make the changes, I would like to know all views that have reference to this linked servers. Is there any programmatic way (TSQL) to perform this task?
Thanks for your help.
I am using SQL Server 2005, 2008 and 2012. The database servers that referencing linked servers are mostly SQL Server 2005
While it may return false positives, and won't capture any cases where a four-part name is constructed using dynamic SQL, this is probably the simplest approach:
SELECT name FROM sys.views
WHERE LOWER(OBJECT_DEFINITION([object_id])) LIKE LOWER('%LinkedServerName%');
This will find the views:
SELECT t2.name, OBJECT_DEFINITION(t1.[object_id]) view_definition
FROM sys.views t1 join sys.servers t2 on
OBJECT_DEFINITION(t1.[object_id]) like '%['+ t2.name + '].%' ESCAPE '['
It can fail if a table, view, schema or database has same name as a linked server.
In case some views have eluded the first check you can add this line this part is not checking for the square brackets surrounding the linked server name. But be aware that this part is more likely to include
extra unwanted views
or OBJECT_DEFINITION(t1.[object_id]) like '% '+ t2.name + '.%'
EDIT: Changed sys.sysservers to sys.servers. Thanks Aaron Bertrand
If you need to find database objects (e.g. tables, columns, triggers) by name - have a look at the FREE Red-Gate tool called SQL Search which does this - it searches your entire database for any kind of string(s).
It's a great must-have tool for any DBA or database developer - did I already mention it's absolutely FREE to use for any kind of use??
Try This:
SELECT name, OBJECT_DEFINITION([object_id]) FROM sys.views
where OBJECT_DEFINITION([object_id]) like '%.%.dbo.%'

Mass change datatype and rename of dependent store procedure variables

I am in the process of optimising my database and I was thinking of changing the datatype for some columns from DATETIME to SMALLDATETIME on my tables.
Is there a system stored procedure that returns both the contents/code of a store procedure and the dependent table which will then allow me to do a join on a filtered list of tables?
Cheers!
EDIT1:
Im looking to programatically rename the stored procedures not track dependencies!
The built-in dependency tracking for SQL isn't very good for this type of work. Two tools come to mind thought...
Red Gate SQL Dependency Tracker - Good for determining all the dependent code
Visual Studio for Database Developers - Contains TSQL Code Analysis which can identify if a piece of data is being treated as an incorrect type.
Red Gate has a free trial on their stuff, which might get you through this job
I answered a simliar question to this (link below) with a sample of a scipt I use to find text in stored procedures (and functions and views). It requires a bit of work, but might help you here.
[How to find data table column reference in stored procedures
[1]: http://How to find data table column reference in stored procedures
If your dependencies in SQL Server are accurate, you can use sys.sql_dependencies with appropriate joins.

Resources