Question : SQL Server stored procedure

I have a stored procedure as follows. My question is can I use #tmp_ tables (ex #tmp_initial_RMD_candidates) to insert intermediate data and then load the final data in a "tp_" table that will be used to generate a report?
***********************************************************************************************
If EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'tp_rmd_rpt')
      DROP TABLE tp_rmd_rpt
GO
IF EXISTS (SELECT name FROM sysindexes
         WHERE name = 'ix01')
    DROP INDEX tp_rmd_rpt.ix01
GO
--drop table tp_rmd_rpt
create table dbo.tp_rmd_rpt
(
 prsn_id int,
 pers_id int,
 ss_nr varchar(9),
 fst_nm varchar(20),
 mid_nm varchar(20),
 last_nm varchar(20),
 sfx_cd char(4),
 brth_dt datetime,
 deth_dt datetime,
-- age decimal(4,1),
 RMD_dt datetime
)
GO
create index ix01 on tp_rmd_rpt (pers_id)
GO
/****** Object:  StoredProcedure [dbo].[csp_rmd_rpt]    Script Date: 06/02/2010 07:24:22 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
****************************************************************************************************
* Object:            Stored Procedure dbo.csp_rmd_rpt
****************************************************************************************************
*/
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA = 'dbo' AND ROUTINE_NAME = 'csp_rmd_rpt')
      DROP PROCEDURE dbo.csp_rmd_rpt
GO

CREATE PROCEDURE dbo.csp_rmd_rpt

as

begin

declare @rpt_run_dt datetime
select @rpt_run_dt = busn_dt from be_busn_dt

Select
be_prsn.prsn_id,
pers_id,
fst_nm ,
mid_nm ,
last_nm ,
sfx_cd ,
brth_dt ,
deth_dt ,
convert(decimal(5,2),(CASE WHEN DATEPART(d, getdate()) >= DATEPART(d, brth_dt) THEN (DATEDIFF(m, brth_dt, getdate()) / 12.0)
                                                ELSE ((DATEDIFF(m, brth_dt, getdate()) - 1) / 12.0) END))  age,      
(case when day(dateadd(month, ((70*12)+6), brth_dt)) = 1 then dateadd(month, ((70*12)+6), brth_dt)
                                                else dateadd(day, 1 - day(dateadd(month, ((70*12)+6), brth_dt)), dateadd(month, 1, dateadd(month, ((70*12)+6), brth_dt)))
                                                end ) RMD_dt
into #tmp_initial_RMD_candidates
From  be_prsn, be_empt_hist,be_mbr_acct, acct_stat_vw, be_mbr
Where      be_prsn.prsn_id = be_mbr.prsn_id
            And be_empt_hist.mbr_id = be_mbr_acct.mbr_id
            And be_empt_hist.mbr_id = be_mbr.mbr_id
            And be_mbr_acct.acct_id = acct_stat_vw.acct_id
            And be_mbr_acct.mbr_id = be_mbr.mbr_id
            and be_empt_hist.end_dt in (select a.max_end_dt
            from (select mbr_id, max(end_dt) max_end_dt from be_empt_hist
                        where mbr_id = be_mbr.mbr_id
                        group by mbr_id ) a
                        where a.max_end_dt != '2999-12-31')
            And pers_id IS NOT NULL
            And pln_id <> 3
            And DATEDIFF(MONTH, brth_dt, CONVERT(DATETIME, '04.15.'+ CAST(CASE WHEN MONTH(getdate()) > 4 THEN YEAR(getdate()) + 1 ELSE YEAR(getdate()) END AS varchar)) ) > 846
            And acct_stat_vw.eff_dt =
                        (
                        SELECT MAX(x.eff_dt)
                        FROM acct_stat_vw AS x
                        WHERE x.acct_id = acct_stat_vw.acct_id AND x.acct_id = be_mbr_acct.acct_id
                              and x.acct_stat_cd in ('ACT','WAIT', 'DTH')
                              and convert(char(10),x.eff_dt,111) + str(x.acct_id) =
                              (select
                                    max(convert(char(10),x2.eff_dt,111)+ str(x2.acct_id))
                              from
                                    acct_stat_vw x2
                              where
                                    x2.acct_id = acct_stat_vw.acct_id and x2.acct_id = be_mbr_acct.acct_id
                              )
                        )
            And acct_stat_vw.acct_stat_cd  in ('ACT','WAIT', 'DTH')
