2010-04-01 36 views
1

我需要的功能,但似乎無法得到它完全正確,我已經看過這裏和其他地方的例子,似乎無法得到這個剛剛好,我需要一個可選的項目列入我的查詢中,我有這個疑問(它的工作原理):如何獲取SQL函數運行不同的查詢並從任一查詢返回值?

SELECT TOP 100 PERCENT SKU, Description, LEN(CONVERT(VARCHAR 
(1000),Description)) AS LenDesc FROM tblItem 
WHERE Title = @Title AND Manufacturer = @Manufacturer 
ORDER BY LenDesc DESC 

這個工作在函數中,但是製造商是可選本次搜索的 - 這是要找到一個類似項目的描述,如果沒有出現,其它的查詢是:

SELECT TOP 100 PERCENT SKU, Description, LEN(CONVERT(VARCHAR 
(1000),Description)) AS LenDesc FROM tblItem 
WHERE Title = @Title ORDER BY LenDesc DESC 

一個缺少製造商,如何讓我的功能,使用基於製造商V無論是查詢存在或不存在。 原因是我有一個功能,首先檢查一個SKU的描述,如果它不存在 - 它使用此方法從類似產品獲取描述,然後使用相似產品的描述更新正在添加的產品。

這是迄今爲止功能:

ALTER FUNCTION [dbo].[GetDescriptionByTitleManufacturer] 
( 
    @Title varchar(400), 
    @Manufacturer varchar(160) 
) 
RETURNS TABLE 
AS 
RETURN (
    SELECT TOP 100 PERCENT SKU, Description, LEN(CONVERT(VARCHAR 
    (1000),Description)) AS LenDesc FROM tblItem 
    WHERE Title = @Title AND Manufacturer = @Manufacturer 
    ORDER BY LenDesc DESC 
    ) 

我已經嘗試添加開始和IF ...別人的,但得到的錯誤或語法問題,每一個方式,我試試吧,我希望能夠做一些事情像這樣的僞功能(不工作):

ALTER FUNCTION [dbo].[GetDescriptionByTitleManufacturer] 
( 
    @Title varchar(400), 
    @Manufacturer varchar(160) 
) 
RETURNS TABLE 
AS 
BEGIN 
IF (@Manufacturer = Null) 
RETURN (
    SELECT TOP 100 PERCENT SKU, Description, LEN(CONVERT(VARCHAR 
    (1000),Description)) AS LenDesc FROM tblItem 
    WHERE Title = @Title ORDER BY LenDesc DESC 
    ) 
ELSE 
RETURN (
    SELECT TOP 100 PERCENT SKU, Description, LEN(CONVERT(VARCHAR 
    (1000),Description)) AS LenDesc FROM tblItem 
    WHERE Title = @Title AND Manufacturer = @Manufacturer 
    ORDER BY LenDesc DESC 
    ) 

END

回答

4

你能不能做這樣的事?

ALTER FUNCTION [dbo].[GetDescriptionByTitleManufacturer] 
( 
    @Title varchar(400), 
    @Manufacturer varchar(160) 
) 
RETURNS TABLE 
AS 
RETURN (
    SELECT TOP 100 PERCENT SKU, Description, LEN(CONVERT(VARCHAR 
    (1000),Description)) AS LenDesc FROM tblItem 
    WHERE Title = @Title AND (@Manufacturer IS NULL OR Manufacturer = @Manufacturer) 
    ORDER BY LenDesc DESC 
    ) 
+0

我不能相信我錯過了能夠這樣做!通過嘗試將其作爲IF ... ELSE子句進行旁觀。我確實希望能夠根據函數的返回值將函數鏈接在一起,所以如果您可以對函數執行可選返回,那麼它會很方便 - 但這直接回答了我的問題。 – RoguePlanetoid 2010-04-01 10:13:52

+0

很高興我能幫到你。請記住標記您接受的答案! – 2010-04-01 10:22:00

+0

'(@Manufacturer IS NULL OR Manufacturer = @Manufacturer)'不會使用索引。但是,如果OP位於特定版本的SQL Server 2008上,有一種方法可以使用索引,請參閱我的答案。 – 2010-04-01 11:40:40

0

你可以嘗試使用NULL製造商犯規申請時:

SELECT TOP 100 PERCENT SKU, Description, LEN(CONVERT(VARCHAR 
(1000),Description)) AS LenDesc FROM tblItem 
WHERE Title = @Title AND (Manufacturer = @Manufacturer OR @Manufacturer IS NULL) 
ORDER BY LenDesc DESC 
1

當你做你AND (Manufacturer = @Manufacturer OR @Manufacturer IS NULL)消除使用索引。動態搜索有許多微妙的性能影響,您應該閱讀Dynamic Search Conditions in T-SQL by Erland Sommarskog

如果您擁有正確的SQL Server 2008版本(SQL 2008 SP1 CU5(10.0.2746)及更高版本),則可以使用此小技巧實際使用索引:

添加OPTION (RECOMPILE)到您的查詢,see Erland's article,和SQL Server將解決從(@Manufacturer IS NULL OR Manufacturer = @Manufacturer)OR查詢計劃之前創建基於本地變量的值,可以使用索引。

ALTER FUNCTION [dbo].[GetDescriptionByTitleManufacturer] 
( 
    @Title varchar(400), 
    @Manufacturer varchar(160) 
) 
RETURNS TABLE 
AS 
RETURN 
(SELECT TOP 100 
    PERCENT SKU, Description, LEN(CONVERT(VARCHAR(1000),Description)) AS LenDesc 
    FROM tblItem 
     WHERE Title = @Title 
     AND (@Manufacturer IS NULL OR Manufacturer = @Manufacturer) 
    ORDER BY LenDesc DESC 
    OPTION (RECOMPILE) ---<<<<only valid for SQL 2008 SP1 CU5 (10.0.2746) and later 
) 
GO