·您的位置: 首页 » 资源教程 » 编程开发 » 数据库 » SQL自定义函数的应用

SQL自定义函数的应用

类别: 数据库教程  评论数:0 总得分:0
**************************************************************************
函数名:GetClassName(@ClassID,@ClassType)
参数:@ClassID 类别的ID号,@ClassType,类别的种类 0:语言1:总分类,2:大类别,3:子类别 ,4:国家5:省份6:市县
返回:@ClassName类别的名称 nvarchar(100) *************************************************************************/
CREATE FUNCTION GetClassName(@ClassID as int, @ClassType as int)
RETURNS nvarchar(100) as
begin
declare @ClassName as nvarchar(100)
set @ClassName = \'\'
if(@ClassType = 0)
begin
SELECT @ClassName = LngName FROM LXBIZ_Language WHERE (LngTypeID = @ClassID)
end
if(@ClassType = 1)
begin
SELECT @ClassName = CategoryName FROM LXBIZ_Category WHERE (CategoryID = @ClassID)
end
if(@ClassType = 2)
begin
SELECT @ClassName = ClassName FROM LXBIZ_BigClass WHERE (ClassID = @ClassID)
end
if(@ClassType = 3)
begin
SELECT @ClassName = SubClassName FROM LXBIZ_SubClass WHERE (SubClassID = @ClassID)
end
if(@ClassType = 4)
begin
SELECT @ClassName = CountryName FROM LXBIZ_Country WHERE (CountryID = @ClassID)
end
if(@ClassType = 5)
begin
SELECT @ClassName = ProvinceName FROM LXBIZ_Province WHERE (ProvinceID = @ClassID)
end
if(@ClassType = 6)
begin
SELECT @ClassName = CityName FROM LXBIZ_City WHERE (CityID = @ClassID)
end
return @ClassName
end

SQL自定义函数在SQL查询中的应用:

我帖一个我自定义的SQL视图给大家看一下

SELECT dbo.LXBIZ_AccoutInfo.AccoutID, dbo.LXBIZ_AccoutInfo.AccoutName,
dbo.LXBIZ_AccoutInfo.AccoutPassWord, dbo.LXBIZ_AccoutInfo.SafeQuestion,
dbo.LXBIZ_AccoutInfo.SafeAnswer, dbo.LXBIZ_AccoutInfo.AccoutEmail,
dbo.LXBIZ_AccoutInfo.AccoutFlag, dbo.LXBIZ_AccoutInfo.AccoutLevel,
dbo.LXBIZ_AccoutInfo.AccountExpireTime, dbo.LXBIZ_AccoutInfo.AccountMoney,
dbo.LXBIZ_AccoutInfo.LoginCount, dbo.LXBIZ_AccoutInfo.LastLoginIP,
dbo.LXBIZ_AccoutInfo.LastLoginTime, dbo.LXBIZ_AccoutInfo.RegTime,
dbo.LXBIZ_EnterpriseInfo.EnterpriseID, dbo.LXBIZ_EnterpriseInfo.CompanyFileUrl,
dbo.LXBIZ_EnterpriseInfo.ClassID,
dbo.GetClassName(dbo.LXBIZ_EnterpriseInfo.ClassID, 2) AS ClassName,
dbo.LXBIZ_EnterpriseInfo.ModeTypeID, dbo.LXBIZ_EnterpriseInfo.SellKeyword,
dbo.LXBIZ_EnterpriseInfo.BuyKeyWord, dbo.LXBIZ_EnterpriseInfo.CompanyName,
dbo.LXBIZ_EnterpriseInfo.CountryID,
dbo.GetClassName(dbo.LXBIZ_EnterpriseInfo.CountryID, 4) AS CountryName,
dbo.LXBIZ_EnterpriseInfo.ProvinceID, dbo.LXBIZ_EnterpriseInfo.CityID,
dbo.LXBIZ_EnterpriseInfo.CompanyAddress, dbo.LXBIZ_EnterpriseInfo.CompanyUrl,
dbo.LXBIZ_EnterpriseInfo.ContactName, dbo.LXBIZ_EnterpriseInfo.JobTitle,
dbo.LXBIZ_EnterpriseInfo.ContactSex, dbo.LXBIZ_EnterpriseInfo.ContactTel,
dbo.LXBIZ_EnterpriseInfo.ContactFax, dbo.LXBIZ_EnterpriseInfo.ContactMobile,
dbo.LXBIZ_EnterpriseInfo.PostCode, dbo.LXBIZ_EnterpriseInfo.BankName,
dbo.LXBIZ_EnterpriseInfo.BankNumber, dbo.LXBIZ_EnterpriseInfo.Turnover,
dbo.LXBIZ_EnterpriseInfo.EmployersNum, dbo.LXBIZ_EnterpriseInfo.CompnayLICD,
dbo.LXBIZ_EnterpriseInfo.CompanyHits,
dbo.GetClassName(dbo.LXBIZ_EnterpriseInfo.LngTypeID, 0) AS LngName, <这是取出语言类别的名称,调用以上定义的自定义函数>
dbo.LXBIZ_EnterpriseInfo.IssueTime, dbo.LXBIZ_EnterpriseInfo.Corporate
FROM dbo.LXBIZ_EnterpriseInfo INNER JOIN
dbo.LXBIZ_AccoutInfo ON
dbo.LXBIZ_EnterpriseInfo.AccoutID = dbo.LXBIZ_AccoutInfo.AccoutID
-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1