且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

以编程方式检索与 SQL Server Management Studio gui 返回的源相同的 SQL Server 存储过程源?

更新时间:2023-01-30 11:48:47

EXEC sp_helptext 'your procedure name';

这避免了 INFORMATION_SCHEMA 方法的问题,如果存储过程太长,它就会被切断.

This avoids the problem with INFORMATION_SCHEMA approach wherein the stored procedure gets cut off if it is too long.

更新:David 写道,这与他的 sproc 不同……也许是因为它将行作为记录"返回以保留格式?如果您想以更自然"的格式查看结果,您可以先使用 Ctrl-T(输出为文本),它应该完全按照您输入的方式打印出来.如果您在代码中执行此操作,那么执行 foreach 以完全相同的方式组合您的结果是微不足道的.

Update: David writes that this isn't identical to his sproc...perhaps because it returns the lines as 'records' to preserve formatting? If you want to see the results in a more 'natural' format, you can use Ctrl-T first (output as text) and it should print it out exactly as you've entered it. If you are doing this in code, it is trivial to do a foreach to put together your results in exactly the same way.

更新 2:这将为源提供CREATE PROCEDURE"而不是ALTER PROCEDURE",但我知道没有办法让它改用ALTER".不过,这是一件微不足道的事情,不是吗?

Update 2: This will provide the source with a "CREATE PROCEDURE" rather than an "ALTER PROCEDURE" but I know of no way to make it use "ALTER" instead. Kind of a trivial thing, though, isn't it?

更新 3:有关如何在源代码控制系统中维护 SQL DDL(数据库结构)的更多见解,请参阅评论.这才是这个问题的关键.

Update 3: See the comments for some more insight on how to maintain your SQL DDL (database structure) in a source control system. That is really the key to this question.