Link to home
Start Free TrialLog in
Avatar of Gus Koutsivitis
Gus KoutsivitisFlag for United States of America

asked on

Loop through SQL parameters and insert to temp table?

hi guys,
I am not sure how to do this.  I need to loop through these two parameters and then insert into temp table.  I have most of it written, but how would I go about looping and doing the insert?

for example in the query below, I need to insert   @DT from  ' 00:00:00.000' all the way up to  ' 23:00:00.000' in the table.
And then increase @hr from 0 all the way to 23.
It's a dirty way of doing this but it's what I have atm.  Any help would be appreciated.

QUERY:
create table ##tempAP
(	[Patient] varchar(50),
	[Age_sex] varchar(50),
	[ArrivalDT] datetime, 
	[MRN] varchar(50),
	[AcctNum] varchar(50),
	[Status1] varchar(50),
	[Status2] varchar(50),
	[DEP] varchar(50),
	[DispoDT] datetime,
	
);

DECLARE @DT varchar(50)
DECLARE @hr INT
SET  @DT =  DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), -1) +  ' 00:00:00.000'
SET @hr = 0

insert into ##tempAP
select 
{fn CONCAT({fn CONCAT({fn CONCAT({fn CONCAT({fn RTRIM(T1."pt_lst_nm")},', ')},
{fn RTRIM(T1."pt_fst_nm")})},' ')},{fn RTRIM(T1."pt_mid_nm")})} 'Patient', 
{fn CONCAT({fn CONCAT(T2."pt_age",'/')},T1."pt_sex")} 'Age_sex', 
T2."entered_dt" 'ArrivalDT', 
T3."MedRecNbr" 'MRN', 
T4."hosp_enct_no" 'acctNum', 
case  when T5."rec_date" > [b]cast(@DT as datetime2)[/b]
--{ts '2017-02-09 00:00:00.000'} 
or T5."rec_date" is null then 'ACTIVE' 
else T5."stat_code" 
end 'Status1', 
case  when T4."hosp_enct_no" in (select distinct T4."hosp_enct_no"
from 
"mpi" T1, 
"PatientMRXref" T3, 
"pt_addnl_curstat_ALL" T4, 
("ptcurstatus_noerror" T2 
LEFT OUTER JOIN "OEOrders_ALL" T5 on T2."mpi_id" = T5."mpi_id" 
and T2."pt_enct_no" = T5."enct_num" 
and 0 <> T5."mpi_id") 
LEFT OUTER JOIN "Results_ALL_Cognos" T6 on T5."seq_num" = T6."order_num" 
and T5."mpi_id" = T6."mpi_id" and T5."enct_num" = T6."enct_num"
where 
T1."mpi_id" = T2."mpi_id" 
and T3."mpi_id" = T1."mpi_id" 
and T4."mpi_id" = T2."mpi_id" 
and T4."pt_enct_no" = T2."pt_enct_no" 
and T2."entered_dt" >= {fn TIMESTAMPADD(SQL_TSI_DAY,-7,{fn NOW()})} 
and 
(case  when (T2."pt_curr_stat" in ('MANUALREL', 'RELEASED')) 
then T2."pt_stat_etm" else NULL end >= [b] cast(@DT as datetime2)[/b]
--{ts '2017-02-09 00:00:00.000'} 
or case  when T2."pt_curr_stat" in ('MANUALREL', 'RELEASED') 
then T2."pt_stat_etm" else NULL end  is null) 
and T6."date_time" <=  cast(@DT as datetime2)
--{ts '2017-02-09 00:00:00.000'} 
and T5."Universal_ServiceID" in ('readytomove', 'bedrequest') 
and {fn LOCATE('clean',T6."result_text")} <> 0) 
then 'Clean' 
when T4."hosp_enct_no" in (select distinct T4."hosp_enct_no"
from 
"mpi" T1, 
"PatientMRXref" T3, 
"pt_addnl_curstat_ALL" T4, 
("ptcurstatus_noerror" T2 
LEFT OUTER JOIN "OEOrders_ALL" T5 on T2."mpi_id" = T5."mpi_id" 
and T2."pt_enct_no" = T5."enct_num" and 0 <> T5."mpi_id") 
LEFT OUTER JOIN "Results_ALL_Cognos" T6 on T5."seq_num" = T6."order_num" 
and T5."mpi_id" = T6."mpi_id"
and T5."enct_num" = T6."enct_num"
where 
T1."mpi_id" = T2."mpi_id" 
and T3."mpi_id" = T1."mpi_id" 
and T4."mpi_id" = T2."mpi_id" 
and T4."pt_enct_no" = T2."pt_enct_no" 
and T2."entered_dt" >= {fn TIMESTAMPADD(SQL_TSI_DAY,-7,{fn NOW()})} 
and (case  when (T2."pt_curr_stat" in ('MANUALREL', 'RELEASED')) 
then T2."pt_stat_etm" else NULL end  >=  [b]cast(@DT as datetime2)[/b]
--{ts '2017-02-09 00:00:00.000'} 
or case  when T2."pt_curr_stat" in ('MANUALREL', 'RELEASED') 
then T2."pt_stat_etm" else NULL end  is null) 
and T6."date_time" <=  [b]cast(@DT as datetime2[/b])
--{ts '2017-02-09 00:00:00.000'} 
and T5."Universal_ServiceID" in ('readytomove', 'bedrequest') 
and {fn LOCATE('dirty',T6."result_text")} <> 0) 
then 'Dirty' when T4."hosp_enct_no" in (select distinct T1."hosp_enct_no"
from 
"pt_addnl_curstat_ALL" T1, 
"mpi" T3, "sysbed" T5, 
(("ptcurstatus_noerror" T4 
LEFT OUTER JOIN "PatientMRXref" T2 on T4."mpi_id" = T2."mpi_id" 
and T4."MRNSource" = T2."MRNSource") 
LEFT OUTER JOIN "pt_statistics_ALL" T6 on T4."mpi_id" = T6."mpi_id" 
and T4."pt_enct_no" = T6."pt_enct_no") 
LEFT OUTER JOIN "DispositionAll_ALL" T7 on T4."mpi_id" = T7."mpi_id" 
and T4."pt_enct_no" = T7."pt_enct_no"
where 
T1."mpi_id" = T4."mpi_id" 
and T1."pt_enct_no" = T4."pt_enct_no" 
and T3."mpi_id" = T4."mpi_id" 
and T5."bedcode" = T4."pt_bed_cd" 
and T6."stat_code" = 'OBSERV' 
and (case  when T4."pt_curr_stat" in ('MANUALREL', 'RELEASED') 
then T4."pt_stat_etm" 
else NULL end  
is null or 
case  when (T4."pt_curr_stat" in ('MANUALREL', 'RELEASED')) 
then T4."pt_stat_etm" else NULL end  >  cast(@DT as datetime2))
--{ts '2017-02-09 00:00:00.000'}) 
and T7."rec_date" is null) 
then 'Observation' 
when case  when 
(T5."rec_date" >  cast(@DT as datetime2)
--{ts '2017-02-09 00:00:00.000'} 
or T5."rec_date" is null) 
then 'ACTIVE' 
else T5."stat_code"
end  = 'Admit' 
then 'No Bed' 
when case  
when (T5."rec_date" >  [b]cast(@DT as datetime2)[/b]
--{ts '2017-02-09 00:00:00.000'} 
or T5."rec_date" is null) 
then 'ACTIVE' 
else T5."stat_code" 
end = 'ACTIVE' 
then 'Active' 
else 'Pending Release' 
end 'Status2',
T6.dept_code 'DEP',
T5."rec_date" 'DispoDT'
from 
"mpi" T1, 
"PatientMRXref" T3, 
"pt_addnl_curstat_ALL" T4, 
"sysbed" T6, 
"ptcurstatus_noerror" T2 
LEFT OUTER JOIN "DispositionNursing_ALL" T5 
on T2."mpi_id" = T5."mpi_id" 
and T2."pt_enct_no" = T5."pt_enct_no"
where 
T1."mpi_id" = T2."mpi_id" 
and T3."mpi_id" = T1."mpi_id" 
and T4."mpi_id" = T2."mpi_id" 
and T4."pt_enct_no" = T2."pt_enct_no" 
and T6."bedcode" = T2."pt_bed_cd" 
and T2."entered_dt" >= {fn TIMESTAMPADD(SQL_TSI_DAY,-7,{fn NOW()})}
 and T2."entered_dt" <=  [b]cast(@DT as datetime2)[/b]
 --{ts '2017-02-09 00:00:00.000'} 
 and (case  when (T2."pt_curr_stat" in ('MANUALREL', 'RELEASED')) 
 then T2."pt_stat_etm" else NULL end  >  [b]cast(@DT as datetime2)[/b]
 --{ts '2017-02-09 00:00:00.000'} 
 or case  when T2."pt_curr_stat" in ('MANUALREL', 'RELEASED') 
 then T2."pt_stat_etm" else NULL end  is null) 
 --and T6."dept_code" = 'EDPEDS'
order by 5 asc , 8 desc

Open in new window

SOLUTION
Avatar of James0628
James0628

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of James0628
James0628

Heh heh.  Good point, ste5an.  If they're going to put the 24 hours in a separate table, they don't really need to use a loop to create that table, since there are only 24 values.  I didn't really think that part through.

 James
Avatar of Gus Koutsivitis

ASKER

Thank you both!