group by      be_prsn.prsn_id,
                  pers_id,
                  ss_nr,
                  fst_nm,
                  mid_nm,
                  last_nm,
                  sfx_cd,
                  brth_dt,
                  deth_dt,
                  (CASE WHEN DATEPART(d, getdate()) >= DATEPART(d, brth_dt) THEN (DATEDIFF(m, brth_dt, getdate()) / 12.0)
                                                ELSE ((DATEDIFF(m, brth_dt, getdate()) - 1) / 12.0) END) --remove this from final file as the age will not be displayed in the final file.
                  ,(case when day(dateadd(month, ((70*12)+6), brth_dt)) =1 then dateadd(month, ((70*12)+6), brth_dt)
                        else
                        dateadd(day, 1 - day(dateadd(month, ((70*12)+6), brth_dt)), dateadd(month, 1, dateadd(month, ((70*12)+6),brth_dt)))
                        end )

--get acct_bal
select  
initial_rmd_accts.pers_id,
initial_rmd_accts.deth_dt,
sum(vw.pre_tax_cntrb+
       vw.post_tax_cntrb +
       vw.empr_pre_tax_cntrb +
       vw.var_empr_pre_tax_cntrb_amt +
       vw.var_post_tax_cntrb_amt +
       vw.var_pre_tax_cntrb_amt+
       vw.unt_cntrb_amt +
       vw.pre_tax_unt_cntrb_amt+                               
       vw.rglr_int_item_amt+
       vw.var_int_item_amt +
       vw.unit_int_item_amt) as tot_acct_bal
into      #acct_bal
from      #tmp_initial_RMD_candidates initial_rmd_accts, mam_acct_bal_vw vw, be_prsn a , be_mbr b, be_mbr_acct c
where   a.pers_id = initial_rmd_accts.pers_id
            and a.prsn_id = initial_rmd_accts.prsn_id
            and b.prsn_id = a.prsn_id
            and c.mbr_id = b.mbr_id
            and vw.acct_id = c.acct_id
            and c.pln_id <> 3
group by initial_rmd_accts.pers_id, initial_rmd_accts.deth_dt

select
tprmd.pers_id   pers_id,
acctbal.deth_dt deth_dt,
acctbal.tot_acct_bal,
tprmd.prsn_id prsn_id,
tprmd.fst_nm fst_nm,
tprmd.mid_nm mid_nm,
tprmd.last_nm last_nm,
tprmd.sfx_cd sfx_cd,
tprmd.brth_dt brth_dt,
tprmd.RMD_dt RMD_dt,
case when acctbal.tot_acct_bal = 0.0 and acctbal.deth_dt is not null then 'exclude'
      when (acctbal.tot_acct_bal = 0.0 and  acctbal.deth_dt is null)or (acctbal.tot_acct_bal > 0.0 and  acctbal.deth_dt is null) or (acctbal.tot_acct_bal > 0.0 and  acctbal.deth_dt is not null) then 'include'  end as RMD_flag  
into      #tmp_rmd_flag
from      tp_rmd_rpt tprmd join #acct_bal acctbal
            on acctbal.pers_id = tprmd.pers_id

insert into tp_rmd_rpt
select      
prsn_id,
pers_id,
ss_nr,
fst_nm ,
mid_nm ,
last_nm,
sfx_cd,
brth_dt,
deth_dt,
RMD_dt
from #tmp_rmd_flag
where RMD_flag = 'include' --3486

RETURN
END

Answer : SQL Server stored procedure

please add the below line and check

no global (inside) 1 interface
sysopt connection permit-vpn
access-list no-nat extended permit ip 10.200.1.0 255.255.255.0 172.25.20.0 255.255.255.0
access-list no-nat extended permit ip 10.200.1.0 255.255.255.0 172.25.30.0 255.255.255.0
nat (inside) 0 access-list no-nat
Random Solutions  
 
programming4us programming4us