SQL自定義函數(shù)的應(yīng)用
發(fā)表時(shí)間:2024-06-02 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]函數(shù)名:GetClassName(@ClassID,@ClassType) 參數(shù):@ClassID 類別的ID號(hào),@ClassType,類別的種類 0:語言1:總分類,2:大類別,3:子類別 ,4:國家5:省份6:市縣 返回:@ClassName類別的名稱 nvarchar(100) POWER ...
函數(shù)名:GetClassName(@ClassID,@ClassType)
參數(shù):@ClassID 類別的ID號(hào),@ClassType,類別的種類 0:語言1:總分類,2:大類別,3:子類別 ,4:國家5:省份6:市縣
返回:@ClassName類別的名稱 nvarchar(100)
POWER BY ADPOST
create time : 2004年3月24日 14:16:16
*************************************************************************/
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自定義函數(shù)在SQL查詢中的應(yīng)用:
我帖一個(gè)我自定義的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, <這是取出語言類別的名稱,調(diào)用以上定義的自定義函數(shù)>
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