I am trying to create a function that takes as a parameter an expression like what is used with BINARY_CHECKSUM. Specific开发者_开发问答ally, I want my parameter to be a list of 1 to N column names. When I look at the definition of BINARY_CHECKSUM (or MAX or CHECKSUM for that matter) in Object Explorer in SQL Server Management Studio the parameter is defined as 'Expression (any type)'- can a user-defined function emulate this? I know that I could use a parameter a delimited list of comma names, but I'm curious about 'Expression (any type)'.
You can try it this way:
declare @cmd varchar(max)
declare @columns varchar(max), @table_name sysname
set @table_name = 'sys.objects'
set @columns = '*'
set @cmd = 'select top 1 binary_checksum('+@columns+') as column_name from '+@table_name
print @cmd
exec (@cmd)
If you want to write your own aggregate functions (like BINARY_CHECKSUM
, COUNT
, etc) in SQL Server 2005 or later, you'll have to use the CLR. See the following on MSDN:
How to: Create and Run a CLR SQL Server Aggregate
CREATE AGGREGATE
精彩评论