problem
How to create ssis doing import data from excel to database sql server 2012 then show result status on excel ?
i work on sql server 2012 i create stored procedure import data from excel to database
steps for work
1- i create excel 2010 have textcolumns companyName,textcode,MetaData
2-store data on temptable and validate it if any thing form entry data not have id on my database or empty then status will not valid
3-insert data if status =null that meaning no error
4-status will be insert to record inserted
5-record have wrong or status not =null meaning have wrong data
6- i collect both inserted and not inserted as output excel sheet to kno reasons of record
not inserted
and records not inserted and show reasons on status field on excel output
i here using openrowset i need to make it using ssis package
How to make steps above using ssis package
Exec [PCN].[sp_MasterDataInsert]'\\192.168.3.108\Import\CompanyNewsInputTemplate.xlsx','\\192.168.3.108\export\CompanyNewsOutputTemplate.xlsx'
create proc [PCN].[sp_MasterDataInsert]
@ImportFilePath varchar(500) ,
@ExportFilePath varchar(500)
as
begin
create table #TempMaster(
Id int identity,
CompanyId nvarchar(200),
CompanyName nvarchar(50),
DocumentRevision_ID INT,
DoconlineUrl nvarchar(300),
DocofflineUrl nvarchar(300),
AttachementRevision_ID INT,
TextCode NVARCHAR(500),
MetaData NVARCHAR(500),
AttachonlineUrl nvarchar(300),
AttachofflineUrl nvarchar(300),
SourceGeneralTypeID int,
SourceGeneralType nvarchar(50),
ImageGeneralTypeID int,
ImageGeneralType nvarchar(300),
InitialPriGeneralTypeID int,
InitialPriGeneralType nvarchar(300),
DailyBacklogGeneralTypeID int,
DailyBacklogGeneralType nvarchar(300),
[Status] VARCHAR(50)
)
Declare @sql nvarchar(max)
Set @sql= 'insert into #TempMaster
(
CompanyName ,
DoconlineUrl,
DocofflineUrl,
AttachonlineUrl,
AttachofflineUrl,
SourceGeneralType,
ImageGeneralType,
InitialPriGeneralType,
DailyBacklogGeneralType,
TextCode,
MetaData
)
Select
CompanyName ,
DoconlineUrl,
DocofflineUrl,
AttachonlineUrl,
AttachofflineUrl,
SourceGeneralType,
ImageGeneralType,
InitialPriGeneralType,
DailyBacklogGeneralType,
TextCode,
MetaData
FROM OPENROWSET( ''Microsoft.ACE.OLEDB.12.0'', ''Excel 12.0 Xml;HDR=YES;Database=' + @ImportFilePath + ''','' SELECT * FROM [Sheet1$]'')'
Exec(@sql)
update tmp
set tmp.CompanyId=c.CompanyID
from #TempMaster tmp
INNER join Z2DataCompanyManagement.[CompanyManagers].[Company] c on
c.CompanyName =tmp.CompanyName
where tmp.[Status] is null
update tmp
set tmp.[Status]='Company Not Found'
from #TempMaster tmp
where tmp.[Status] is null
and tmp.CompanyId is null
--====================================
update tmp
set tmp.SourceGeneralTypeID=c.GeneralTypeID
from #TempMaster tmp
INNER join [PCN].[GeneralTypes] c on c.GeneralTypeName =tmp.SourceGeneralType and c.TypeID=1
where tmp.[Status] is null and c.GeneralTypeID is not null
update tmp
set tmp.[Status]='Source Type Not Found'
from #TempMaster tmp
where tmp.[Status] is null
and SourceGeneralTypeID is null
update tmp
set tmp.DailyBacklogGeneralTypeID=c.GeneralTypeID
from #TempMaster tmp
INNER join [PCN].[GeneralTypes] c on
c.GeneralTypeName =tmp.DailyBacklogGeneralType and c.TypeID=2
where tmp.[Status] is null and c.GeneralTypeID is not null
update tmp
set tmp.[Status]='DailyBacklogGeneralTypeID Type Not Found'
from #TempMaster tmp
where tmp.[Status] is null
and DailyBacklogGeneralTypeID is null
update tmp
set tmp.[Status]='MetaData Not Found'
from #TempMaster tmp
where tmp.[Status] is null
and MetaData is null
update tmp
set tmp.[Status]='TextCode Not Found'
from #TempMaster tmp
where tmp.[Status] is null
and TextCode is null
insert into
[PCN].[MasterData]
(
CompanyID ,
DocumentRevision_ID,
AttachementRevision_ID,
SourceGeneralTypeID,
ImageGeneralTypeID,
InitialPriGeneralTypeID,
DailyBacklogGeneralTypeID,
TextCode,
MetaData,
CreatedBy ,
CreatedDate ,
ModifiedDate
)
select
CompanyID ,
DocumentRevision_ID,
AttachementRevision_ID,
SourceGeneralTypeID,
ImageGeneralTypeID,
InitialPriGeneralTypeID,
DailyBacklogGeneralTypeID,
TextCode,
MetaData,
@CreatedBy ,
GETDATE() ,
GETDATE()
from #TempMaster tmp
where tmp.[Status] is null
update tmp
set tmp.[Status]='Inserted'
from #TempMaster tmp
where tmp.[Status] is null
DECLARE @Export varchar(max)
SET @Export =
'INSERT INTO OPENROWSET(''Microsoft.ACE.OLEDB.12.0'',''Excel 12.0 Xml;HDR=YES;Database='+ @ExportFilePath +''',''SELECT * FROM [Sheet1$]'')
select
CompanyName ,
DoconlineUrl,
DocofflineUrl,
AttachonlineUrl,
AttachofflineUrl,
SourceGeneralType,
ImageGeneralType,
InitialPriGeneralType,
DailyBacklogGeneralType,
TextCode,
MetaData,
[Status]
from #TempMaster' ;
execute (@Export);
drop table #TempMaster
End