sql server - SQL Function for SumIf 2012 -


this question has answer here:

i have following sql function, not running correctly, intended returned value total

set ansi_nulls on go set quoted_identifier on go  create function [dbo].[sumif](@columnname [varchar](100), @criteria [varchar](500)) returns [decimal]  begin     -- declare return variable here     declare @total decimal     declare @tablename decimal     select @total = sum(@columnname) @tablename  @criteria     return @total  end 

the use syntax like

select dbo.sumif(fees.fee_amount, fees.fee_code ='b01')

so tablename need extracted columnname variable passed.

i don't think able implement function way. you're trying pass expression function don't think possible in sql server. may able dynamic sql passing in strings not cleanly you're hoping.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -