如何在 SQL 2000 中删除存储过程 + SQL 2005兼容方式?

发布于 2024-09-11 23:09:57 字数 361 浏览 7 评论 0原文

我有一个项目要求我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

夜深人未静 2024-09-18 23:09:57

这适用于 SQL 2000 和 SQL 2005。我现在已经测试过它。

USE databasename
GO

IF object_id('schema.StoredProcedureName') IS NOT NULL
DROP PROCEDURE schema.StoredProcedureName
GO

CREATE PROCEDURE schema.StoredProcedureName
.. your code

This works for both SQL 2000 and SQL 2005. I have tested it right now.

USE databasename
GO

IF object_id('schema.StoredProcedureName') IS NOT NULL
DROP PROCEDURE schema.StoredProcedureName
GO

CREATE PROCEDURE schema.StoredProcedureName
.. your code
白日梦 2024-09-18 23:09:57

不要使用系统表:使用 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

救星 2024-09-18 23:09:57

认为

 IF OBJECT_ID('your_sp_name') IS NOT NULL

会告诉你它是否存在,尽管我目前无法在 2000 上进行测试...

I think

 IF OBJECT_ID('your_sp_name') IS NOT NULL

will tell you if it is there, although I can't test on 2000 at the mo...

太阳公公是暖光 2024-09-18 23:09:57

FWIW

select * from sysobjects where type = 'p'

在 SQL 2008 中仍然有效,所以我猜测这仍然是可以接受的最低公分母。 2000 年还没有 DMV。

FWIW

select * from sysobjects where type = 'p'

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.

野侃 2024-09-18 23:09:57

您最好的选择仍然是兼容性视图、sysobectssyscolumns 等,

请查看以下链接
http://msdn.microsoft.com/en-us/library/ms187376。 ASPX

许多早期的系统表
SQL Server 版本现已发布
作为一组视图来实现。这些
视图被称为兼容性
观点,它们是为了落后
仅兼容性。兼容性
视图公开相同的元数据
在 SQL Server 2000 中可用。

You best option is staill the compatibility views, sysobects, syscolumns, etc

Check out the following link
http://msdn.microsoft.com/en-us/library/ms187376.aspx

Many of the system tables from earlier
releases of SQL Server are now
implemented as a set of views. These
views are known as compatibility
views, and they are meant for backward
compatibility only. The compatibility
views expose the same metadata that
was available in SQL Server 2000.

瘫痪情歌 2024-09-18 23:09:57

在我看来,您重新创建了关于 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.

絕版丫頭 2024-09-18 23:09:57

使用 INFORMATION_SCHEMA.ROUTINES 视图应该适用于 SQL Server 2000、2005和 2008 年。唯一的缺点是视图不再是确定对象模式的可行方法。

但如果这不是问题,请尝试这样的脚本:

USE YourDB
GO

IF EXISTS (
  SELECT * 
  FROM INFORMATION_SCHEMA.ROUTINES 
  WHERE ROUTINE_NAME = 'usp_test'
) DROP PROCEDURE usp_test
GO

CREATE PROCEDURE usp_test AS
SELECT 1 AS val
GO

EXEC usp_test
GO

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:

USE YourDB
GO

IF EXISTS (
  SELECT * 
  FROM INFORMATION_SCHEMA.ROUTINES 
  WHERE ROUTINE_NAME = 'usp_test'
) DROP PROCEDURE usp_test
GO

CREATE PROCEDURE usp_test AS
SELECT 1 AS val
GO

EXEC usp_test
GO
邮友 2024-09-18 23:09:57

在大多数情况下,我会尝试在 2005 机器上运行 SQL2000 TSQL,因为我希望它在很大程度上向后兼容。也就是说,您应该完成生产设备的升级,以便可以使用更新的 TSQL。

如果您无法找到版本之间的兼容性,您可以首先检测版本。

要确定正在运行的 SQL Server 2000/2005 版本,请使用查询分析器连接到 SQL Server 2000/2005,然后运行以下代码:

   SELECT 
      SERVERPROPERTY('productversion'), 
      SERVERPROPERTY ('productlevel'), 
      SERVERPROPERTY ('edition')

结果为:
产品版本(例如 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:

   SELECT 
      SERVERPROPERTY('productversion'), 
      SERVERPROPERTY ('productlevel'), 
      SERVERPROPERTY ('edition')

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文