sql函数笔记
公司以用户表中,有企业用户,个人用户和系统管理员。
其中user_type表示用户权限,企业用户可以赋给本公司任意员工企业用户权限。现写一个函数,用来查询每一个公司总人数,以及每种权限的人数。
CREATE FUNCTION aggregate
(
@customer_code nvarchar(30),
@user_type nvarchar(30)
)
RETURNS int
BEGIN DECLARE @num int
SET @num = 0
if @user_type != ''
SELECT @num = count(*)
FROM TBL_LMS_CSA_USER
WHERE customer_code = @customer_code and
user_type = @user_type
else
SELECT @num = count(*)
FROM TBL_LMS_CSA_USER
WHERE customer_code = @customer_code
RETURN @num
END
go
--查询,1,2,3分别是用户类型,数据有相应的字典表,分别代表个人用户,企业用户和系统管理员。
SELECT dbo.aggregate(CUSTOMER_CODE, '') , dbo.aggregate(CUSTOMER_CODE, 1) , dbo.aggregate(CUSTOMER_CODE, 2) , dbo.aggregate(CUSTOMER_CODE, 3) , CUSTOMER_CODE
FROM TBL_LMS_CSA_USER
GROUP BY CUSTOMER_CODE
ORDER BY EXPR1

浙公网安备 33010602011771号