sql 自定义函数 ,where后拼接条件跳转语句,实现可变参数查询

and ( @kscode is null or ( tbzdqk.kscode = @kscode and @kscode is not null ) )  在where 条件语句之后加上如此 语句即可实现。

其中 @kscode就是那个可变参数,这里我将@kscode这个参数默认设为 null。

函数定义(关键看@kscode的定义)如下:

ALTER FUNCTION [dbo].[FeatureStat]

 -- Add the parameters for the function here
 @Annual smallint , @kscode nvarchar(15) = null
)
RETURNS @StatTB TABLE( coverArea float , destroyArea float , recoveryArea float , solidStock float , waterLossArea float ,disasterNum int )
AS
begin

..........

...................

原文地址:https://www.cnblogs.com/liwenqiang/p/2589848.html