Domanda

I have a SP that runs everynight to Insert and Update the content of a table based on an excel file (Excel 2010 on Windows Server 20008 R2). Below is my SP and the image represents my table's structure and the excel file format. I just need to double check my SP with you guys to make sure I am doing this correctly and if I am on the right track. The excel file includes 3 columns both Cust_Num and Cust_Seq are primary since there would never be a case that same combination of Cust_Num and Cust_Seq exist for a customer name. For example, for Cust_Num = 1 and Cust_Num=0 there will never be another of same combination of Cust_Num being 1 and Cust_Num being 0. However the name will usually repeat in the spreadsheet. So, would you guys please let me know if the SP is correct or not? (in the SP first the Insert statement runs and then the Update Statement):

**First The Insert runs in the SP

 INSERT INTO Database.dbo.Routing_CustAddress
 SELECT a.[Cust Num],a.[Cust Seq],a.[Name]
 FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 
 'Excel 8.0;HDR=YES;Database=C:\Data\custaddr.xls;', 
 'SELECT*
 FROM [List_Frame_1$]') a Left join Routing_CustAddress b 
 on a.[Cust Num] = b.Cust_Num and a.[Cust Seq] = b.Cust_Seq where b.Cust_Num is null

 ***Then the Update Runs in the SP

 UPDATE SPCustAddress
 SET SPCustAddress.Name = CustAddress.Name
 FROM ArPd_App.dbo.Routing_CustAddress SPCustAddress
 INNER JOIN OPENROWSET('Microsoft.ACE.OLEDB.12.0', 
 'Excel 8.0;HDR=YES;Database=C:\Data\custaddr.xls;', 
 'SELECT *
 FROM [List_Frame_1$]')CustAddress
 ON SPCustAddress.Cust_Num = CustAddress.[Cust Num]
 AND SPCustAddress.Cust_Seq = CustAddress.[Cust Seq]

Table Structure and Excel File Format

È stato utile?

Soluzione

Right here is some code I havent tested it so I'll leave it for you but it shold work

Create the stagging table first.

CREATE TABLE dbo.Routing_CustAddress_Stagging
(
Cust_Name NVARCHAR(80),
Cust_Seq  NVARCHAR(80),
Name      NVARCHAR(MAX)
)
GO

Then create the following Stored Procedure. It will take the FilePath and Sheet name as parameter and does the whole lot for you.

1) TRUNCATE the stagging table.
2) Upload data into stagging table from provided Excel file, and sheet.
3) and finnaly does the UPSERT operation in two separate statements.


CREATE PROCEDURE usp_Data_Upload_Via_File
@FilePath  NVARCHAR(MAX),
@SheetName NVARCHAR(MAX)
AS
BEGIN
 SET NOCOUNT ON;


          IF (@FilePath IS NULL OR @SheetName IS NULL)
            BEGIN
              RAISERROR('Please Provide valid File Path and SheetName',16,1)
              RETURN;
            END

  -- Truncate the stagging table first
  TRUNCATE TABLE dbo.Routing_CustAddress_Stagging;

  -- Load Data from Excel sheet

DECLARE @Sql       NVARCHAR(MAX);

SET @Sql =  N' INSERT INTO dbo.Routing_CustAddress_Stagging ([Cust Num],[Cust Seq],[Name]) ' +
            N' SELECT [Cust Num],[Cust Seq],[Name]  '                +
            N' FROM OPENROWSET(''Microsoft.ACE.OLEDB.12.0'', '       +
            N' ''Excel 8.0;HDR=YES;Database='+ @FilePath + ';'' ,' +
            N' ''SELECT* FROM ['+ @SheetName +']'')' 

EXECUTE sp_executesql @Sql

 -- Now the UPSERT statement.
 UPDATE T
 SET T.Name = ST.NAME
 FROM dbo.Routing_CustAddress T INNER JOIN dbo.Routing_CustAddress_Stagging ST
 ON T.Cust_Name = ST.Cust_Name AND T.Cust_Seq = ST.Cust_Seq

 -- Now the Insert Statement
 INSERT INTO dbo.Routing_CustAddress 
 SELECT ST.[Cust Num],ST.[Cust Seq],ST.[Name]
 FROM dbo.Routing_CustAddress_Stagging ST LEFT JOIN dbo.Routing_CustAddress T
 ON T.Cust_Name = ST.Cust_Name AND T.Cust_Seq = ST.Cust_Seq
 WHERE T.Cust_Name IS NULL OR T.Cust_Seq IS NULL

END
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top