我有一个要求,我应该将第一行与以前的数据库记录行进行比较,它们是逗号分隔的。示例:我的表数据如下所示:
ID Phase Updated By
1 Test1,Test2,Test3 sxmalla
2 Test1,Test2 rkgauta
3 Test1,Test3 sxmalla
我必须显示 Phase 中的最新更改以及更新的人员。逗号分隔的数据在前端使用多个 chechbox 提交。用户可以通过同一页面中的多个复选框来更新它们。这里 Test3 是最近的更改,然后在 Test2 下面是最近的更改,第三行是实际的第一个条目。
我需要将结果显示为
Phase Updated By
Test3 sxmalla
Test2 rkgauta
Test1,Test3 sxmalla
这是我目前用来比较 2 个逗号分隔行的代码
ALTER Proc [dbo].[PMT_GetPhasesEditHistory]
@Project_ID int
As Begin
SET NOCOUNT ON
Declare @added varchar(1000), @removed varchar(1000),@strN varchar(1000),@strO varchar(1000)
DECLARE test_cursor CURSOR FOR
SET @strO=(SELECT TOP 1 P1.Phase from phase P1,Phase P2 where P1.ID = P2.ID-1 and p1.project_id=@Project_ID order by p1.ID Desc)
SET @strN =(SELECT TOP 1 P2.Phase from phase P1,Phase P2
where P1.ID = P2.ID-1 and p1.project_id=@Project_ID order by p1.ID Desc)
If object_id('dbo.#tN') is not null Begin ; drop table dbo.#tN ;End
CREATE TABLE dbo.#tN( var varchar(100)); insert into #tN select * from fnSplitStringAsTable(@strN,',')
If object_id('dbo.#tO') is not null Begin; drop table dbo.#tO ;End
CREATE TABLE dbo.#tO (var varchar(100)); insert into #tO select * from fnSplitStringAsTable(@strO,',')
Declare @i int
Set @i=1
Set @added = ''
While @i != (select COUNT(*)+1 from #tN)
Begin
if not exists(select VAR from #tO where var = (select Top 1 var from #tN where var in ( Select Top (@i) VAR from #tN where Var in (Select Top (@i) var From #tN order by VAR desc) order by var asc)))
Begin
Set @added = @added + (select Top 1 var from #tN where var in ( Select Top (@i) VAR from #tN where Var in (Select Top (@i) var From #tN order by VAR desc) order by var asc)) + ','
End
set @i=@i+1
End
If(len(@added) > 1) Begin; set @added = RTRIM(LEFT(@added,Len(@added) - 1)); End
Select @added as Added
drop table #tN;Drop table #tO
End