如何在 SQL 2000 中删除存储过程 + SQL 2005兼容方式?
我有一个项目要求我在 SQL Server 2005 中进行开发,但要部署到 SQL Server 2000 机器。
对于 99% 的 SQL 代码,我没有任何问题,一切似乎都是向后兼容的。
现在我正要开始将所有存储过程 (SP) 添加到源代码管理中,我喜欢每次执行查询时执行删除添加的想法。 IE 如果 SP 已存在,请先删除它。然后创建/重新创建 SP。
如何在单个脚本中以与 SQL 2000 和 SQL 2005 兼容的方式执行此操作,以便我的脚本只能在开发 (2000) 和生产 (2005) 期间工作?我相信语法略有不同,并且 SP 元数据存储在不同的系统表中。
请协助编写有效的 SQL 脚本。
I have a project that requires me to do development in SQL Server 2005, but do deployments to a SQL Server 2000 box.
For 99% of the SQL code, I have no problems, everything appears to be backwards compatible.
Now I am just about to start adding all the Stored Procedures (SPs) to source control, and I like the idea of doing a drop-add each time the query is executed. I.E. If the SP already exists, first drop it. Then create/re-create the SP.
How do I do this in a single script, in a manner that is compatible with both SQL 2000 and SQL 2005, so that my scripts will just work during Development (2000) AND Production (2005)? I believe the syntax is slightly different, and the SP metadata is stored in different system tables.
Please assist with a working SQL script.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这适用于 SQL 2000 和 SQL 2005。我现在已经测试过它。
This works for both SQL 2000 and SQL 2005. I have tested it right now.
不要使用系统表:使用 OBJECT_ID
我也会使用 ALTER 进行部署,但使用 CREATE 维护源代码控制。也就是说,我只使用差异部署脚本(使用 ALTER),但与发布后的源代码管理文件夹(作为 CREATE)相比,
我既有代码历史记录,也有更简单的部署:无需删除/创建所有进程。例如,如果您忘记了许可怎么办?
顺便说一句,我使用 Red Gate/SVN
Don't use system tables: use OBJECT_ID
I would also deploy using ALTER but maintain source control using CREATE. That is, I only ever use differential deployment scripts (with ALTER) but compare to my source control folder after release (which as CREATE)
I have both code history and simpler deployments: there is no need to drop/create all procs. What if you forget a permission for example?
I use Red Gate/SVN BTW
我认为
会告诉你它是否存在,尽管我目前无法在 2000 上进行测试...
I think
will tell you if it is there, although I can't test on 2000 at the mo...
FWIW
在 SQL 2008 中仍然有效,所以我猜测这仍然是可以接受的最低公分母。 2000 年还没有 DMV。
FWIW
still works in SQL 2008, so am guessing that this is still acceptable as the lowest common denominator. DMV's weren't available in 2000.
您最好的选择仍然是兼容性视图、
sysobects
、syscolumns
等,请查看以下链接
http://msdn.microsoft.com/en-us/library/ms187376。 ASPX
You best option is staill the compatibility views,
sysobects
,syscolumns
, etcCheck out the following link
http://msdn.microsoft.com/en-us/library/ms187376.aspx
在我看来,您重新创建了关于 sys.sp_refreshsqlmodule 的所有存储过程,就像我的旧答案中描述的那样 我正在寻找一种可靠的方法来验证T-SQL存储过程。有人有吗?。存储过程的代码将被再次验证是否包含依赖项。
It seems to me that you recreate all STORED PROCEDUREs with respect of
sys.sp_refreshsqlmodule
like if is described in my old answer I'm looking for a reliable way to verify T-SQL stored procedures. Anybody got one?. The code of STORED PROCEDUREs will be one more time verified inclusive off dependencies.使用 INFORMATION_SCHEMA.ROUTINES 视图应该适用于 SQL Server 2000、2005和 2008 年。唯一的缺点是视图不再是确定对象模式的可行方法。
但如果这不是问题,请尝试这样的脚本:
Using the INFORMATION_SCHEMA.ROUTINES view should work in SQL Server 2000, 2005, and 2008. The only downside is that the view is no longer a viable means of determining the object's schema.
But if that is not a concern, try a script like this:
在大多数情况下,我会尝试在 2005 机器上运行 SQL2000 TSQL,因为我希望它在很大程度上向后兼容。也就是说,您应该完成生产设备的升级,以便可以使用更新的 TSQL。
如果您无法找到版本之间的兼容性,您可以首先检测版本。
要确定正在运行的 SQL Server 2000/2005 版本,请使用查询分析器连接到 SQL Server 2000/2005,然后运行以下代码:
结果为:
产品版本(例如 8.00.534)。
产品级别(例如“RTM”或“SP2”)。
版本(例如“标准版”)。
例如,结果类似于:
8.00.534 RTM 标准版
来源:http://blog.sqlauthority.com/2007/03/07/sql-server-script-to-define-which-version -of-sql-server-2000-2005-is-running/
一旦确定了版本,就可以执行适当级别的代码。
In most cases, I'd try to run SQL2000 TSQL on the 2005 box, as I'd expect it to be largely backward-compatible. That said, you ought to finish upgrading your production box so you can use newer TSQL.
In cases where you can't find compatibility between the versions, you could first detect the version.
To determine which version of SQL Server 2000/2005 is running, connect to SQL Server 2000/2005 by using Query Analyzer, and then run the following code:
The results are:
The product version (for example, 8.00.534).
The product level (for example, “RTM” or “SP2″).
The edition (for example, “Standard Edition”).
For example, the result looks similar to:
8.00.534 RTM Standard Edition
Source: http://blog.sqlauthority.com/2007/03/07/sql-server-script-to-determine-which-version-of-sql-server-2000-2005-is-running/
Once you determine the version, you can execute the proper level of code.