MSSQL: update matching rows from the uploaded file

2019-09-05 14:03发布

问题:

Help me guys, I want to update my existing table from the uploaded files, but I am having a trouble on the part where the update starts, I want to match the reference number of from the table so it will select the right row to update.

USE [RMT]
GO
/****** Object:  StoredProcedure [dbo].[gen048update]    Script Date: 03/29/2016 08:35:52 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      
-- Create date: 
-- Description: Upload data
-- =============================================
ALTER PROCEDURE [dbo].[gen048update]
(
        @fileDate VARCHAR(20)
)


AS BEGIN
SET NOCOUNT ON

declare @sql NVARCHAR(MAX),

@dynamic varchar(8000),
@tablesuffix varchar(8),
@refd varchar(30),
@refcnt varchar(30)



select @refd=(SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 102), '.', '') AS [MMDDYYYY])
select @tablesuffix=(SELECT REPLACE(UPPER(RIGHT(CONVERT(VARCHAR(11), GETDATE(), 106), 8)), '', '') AS [Mon-YYYY])
--print @tablesuffix
--convert(varchar,day(convert(datetime, @fileDate)))+upper(substring(datename(m,convert(datetime,@fileDate)),1,3))+convert(varchar,year(convert(datetime,@fileDate)))


--**COMPOSE FIGURE COLUMNS
 declare @currmonth varchar(10)
 select @dynamic=''

create table #logs(
i int identity(1,1),
tableName varchar(50),

status varchar(30)
)

--SELECT getdate()

    SET NOCOUNT ON;
    CREATE TABLE #temp (WHOLEROW VARCHAR(8000))

    IF OBJECT_ID('tempdb.dbo.#temp') IS NOT NULL
        DROP TABLE #temp

    CREATE TABLE #temp1 (WHOLEROW VARCHAR(8000))

    --BEGIN TRY

        BEGIN TRANSACTION

        DECLARE @SQL2 NVARCHAR(MAX)
        SET @SQL2 = 'BULK INSERT #temp1
        FROM ''Y:\FTP\RMT\RMT\gen048\delivery\' + @fileDate + '.txt' + '''
        WITH (DATAFILETYPE = ''char'', KEEPNULLS, FIRSTROW=1, MAXERRORS=0, ROWTERMINATOR =  ''' + CHAR(0x0A) + ''')'

;WITH Splitted(AsXml) AS
(
    SELECT CAST('<x>' + REPLACE(t1.WHOLEROW,'|','</x><x>') + '</x>' AS XML)
    FROM #records AS t1
)
SELECT AsXml.value('x[1]','varchar(max)') AS ID
      ,ID_Splitted.*
      ,AsXml.value('x[2]','varchar(max)') AS FSI_Field
      ,AsXml.value('x[3]','varchar(max)') AS CAPTION
      ,AsXml.value('x[4]','varchar(max)') AS STATUS
      ,AsXml.value('x[5]','varchar(max)') AS NUMBER
      ,AsXml.value('x[6]','varchar(max)') AS Fld1
      ,AsXml.value('x[7]','varchar(max)') AS Fld2
      ,AsXml.value('x[8]','varchar(max)') AS Fld3
      ,AsXml.value('x[9]','varchar(max)') AS Fld4
      ,AsXml.value('x[10]','varchar(max)') AS TARGET
      ,AsXml.value('x[11]','varchar(max)') AS TARGET_TYPE
      ,AsXml.value('x[12]','varchar(max)') AS TARGET_NUMBER
INTO #temp2
FROM Splitted
CROSS APPLY(SELECT LEFT( AsXml.value('x[1]','varchar(max)'),1) AS FirstLetter
                  ,CAST(SUBSTRING(AsXml.value('x[1]','varchar(max)'),2,8) AS DATE) AS IncludedDate
                  ,CAST(SUBSTRING( AsXml.value('x[1]','varchar(max)'),11,100)  AS INT) AS IncludedNumber) AS ID_Splitted;

        EXEC sys.sp_executesql @SQL2

declare @sourceTable varchar(500)
declare @year varchar(22)
declare @month varchar(3)
declare @test varchar(11)
declare @result varchar(8)
declare @index int

declare @string varchar(15)
set @string = (SELECT DISTINCT TOP 1 REPLACE(dbo.fn_Parsename(WHOLEROW, '|', 0), CHAR(9), '') FROM #temp1)
set @test = (select UPPER(convert(datetime,substring(@string,2,charindex('-',@string,1)-2))))
--select @test

set @month =(left(@test,3))

set @year = (right(@test,4))
--select @year

set @result = @month + @year
-- select @result

set @sourceTable = 'gen_048_'+@result
select @sourceTable

declare @ref varchar(255)
SET @ref = 'select convert(varchar(55),refdate)+''-''+convert(varchar(55),refcount)'
select @ref

declare @string2 varchar(255)
SET @string2 = 'select convert(varchar(55),refdate)+''-''+convert(varchar(55),refcount) FROM '
               + Quotename(@sourceTable)
print @string2
EXEC (@string2)

set @index = 1




SELECT * FROM #temp2;

DROP TABLE #temp2;
DROP TABLE #temp1;


declare @refnum varchar(100)
declare @stat varchar(100)
declare @statdate varchar(100)


WHILE (@index <= (SELECT MAX([index]) FROM #records))


        BEGIN

        set @stat = (select stat from #records where [index] = @index)
        select @stat

        set @statdate = (select statdate from #records where [index] = @index)
        select @statdate

        set @refnum = (select refnum from #records where [index] = @index)
        set @refnum = replace(@refnum, 'F', '')

        select @refnum
        --  = '+@refnum+'(courier, stat, statdate, stat2, statdate2, stat3, statdate3, recvby, rel, pupdate)
        select * from #records

                        --(select UPPER(convert(datetime,substring(@string,2,charindex('-',@string,1)-2))))
        set @sql = '
            MERGE gen_048_MAR2016 target
            USING #records source
                ON target.refdate'+'-'+'select convert(varchar(20), target.refcount) = (select substring(source.refnum, 2, 13))
            WHEN MATCHED THEN
                UPDATE
                    SET
                    target.stat = source.stat

            WHEN NOT MATCHED BY TARGET THEN
              INSERT (stat, statdate)
              VALUES (source.stat, source.statdate)                 
                ;'
                select @refnum, @stat, @statdate
                print @sql
                exec (@sql)

            SELECT 'File has been successfully uploaded', @fileDate,'success' as msg

          set @index = @index + 1

       END

        COMMIT TRANSACTION

END

回答1:

This is a much easier approach to get into your pipe delimited data. You find your table-like data within #temp2. From here on it should be easy to UPDATE,INSERT or MERGE your data into your target.

Attention: I could just guess the meaning of your columns, so the column names might be rubbish...

CREATE TABLE #temp1(WHOLEROW VARCHAR(8000));
INSERT INTO #temp1 VALUES
 ('F20160323-1000|FSI|BISCOCHO|DELIVERED|42317|||||GINA T|TENANT|42311')
,('F20160323-1001|FSI|MARTINNE|DELIVERED|42314|||||MERLIE STRADA|IN LAW|42311')
,('F20160323-1002|FSI|MANALO|DELIVERED|42313|||||GINA A|TENANT|42311')
,('F20160323-1004|FSI|HAMOY CAJAYON|DELIVERED|42311|||||MERLIE QTRADA|IN LAW|42311')
,('F20160323-1003|FSI|FRANCISCA|DELIVERED|42312|||||GINA B|TENANT|42311')
,('F20160323-1005|FSI|NINA|DELIVERED|42318|||||MERLIE STRADA|IN LAW|42311');

;WITH Splitted(AsXml) AS
(
    SELECT CAST('<x>' + REPLACE(t1.WHOLEROW,'|','</x><x>') + '</x>' AS XML)
    FROM #temp1 AS t1
)
SELECT AsXml.value('x[1]','varchar(max)') AS ID
      ,ID_Splitted.*
      ,AsXml.value('x[2]','varchar(max)') AS FSI_Field
      ,AsXml.value('x[3]','varchar(max)') AS CAPTION
      ,AsXml.value('x[4]','varchar(max)') AS STATUS
      ,AsXml.value('x[5]','varchar(max)') AS NUMBER
      ,AsXml.value('x[6]','varchar(max)') AS Fld1
      ,AsXml.value('x[7]','varchar(max)') AS Fld2
      ,AsXml.value('x[8]','varchar(max)') AS Fld3
      ,AsXml.value('x[9]','varchar(max)') AS Fld4
      ,AsXml.value('x[10]','varchar(max)') AS TARGET
      ,AsXml.value('x[11]','varchar(max)') AS TARGET_TYPE
      ,AsXml.value('x[12]','varchar(max)') AS TARGET_NUMBER
INTO #temp2
FROM Splitted
CROSS APPLY(SELECT LEFT( AsXml.value('x[1]','varchar(max)'),1) AS FirstLetter
                  ,CAST(SUBSTRING(AsXml.value('x[1]','varchar(max)'),2,8) AS DATE) AS IncludedDate
                  ,CAST(SUBSTRING( AsXml.value('x[1]','varchar(max)'),11,100)  AS INT) AS IncludedNumber) AS ID_Splitted;

SELECT * FROM #temp2;

DROP TABLE #temp2;
DROP TABLE #temp1;

The result

ID         FirstLetter  IncludedDate    InclNmbr    FSI CAPTION      STATUS     NUMBER  TARGET          TYPE    NUMBER
F20160323-1000  F       2016-03-23      1000        FSI BISCOCHO     DELIVERED  42317   GINA T          TENANT  42311
F20160323-1001  F       2016-03-23      1001        FSI MARTINNE     DELIVERED  42314   MERLIE STRADA   IN LAW  42311
F20160323-1002  F       2016-03-23      1002        FSI MANALO       DELIVERED  42313   GINA A          TENANT  42311
F20160323-1004  F       2016-03-23      1004        FSI HAMOY CAJAYONDELIVERED  42311   MERLIE QTRADA   IN LAW  42311
F20160323-1003  F       2016-03-23      1003        FSI FRANCISCA    DELIVERED  42312   GINA B          TENANT  42311
F20160323-1005  F       2016-03-23      1005        FSI NINA         DELIVERED  42318   MERLIE STRADA   IN LAW  42311