开发者

'Expression' parameter data types for user-defined functions

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