主頁 > 知識庫 > 不固定參數(shù)的存儲過程實(shí)現(xiàn)代碼

不固定參數(shù)的存儲過程實(shí)現(xiàn)代碼

熱門標(biāo)簽:陜西電銷外呼系統(tǒng)好用嗎 al智能電話機(jī)器人 數(shù)字匠心電銷機(jī)器人 最新人工智能電銷機(jī)器人 沒聽見電話機(jī)器人幫你接 銀川高頻外呼回?fù)芟到y(tǒng)多少錢 如何做地圖標(biāo)注圖鋪 成都電話外呼系統(tǒng)一般多少錢 蘭州語音外呼系統(tǒng)運(yùn)營商
我想此時(shí)不妨使用字符串參數(shù)來幫助我們解決這種情況,利用字符串分割的方法將一個(gè)參數(shù)分割成數(shù)個(gè)參數(shù)來解決。下面我們看一個(gè)例子:

假設(shè)現(xiàn)在給你一個(gè)產(chǎn)品信息列表(顯示出各個(gè)商品的基本信息),現(xiàn)在我想要根據(jù)所選擇商品進(jìn)行統(tǒng)計(jì)(任意選擇幾種),例如統(tǒng)計(jì)出價(jià)格10,11-20,21-30,31-40,41-50,50以上的商品個(gè)有多少個(gè)(姑且認(rèn)為就統(tǒng)計(jì)這些)。此時(shí)如果使用存儲過程就勢必需要傳入所選商品的id作為參數(shù),但是id個(gè)數(shù)是不固定的。此時(shí)估計(jì)會有人這樣寫:
復(fù)制代碼 代碼如下:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: jianxin160
-- Create date: 2010.11.05
-- Description: 統(tǒng)計(jì)商品
-- =============================================
ALTER PROCEDURE StatProductInfo
(
@ids VARCHAR(8000)
)
AS
BEGIN
DECLARE @followingTen INT
DECLARE @elevenToTwenty INT
DECLARE @twentyOneToThirty INT
DECLARE @thirtyOneToFourty INT
DECLARE @fourtyOneToFifty INT
DECLARE @fiftyOrMore INT

SELECT @followingTen=COUNT(*)
FROM dbo.Products
WHERE ProductID IN(@ids) AND UnitPrice10

SELECT @elevenToTwenty=COUNT(*)
FROM dbo.Products
WHERE ProductID IN(@ids) AND UnitPrice BETWEEN 11 AND 20

SELECT @twentyOneToThirty=COUNT(*)
FROM dbo.Products
WHERE ProductID IN(@ids) AND UnitPrice BETWEEN 21 AND 30

SELECT @thirtyOneToFourty=COUNT(*)
FROM dbo.Products
WHERE ProductID IN(@ids) AND UnitPrice BETWEEN 31 AND 40

SELECT @fourtyOneToFifty=COUNT(*)
FROM dbo.Products
WHERE ProductID IN(@ids) AND UnitPrice BETWEEN 41 AND 50

SELECT @fiftyOrMore=COUNT(*)
FROM dbo.Products
WHERE ProductID IN(@ids) AND UnitPrice>50

SELECT @followingTen AS '$10',@elevenToTwenty AS '$11-$20',
@twentyOneToThirty AS '$21-$30',@thirtyOneToFourty AS '$31-$40',
@fourtyOneToFifty AS '$41-$50',@fiftyOrMore AS '>$50'
END
GO

其實(shí)如果你測試一下(例如:EXEC dbo . StatProductInfo '3,4,8,10,22' )是有問題的,sql server認(rèn)為這整個(gè)是一個(gè)參數(shù),轉(zhuǎn)換時(shí)出錯(cuò)。此時(shí)我們想一下如果這些字段在一個(gè)虛表中就容易操作多了,但是一般虛表是有其他表通過查詢得到,現(xiàn)在根本無法查詢又哪來的虛表呢?聰明的朋友或許已經(jīng)想到可以使用"表值函數(shù)"。對,答案就是使用"表值函數(shù)"。我們知道"表值函數(shù)"可以返回一個(gè)"Table"類型的變量(相當(dāng)于一張?zhí)摫?,存放于?nèi)存中),我們首先將字符串分割存放到"表值函數(shù)"的一個(gè)字段中,然后我們再從"表值函數(shù)"中查詢就可以了(這個(gè)例子也是"表值函數(shù)"的一個(gè)典型應(yīng)用)。具體sql如下:
復(fù)制代碼 代碼如下:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: cmj
-- Create date: 2010.11.05
-- Description: 返回一個(gè)Table,只有一列,每一行的數(shù)據(jù)就是分割好的字符串
-- =============================================
CREATE FUNCTION GetSplitFieldsByString
(
@toSplitString varchar(1000),
@splitChar varchar(10)
)
RETURNS
@tb TABLE(sp varchar(100))
AS
BEGIN
DECLARE @i INT
SET @toSplitString=RTRIM(LTRIM(@toSplitString))
SET @i=CHARINDEX(@splitChar,@toSplitString)
WHILE @i>0
BEGIN
INSERT @tb VALUES(LEFT(@toSplitString,@i-1))
SET @toSplitString=RIGHT(@toSplitString,LEN(@toSplitString)-@i)
SET @i=CHARINDEX(@splitChar,@toSplitString)
END
IF LEN(@toSplitString)>0
INSERT @tb VALUES(@toSplitString)
RETURN
END
GO

