在sql server中,有什么方法可以检查架构更改是否会影响存储过程?
在 SQL Server 中,有什么方法可以检查架构中的更改是否会影响存储过程(和/或视图)?
例如,更改一个表中的列名可能会破坏某些存储过程;如何检查受影响的存储过程?
In SQL Server, is there any way to check whether the changes in the schema will impact Stored Procedures (and/or Views)?
For example a change of the column name in one table, may break some Stored Procedures; how to check the impacted stored procs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
尝试使用:
和/或
try using:
and/or
使用 Visual Studio Database Edition 进行 T-SQL 开发。它会在构建过程中捕获此类问题,因为它会创建部署 .dbschema 文件。
Use Visual Studio Database Edition for your T-SQL development. It will catch such problems during build, as it creates the deployment .dbschema file.
在 SSMS (SQL Server Management Studio) 中,右键单击要更改的对象,然后单击“查看依赖关系”。我认为这不会从另一个数据库找到引用。
您还可以在存储过程中查找引用(如果它们未加密)。您必须在您怀疑可能引用您正在更改的对象的每个数据库中执行此操作。
选择对象.名称
,sql_modules.definition
来自 sys.sql_modules sql_modules
在 sql_modules.object_id =objects.object_id 上加入 sys.objects 对象
其中定义如“%some column name%”;
我没有发现任何东西在 100.000000% 的时间都是 100.0000% 准确的。
In SSMS (SQL Server Management Studio) right click on the object you are changing and click on View Dependencies. I don't think this will find references from another database.
You can also look for references in stored procedures if they are not encrypted. You would have to do this in each database you suspect might reference the object you are changing.
select objects.name
,sql_modules.definition
from sys.sql_modules sql_modules
join sys.objects objects on sql_modules.object_id = objects.object_id
where definition like '%some column name%';
I have found nothing that is 100.0000% accurate 100.000000% of the time.
我认为做到这一点的最好方法是使用视图从实际表中抽象存储过程,并使用“WITH SCHEMABINDING”子句创建这些视图,这应该防止会破坏视图的更改...
Best way I can think to do this is to abstract your stored procedures from your actual tables using views, and to create those views with a "WITH SCHEMABINDING" clause which should prevent changes that will break your views...
商业工具,例如Red Gate 的 SQL Refactor 可以做到这一点。
我认为最近版本的Visual Studio也包含这种功能,但我没有尝试过。
据我所知,Microsoft SQL Server 本身没有内置功能可以执行此操作。 更正:我刚刚读到sp_depends 在 KM 对这篇文章的回答中...请注意,sp_depends 的用法已被弃用;它被 sys.dm_sql_referencing_entities 和 sys.dm_sql_referenced_entities 替换。
此外,如果底层存储过程使用动态 SQL,检测依赖关系的任务变得更加困难并且容易“遗漏”。
Commercial tools such as Red Gate's SQL Refactor can do this.
I think that recent version of Visual Studio also include this kind of features, but I haven't tried.
To my knowledge, there are no built-in features of Microsoft SQL Server per-se which will do this. Correction: I just read about sp_depends in KM's answer to this post... Note that sp_depends's usage is deprecated; it is replaced by sys.dm_sql_referencing_entities and sys.dm_sql_referenced_entities
Also, if the underlying stored procedures use dynamic SQL, the task of detecting dependencies becomes more difficult and prone to "misses".
如果您想更改对象或列的名称,则可以使用 Red Gate Software SQL Prompt 5 将生成一个脚本,该脚本既执行重命名,又更新对其他对象中旧名称的引用。
如果您只是对依赖于列名的内容感兴趣,那么 SQL Prompt 5 还具有列依赖项功能,将鼠标悬停在脚本中的列名上会弹出一个包含对象列表的窗口指的是该列。
您可以免费下载 14 天试用版,看看这些功能是否适合您。
保罗·史蒂芬森
SQL 提示项目管理器
红门软件
If you want to change the name of an object or column, then the Smart Rename feature of Red Gate Software's SQL Prompt 5 will generate a script that both performs the rename and updates references to the old name in other objects.
If you're just interested in what depends on a column name, then SQL Prompt 5 also has a Column Dependencies function, where hovering over the column name in a script pops up a window containing a list of objects that refer to the column.
You can download a 14-day trial for free, to see if either of these features works for you.
Paul Stephenson
SQL Prompt Project Manager
Red Gate Software
看看这些答案:
刷新用户函数 t-SQL 上的元数据
SQL Server 关系隐藏在存储过程而不是架构中< /a>
在 SQL Server 中,如何我可以找到引用列的所有地方吗?
如何查找所有插入、更新或删除记录的存储过程?
除了动态 SQL 之外,尽可能使用 SCHEMABINDING 以及 sp_refreshsqlmodule 和 sql_dependency 对于其他所有内容都非常准确。
Have a look at these answers:
Refreshing metadata on user functions t-SQL
SQL Server relationships buried in stored procedures rather than schema
In SQL Server, how can I find everywhere a column is referenced?
How do I find all stored procedures that insert, update, or delete records?
Other than dynamic SQL, using SCHEMABINDING where possible and sp_refreshsqlmodule and sql_dependencies for everything else is very accurate.
如果您使用 SQL Server
您可以在更改后使用此查询并找到存储过程或视图或...
更改后可能会出现错误
If you use SQL Server
You can use this query after your change and find Stored Procedure Or View Or ...
that after your change might get error