存儲過程處理字符串數組的方法

存儲過程中傳遞數組爲參數,但是可以把數組內容拼成字符串,然後再存儲過程中解析出來處理。
USE [RecommendationTest]
GO
/****** Object: StoredProcedure [dbo].[Get_Recommendation_By_CategoryID]    Script Date: 09/26/2010 16:05:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:   <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER procedure [dbo].[Get_Recommendation_By_CategoryID]
(
    @CategoryIDArray   NVARCHAR(max),--categoryID組成的數組字符串,以逗號隔開
  
@requireCount int --每個類需要顯示數據的條數
)
AS
BEGIN
    DECLARE @categoryID    nvarchar(max)
    DECLARE    @i          INT
    DECLARE    @len        INT   
   
    IF (@CategoryIDArray IS NULL) OR (LTRIM(@CategoryIDArray) = '')
        RETURN
   
    WHILE CHARINDEX(',',@CategoryIDArray) > 0
        BEGIN
            SET @len = LEN(@CategoryIDArray)
            SET @i = CHARINDEX(',', @CategoryIDArray)
            SET @categoryID = LEFT(@CategoryIDArray, @i-1)
            print @categoryID
    --sql語句,從該類中推薦score比較高的@requireCount個資源
    select Top (@requireCount) *
    from dbo.RecommendationByCategoryID t
    where t.categoryID=@categoryID
    order by t.score DESC

            SET @CategoryIDArray = RIGHT(@CategoryIDArray, @len - @i)
        END
    SET @categoryID = @CategoryIDArray   
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章