В настоящее время я вставляю ВСЕ невоспроизводимые элементы во временную таблицу, а затем вставляю ВСЕ воспроизведенные элементы.Вместо этого я хочу пройтись по набору данных и вставить куда угодно, затем выбрать все строки из временной таблицы.
DECLARE @LEADWONDetail TABLE
(
SeqNo [int] IDENTITY(1,1) NOT NULL,
Constituencyid INT,
leadwoncode INT,
[Status] NVARCHAR(100),
DistName NVARCHAR(500),
ConstName NVARCHAR(300),
MandalName NVARCHAR(300),
VillageName NVARCHAR(500),
CandName NVARCHAR(500),
Party NVARCHAR(200),
Updatedtimestamp DATETIME
)
IF ((SELECT COUNT(*) FROM tbl_WB WHERE constid = @SlNo) = 0 AND @SlNo IS NOT NULL)
BEGIN
INSERT INTO tbl_WB
SELECT @SlNo
END
INSERT INTO @LEADWONDetail
SELECT A.Const_code AS CONST_CODE, A.Lead_WonCode,
(SELECT
CASE A.Lead_WonCode
WHEN '1' THEN N'LEAD'
WHEN '100' THEN N'WON'
END) AS [STATUS],R.English as DistrictName,OD.English as ConstName,D.English AS MandalName,A.English AS VillageName, B.English AS CANDINAME, C.English AS Party, A.UPDATEDDATETIME AS [DATETIME]
from tbl_ConstituencyMaster as A
LEFT OUTER JOIN tbl_CandidateMaster B ON A.Lead_CandiCode = B.Cand_Code
LEFT OUTER JOIN tbl_PartyMaster C ON B.PartyCode = C.PartyCode
join tbl_DistrictMaster D on A.Dist_Code=D.Dist_Code
join tbl_regionmaster R on a.reg_code=R.reg_code
join tbl_olddistrictMaster od on a.Old_dist_code=od.old_dist_code
AND A.STATECODE = @STATECODE and A.Lead_WonCode = 100 and A.Phase= 1 and R.English in ('ADILABAD','BHADRADRI KOTHAGUDEM','JAGTIAL','JANGAON','JAYASHANKAR BHUPALPALLY','JOGULAMBA GADWALA','KAMAREDDY','KARIMNAGAR','KHAMMAM','KUMURAMBHEEM','MAHABUBABAD','MANCHERIAL','MEDAK','MEDCHAL','NAGARKURNOOL')
and A.Const_code not in (select distinct ConstId from tbl_WB)
ORDER BY A.UPDATEDDATETIME DESC
INSERT INTO @LEADWONDetail
SELECT A.Const_code AS CONST_CODE, A.Lead_WonCode,
(SELECT
CASE A.Lead_WonCode
WHEN '1' THEN N'LEAD'
WHEN '100' THEN N'WON'
END) AS [STATUS],R.English as DistrictName,OD.English as ConstName,D.English AS MandalName,A.English AS VillageName, B.English AS CANDINAME, C.English AS Party, A.UPDATEDDATETIME AS [DATETIME]
from tbl_ConstituencyMaster as A
LEFT OUTER JOIN tbl_CandidateMaster B ON A.Lead_CandiCode = B.Cand_Code
LEFT OUTER JOIN tbl_PartyMaster C ON B.PartyCode = C.PartyCode
join tbl_DistrictMaster D on A.Dist_Code=D.Dist_Code
join tbl_regionmaster R on a.reg_code=R.reg_code
join tbl_olddistrictMaster od on a.Old_dist_code=od.old_dist_code
AND A.STATECODE = @STATECODE and A.Lead_WonCode = 100 and A.Phase= 1 and R.English in ('ADILABAD','BHADRADRI KOTHAGUDEM','JAGTIAL','JANGAON','JAYASHANKAR BHUPALPALLY','JOGULAMBA GADWALA','KAMAREDDY','KARIMNAGAR','KHAMMAM','KUMURAMBHEEM','MAHABUBABAD','MANCHERIAL','MEDAK','MEDCHAL','NAGARKURNOOL')
and A.Const_code in (select distinct ConstId from tbl_WB)
ORDER BY A.UPDATEDDATETIME DESC
select * from @LEADWONDetail
создать таблицу tbl_WB (id int identity (1,1), ConstId INT)
В полученном результате требуется верхняя строка в третьей позиции.