Write a SQL function with that work based on the SQL Server version -


we have clients running on sql server 2008 , 2012.

i want write function going use format function. have custom function doing same thing format in sql server 2008, performance worse format.

i wanted make conditional if it's running on sql server 2012, use built-in t-sql format , if it's on sql server 2008, use own custom function.

so wrote function this:

create function [system].[functionname1] (     @param1 varchar(max),     @param2 varchar(max) ) returns nvarchar(max) begin      declare @sqlversion varchar(16) = convert(varchar(16), serverproperty('productversion'))      set @sqlversion = left(@sqlversion, isnull(nullif(charindex('.', @sqlversion, 1 + charindex('.', @sqlversion)) - 1, - 1), 0))      declare @return decimal(4, 2) = case              when isnumeric(@sqlversion) = 1                 convert(decimal(4, 2), @sqlversion)             else 0             end     if @return > 11.0     begin          return format(@param1, @param2)      end     else     begin         return format_custom(@parm1, @param2)     end      return null end 

the problem in sql server 2008: format function unknown. throws syntax error when want deploy it. have work around deployment script. want know can somehow sql built-in functions pass format line or ignore it?

no, can't done in function.

it has able compile code before of logic can execute, , it's failing @ point because there's function doesn't know of.

and can't apply usual work-around past compilation error - putting not-certain-to-compile code in string , compiling/executing separately using exec call - since you're not allowed dynamic sql in udfs.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

android - CollapsingToolbarLayout: position the ExpandedText programmatically -

Listeners to visualise results of load test in JMeter -