SQL自訂函數的應用

來源:互聯網
上載者:User
函數 函數名:GetClassName(@ClassID,@ClassType)
參數:@ClassID 類別的ID號,@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自訂函數在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




相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。