sp_panelistid1'585','201401','108972',''4','5''
alter procedure sp_panelistid1
(
@branch int,
@yearweak int,
@id int ,
@branchid varchar(10))
as
print @branchid
select f.lydelse as QuestionText, f.id as QuestionID, i.artal as Year, i.vecka as Week, i.id as Intervjuperson,
b.beskrivning as Branch, b.id as BranchID, v.beskrivning as Brand, v.id as BrandID, s.regperson as Buss, f.land as CountryID
, vi.NepaVikt as Weight , cp.CintPanelistId
from fraga f
inner join svar s on s.fraga = f.id
inner join bransch b on b.id = f.bransch
inner join varumarke v on v.id = s.varumarke
inner join intervjuperson i on i.id = s.intervjuperson
inner join vikt vi ON f.Bransch = vi.Bransch AND s.Intervjuperson = vi.Intervjuperson
inner join CintPanelistIntervjuperson cp on s.Intervjuperson=cp.Intervjuperson
where f.bransch = @branch
and (100*i.artal)+i.vecka > @yearweak
and f.land = 1 and f.id=@id
and v.beskrivning in (@branchid)
我需要在@branch id中传递多个值如何传递参数,使其在'IN条件'中工作,如v.beskrivning in('4','5','6','7 =Stämmerhelt ')
答案 0 :(得分:0)
如果您传递逗号分隔的ID,例如'1,2,3',如果您想使用“In”子句,则必须使用 Split()函数。像这样 -
SELECT f.lydelse AS QuestionText
,f.id AS QuestionID
,i.artal AS Year
,i.vecka AS Week
,i.id AS Intervjuperson
,b.beskrivning AS Branch
,b.id AS BranchID
,v.beskrivning AS Brand
,v.id AS BrandID
,s.regperson AS Buss
,f.land AS CountryID
,vi.NepaVikt AS Weight
,cp.CintPanelistId
FROM fraga f
INNER JOIN svar s ON s.fraga = f.id
INNER JOIN bransch b ON b.id = f.bransch
INNER JOIN varumarke v ON v.id = s.varumarke
INNER JOIN intervjuperson i ON i.id = s.intervjuperson
INNER JOIN vikt vi ON f.Bransch = vi.Bransch
AND s.Intervjuperson = vi.Intervjuperson
INNER JOIN CintPanelistIntervjuperson cp ON s.Intervjuperson = cp.Intervjuperson
WHERE f.bransch = @branch
AND (100 * i.artal) + i.vecka > @yearweak
AND f.land = 1
AND f.id = @id
AND v.beskrivning IN (SELECT items FROM dbo.split(@branchid, ','))
注意:分割功能不是内置功能。它使用定义的表值函数。 这是拆分功能。
这是功能。
CREATE FUNCTION [dbo].[Split] (
@String NVARCHAR(4000)
,@Delimiter CHAR(1)
)
RETURNS @Results TABLE (Items NVARCHAR(4000))
AS
BEGIN
DECLARE @INDEX INT
DECLARE @SLICE NVARCHAR(4000)
-- HAVE TO SET TO 1 SO IT DOESNT EQUAL Z
SELECT @INDEX = 1
IF @String IS NULL
RETURN
WHILE @INDEX != 0
BEGIN
-- GET THE INDEX OF THE FIRST OCCURENCE OF THE SPLIT CHARACTER
SELECT @INDEX = CHARINDEX(@Delimiter, @STRING)
-- NOW PUSH EVERYTHING TO THE LEFT OF IT INTO THE SLICE VARIABLE
IF @INDEX != 0
SELECT @SLICE = LEFT(@STRING, @INDEX - 1)
ELSE
SELECT @SLICE = @STRING
-- PUT THE ITEM INTO THE RESULTS SET
INSERT INTO @Results (Items)
VALUES (@SLICE)
-- CHOP THE ITEM REMOVED OFF THE MAIN STRING
SELECT @STRING = RIGHT(@STRING, LEN(@STRING) - @INDEX)
-- BREAK OUT IF WE ARE DONE
IF LEN(@STRING) = 0
BREAK
END
RETURN
END