我有四个桌子的桌子
我们假设我有一条带有以下值的记录
...
...
类似地,我在此表中有其他具有不同值的记录
请注意,LineOfAuthority和Jurisdiction包含以逗号','分隔的多个值。
如果我将参数传递为
然后输出
LicenceNumber = 245678
我的存储过程如下
ALTER PROCEDURE [dbo].[prLicenceFetchForAgentId]
@LineOfAuthority nvarchar(max),
@Jurisdiction nvarchar(max),
@AgentId int
AS
Create table #tbLOA (
LOA nvarchar(max)
);
create table #tbtempJuris(
Jurisdiction nvarchar(max)
);
Insert into #tbLOA(LOA) Select LineOfAuthority from tbLicence where AgentId =@AgentId;
Insert into #tbtempJuris(Jurisdiction) Select Jurisdiction from tbLicence where AgentId =@AgentId;
DECLARE @LOASubString varchar(100);
DECLARE @JurisdictionSubString varchar(100);
-- first cursor
DECLARE @MyCursor CURSOR
SET @MyCursor = CURSOR FAST_FORWARD
FOR
Select LOA FROM #tbLOA
OPEN @MyCursor
FETCH NEXT FROM @MyCursor
INTO @LOASubString
WHILE @@FETCH_STATUS = 0
BEGIN
-- second cursor
DECLARE @MyCursor2 CURSOR
SET @MyCursor2 = CURSOR FAST_FORWARD
FOR
Select LOA FROM #tbLOA
OPEN @MyCursor2
FETCH NEXT FROM @MyCursor2
INTO @JurisdictionSubString
WHILE @@FETCH_STATUS = 0
BEGIN
--second code
if(@LOASubString = @LineOfAuthority and @JurisdictionSubString = @Jurisdiction)
begin
Select LicenceNumber from tbLicence where LineOfAuthority = @LOASubString
and Jurisdiction=@JurisdictionSubString and AgentId =@AgentId;
break;
end
FETCH NEXT FROM @MyCursor2
INTO @JurisdictionSubString
END
CLOSE @MyCursor2
DEALLOCATE @MyCursor2
--end cursor 2
FETCH NEXT FROM @MyCursor
INTO @LOASubString
END
CLOSE @MyCursor
DEALLOCATE @MyCursor
现在我无法得到正确的结果......它没有显示任何许可证号码...... 请帮帮我!!!
答案 0 :(得分:2)
首先,在一列中包含多个值 第一种正常形式的数据库设计>应该避免不惜一切代价!
其次,我不明白你用两个嵌套游标做了什么?!?!?!?
这似乎是一个非常简单的单SELECT
语句:
SELECT
LicenceNumber
FROM
dbo.tbLicence
WHERE
AgentId = @AgentId
AND LineOfAuthority LIKE '%' + @LineOfAuthority + '%'
AND Jurisdiction = LIKE '%' + @Jurisdiction + '%';