然后我們稍微修改一下存儲過程:
復(fù)制代碼 代碼如下:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: jianxin160
-- Create date: 2010.11.05
-- Description: 統(tǒng)計(jì)商品
-- =============================================
ALTER PROCEDURE StatProductInfo
(
@ids VARCHAR(8000)
)
AS
BEGIN
DECLARE @followingTen INT
DECLARE @elevenToTwenty INT
DECLARE @twentyOneToThirty INT
DECLARE @thirtyOneToFourty INT
DECLARE @fourtyOneToFifty INT
DECLARE @fiftyOrMore INT

SELECT @followingTen=COUNT(*)
FROM dbo.Products
WHERE ProductID IN(SELECT sp FROM dbo.GetSplitFieldsByString(@ids,',')) AND UnitPrice10

SELECT @elevenToTwenty=COUNT(*)
FROM dbo.Products
WHERE ProductID IN(SELECT sp FROM dbo.GetSplitFieldsByString(@ids,',')) AND UnitPrice BETWEEN 11 AND 20

SELECT @twentyOneToThirty=COUNT(*)
FROM dbo.Products
WHERE ProductID IN(SELECT sp FROM dbo.GetSplitFieldsByString(@ids,',')) AND UnitPrice BETWEEN 21 AND 30

SELECT @thirtyOneToFourty=COUNT(*)
FROM dbo.Products
WHERE ProductID IN(SELECT sp FROM dbo.GetSplitFieldsByString(@ids,',')) AND UnitPrice BETWEEN 31 AND 40

SELECT @fourtyOneToFifty=COUNT(*)
FROM dbo.Products
WHERE ProductID IN(SELECT sp FROM dbo.GetSplitFieldsByString(@ids,',')) AND UnitPrice BETWEEN 41 AND 50

SELECT @fiftyOrMore=COUNT(*)
FROM dbo.Products
WHERE ProductID IN(SELECT sp FROM dbo.GetSplitFieldsByString(@ids,',')) AND UnitPrice>50

SELECT @followingTen AS '$10',@elevenToTwenty AS '$11-$20',@twentyOneToThirty AS '$21-$30',
@thirtyOneToFourty AS '$31-$40',@fourtyOneToFifty AS '$41-$50',@fiftyOrMore AS '>$50'
END
GO

這樣通過執(zhí)行EXEC dbo . StatProductInfo '3,4,8,10,22' 就可以得到想要的結(jié)果了:

試試這樣會不會快一些
復(fù)制代碼 代碼如下:

SELECT SUM(CASE WHEN UnitPrice 10 THEN 1 ELSE 0 END) '$10',
SUM(CASE WHEN UnitPrice BETWEEN 11 AND 20 THEN 1 ELSE 0 END) '$11-$20',
SUM(CASE WHEN UnitPrice BETWEEN 21 AND 30 THEN 1 ELSE 0 END) '$21-$30',
...
SUM(CASE WHEN UnitPrice > 50 THEN 1 ELSE 0 END) '>$10'
FROM dbo.Products
WHERE ProductID IN(SELECT sp FROM dbo.GetSplitFieldsByString(@ids,','))

標(biāo)簽:本溪 朔州 宜春 邢臺 遼源 鹽城 通化 巴彥淖爾

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《不固定參數(shù)的存儲過程實(shí)現(xiàn)代碼》,本文關(guān)鍵詞  不,固定,參數(shù),的,存儲,過程,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《不固定參數(shù)的存儲過程實(shí)現(xiàn)代碼》相關(guān)的同類信息!
  • 本頁收集關(guān)于不固定參數(shù)的存儲過程實(shí)現(xiàn)代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章