如何检查sql server中的所有存储过程是否正常?
如果我删除表或字段,如何检查 sql server 中的所有存储过程是否正常?
How to check all stored procedure is ok in sql server if I drop a table or fields?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我发现凯德的答案对于制定我自己的脚本来检查数据库中的对象很有用,所以我想我也应该分享我的脚本:
I found Cade's answer useful in formulating my own script for checking objects in a database, so I thought I'd share my script as well:
它不会捕获所有内容(动态 SQL 或后期绑定对象),但它可能很有用 - 对所有非架构绑定存储过程调用 sp_refreshsqlmodule (您可以先调用它以确保更新依赖项,然后查询依赖项,或调用然后查看是否有任何损坏):
It won't catch everything (dynamic SQL or latebound objects), but it can be useful - call sp_refreshsqlmodule on all non-schema bound stored procedures (you can call it before to ensure that dependencies are updated and then query the dependencies, or call it afterwards and see if anything is broken):
我基本上做了同样的事情,但是将其编写为无游标,速度非常快。
I basically did the same thing, but wrote it to be CURSORless which is super fast.
除了 Michael Petito 的脚本之外,您还可以检查 SP(延迟名称解析)中的后期绑定对象的问题,如下所示:
In addition to the script from Michael Petito you can check for issues with late-bound objects in SPs (deferred name resolution) like this:
一旦我对表进行了更改(例如列重命名),我就必须更改引用该表列的所有存储过程、函数和视图。显然我必须手动将它们一一更改。但我的数据库包含数百个这样的对象。所以我想确保我已经改变了所有依赖的对象。一种解决方案是重新编译所有对象(通过脚本)。但重新编译仅发生在每个对象的下一次执行时。但我想要的是验证它们并立即获取详细信息。
为此,我可以使用“sp_refreshsqlmodule”而不是“sp_recompile”。这将刷新每个对象,如果解析不正确,则会抛出错误。
这是下面的脚本;
如果任何对象抛出错误,我现在可以处理它并手动修复问题。
Once I made change to a table such as column rename, I have to alter all the stored procedures, functions and views that refer the table column. Obviously I have to manually alter them one by one. But my database contains hundreds of objects like these. So I wanted to make sure I have altered all the depending objects. One solution is to recompile all the objects (via a script). But recompilation happens on each object’s next execution only. But what I want is to validate them and get the details now.
For that I can use “sp_refreshsqlmodule” instead of “sp_recompile”. This will refresh each object and throws an error if its not parsing correctly.
Here is the script below;
If any object throws an error I can now attend to it and manually fix the issue with it.
想到的几种方法
Couple of ways that come to mind
相同的想法,但更通用 - 您使用主体检查所有用户定义的对象
它会显示编译过程中的错误。这在重命名/删除对象/列等之后非常有用
只需在数据库架构更新后运行它以确保所有主体对象仍然有效
Same idea, but more universal - you check all user defined objects with bodies
And it shows you error during compiling. This is really useful after renaming/removing objects/columns etc
Just run it after database schema update to make sure that all body objects still valid
给出的答案都无法找到因重命名或删除表而导致的错误
但请高兴,我在 SQL Server 2017 及更高版本上有一个解决方案:
None of the answers given can find the error resulting from renaming or dropping a table
but be happy, I have a solution on SQL Server 2017 and higher versions:
我尝试了“Cade Roux”答案,它出错了,我将其修复如下
I tried "Cade Roux" Answer , it went wrong and I fixed it as following
我的方法有点不同。我已经在 SSMS 中为一堆进程创建了更改脚本,然后等待了几秒钟,以便 SSMS 处理它们,我得到了我想要的:
然后 SSMS 右边框为任意行添加一个红点错误,我可以轻松检查、更正该错误,然后执行相同的脚本以使用正确的值进行更新。
My approach was a little bit different. I've created alter script for a bunch of procs in SSMS and then waited for few seconds so SSMS process them and Ive got what I wanted:
O then SSMS right border a red dot for any line in error, which I can easily check, correct and later execute same script to update with correct values.