views:

227

answers:

1

Our database schema is stored in a single file that is intended for production use but we also use when running functional tests on our local boxes. We drop the database and rebuild it from this file and then add a known set of test data. All was working well.

Recently we added some performance improvements. The trouble came when we adding partitioning to one of the tables.

Msg 7736, Level 16, State 1
Server 'SERVERNAME\SQLEXPRESS', Line 4
Partition function can only be created in Enterprise edition of SQL Server. Only Enterprise edition
of SQL Server supports partitioning.

Undaunted I added an if statement to check to see if we were running under Enterprise before enabling partitioning:

IF CHARINDEX('Enterprise', CONVERT(varchar(1000), SERVERPROPERTY('edition'))) > 0
BEGIN
    CREATE PARTITION FUNCTION [MyPartitionRange] (int)
    AS RANGE LEFT
    FOR VALUES (14200000, 14225000, 14250000, 14300000, 14350000, 14375000, 14400000, 14475000, 14500000, 14525000, 14550000, 14575000, 14600000, 14625000, 14650000, 14675000, 14700000, 14725000, 14750000, 14775000, 14800000, 14825000, 14850000, 14875000, 14900000, 14925000, 14950000, 14975000, 15000000 )
END

Unfortunately this still fails. I think SQL Server is telling us about the issue at the time it parses the CREATE PARTITION FUNCTION so it throws the error even if we don't use it.

Any suggestions?

+3  A: 

You may consider this a hack, but you could put the 'create partition...' in a string and then call sp_executesql on it if you are on the right edition.

Timores
It's a hack, but some things have to be hacked. Problem is, wouldn't they have to wrap *every* partitioning reference in dynamic SQL calls?
Philip Kelley
Oddly enough it's only the CREATE PARTITION FUNCTION that would need to be wrapped. All of the other partition functions work with the test for edition as I've coded above.
Epsilon Prime
@Philip, Right.If all the partitioning (or stuff working only in EE) can be put together in a separate file (say 'partitioning.sql'), one could have a conditional call to xp_cmdshell that launches osql -i 'partitioning.sql'You need to enable (at least temporarily) xp_cmdshell, but you no longer need dynamic SQL.
Timores
If we can expaned the scope of the problem to include calls from outside SQL, then yes, a LOT more options open up. I've worked up several processes in the past based on fussy routines that get selectively called based on available versions and editions.
Philip Kelley
I'd certainly be interested to hear about thoughts outside a pure SQL approach although I think we have our workaround. We wrote a quick Java program (for portability reasons since we already require that to be present) to go through and remove the lines that aren't usable in Express. That way the original schema creation script is as is for production and QA.
Epsilon Prime
You could use SMO (the SQL Server Management Objects) to write the 'setup' program. But it's a .NET API, not Java, so may not be applicable to you.
Timores