Thursday, 5 November 2015

File dump by sql Store procedure

Create PROCEDURE [dbo].[SP_NAVupload]
AS
BEGIN
--Truncate Temp Current NAV Table Data
Truncate Table tblNavdailyTemp
--Truncate Temp Current NAV Table Data

Bulk Insert tblNavdailyTemp
From 'D:\1DailyNAV.csv' -- Full Path and File Name
With
(
fieldterminator = ',',
rowterminator = '\n',
FIRSTROW = 2
)

Declare @Rcounttemp1 As int;
Set @Rcounttemp1 =(Select Count(Nav_date)
From [tblNavdaily] With(NOLOCK)
Where Convert(varchar, Nav_date, 110) = (Select Top 1 Convert(varchar, Nav_date, 110) From tblNavdailyTemp))
if(@Rcounttemp1 = 0)
Begin
--print 'in'
--Dump Current NAV Table Data to History NAV Table
Insert into tblNavHistory
(fund_id,benchmark_value,fund_value,value_insdate,benchmark_id,isChecked)
Select (Select Distinct fund_id
From [tblFundsMaster] With(NOLOCK)
Where fund_status = 'Y' And
fund_code = A.rls_fundcode And
IsNull(fund_code,'') <> ''),
NULL, A.nav_value, A.nav_date, NULL, 1
From [tblNavdaily]  As A
Where A.nav_date > (Select Max(value_insdate)
From tblNavHistory With(NOLOCK))
--Dump Current NAV Table Data to History NAV Table

--Truncate Current NAV Table Data
Truncate Table [tblNavdaily]
--Truncate Current NAV Table Data

--Dump Temp Current NAV Table Data to Current NAV Table Data
Insert Into [dbo].[tblNavdaily]
([RLS_FUNDCODE],[CREDENCE_FUNDNAME],[NAV_DATE],[NAV_VALUE],Upload_Date)
Select [RLS_FUNDCODE], [CREDENCE_FUNDNAME], [NAV_DATE], [NAV_VALUE], GetDate()
From tblNavdailyTemp With(NOLOCK)
--Dump Temp Current NAV Table Data to Current NAV Table Data
end
END

No comments:

Post a Comment