Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

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...

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.

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.

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.

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top