Need SQL Query which excecutes List of ID's

Indudhar Gowda 426 Reputation points
2022-05-17T14:01:12.217+00:00
declare @rrId uniqueidentifier  
declare @rvId uniqueidentifier  
   
set nocount on  
set @rrId ='<One RRID at a time>'  
   
/* Finding Recognitions of Reportable Result */  
select distinct RecognitionId into #tempRecogs from Recognitions where ReportableResultId = @rrId  
   
 
/* Finding Result Value of the Reportable Result*/  
select @rvId = TestId from ReportableResults where ReportableResultId = @rrId  
   
/* Finding Limits of Result Value of the Reportable Result */  
select distinct TestId  into #tempRL from ResultLimits where ResultValueId = @rvId  
   
/* Finding Converted Result Value of the Reportable Result */  
select distinct TestId into #tempConvRV from  Results where  ResultId = @rrId  
   
/* Finding Converted Result Limit of the Converted Result Value of the Reportable Result */  
select distinct TestId into #tempConvRL from  Limits where  ValueId in (select * from #tempConvRV)  
   
/*Removing ADs that are shared with other reportable results*/  
delete t from   
#tempAd t join  Results adrr on t.assessmentDataId = adrr.assessmentDataId  
where  ResultId not in(select * from #tempAdrr)  
   
set nocount off  
   
print 'Deletes beginning now'  
   
if exists(select 1 from #tempRecogs)  
begin  
       print 'Deleting Recognitions'  
       delete from Recognitions where  onId in (select * from #tempRecogs)  
end  
   

if exists(select 1 from #tempConvRL)  
begin  
       print 'Deleting Converted  Limits'  
       delete from  Limits where  LimitId in (select * from #tempConvRL)  
end  
   
if exists(select 1 from #tempConvRV)  
begin  
       print 'Deleting Converted Result Values'  
       delete from ResultValues where ResultValueId in (select * from #tempConvRV)   
end  
   
print 'Deleting Result Value'  
delete from  Values where resultValueId = @rvId  
   
/* Drop all Temporary Tables*/  
drop table #tempAdrr    
drop table #tempRL  
drop table #tempConvRL  
drop table #tempConvRV  

Above Code is Working, But Manually we need to change set @rrId ='<One RRID at a time>'

set @rrId ='<One RRID at a time>' this is the Problem Statement where we are manually changing one after another.

Need list of rrId then Excecute one after another.

@rrId =('38F1B368-F65E-4113-987B-A26A28647952','15C2F60C-8757-4194-8993-61993BD0E13F')

Foreach(var variable in rrId)
{
here Existing Querey will run;
}

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,714 questions
SQL Server Reporting Services
SQL Server Reporting Services
A SQL Server technology that supports the creation, management, and delivery of both traditional, paper-oriented reports and interactive, web-based reports.
2,799 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,552 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rajat Garg 76 Reputation points
    2022-05-17T18:12:07.477+00:00

    DECLARE @RRIDLIST_Table TABLE (ID INT IDENTITY(1,1), RRID uniqueidentifier);
    ---insert all your rrid values in table
    INSERT INTO @RRIDLIST_Table (RRID) VALUES (........);

    DECLARE @maxid INT, @Currid INT, @rrid uniqueidentifier;

    SELECT @Currid = MIN(ID), @maxid = MAX(ID) FROM @RRIDLIST_Table;

    WHILE @Currid <= @maxid
    BEGIN

    SELECT @rrid = RRID FROM @RRIDLIST_Table WHERE ID = @Currid;
    --use the rrid variable above in your scripts
    <your code/scripts here>

    SET @Currid = @Currid +1;
    END


1 additional answer

Sort by: Most helpful
  1. Jingyang Li 5,891 Reputation points
    2022-05-17T16:59:53.427+00:00

    Working with a SET thinking.
    Use a table variable (temp table) or TVP if you need to. It will handle multiple values.