Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

MSSQL:perbarui baris yang cocok dari file yang diunggah

Ini adalah pendekatan yang jauh lebih mudah untuk masuk ke data yang dibatasi pipa Anda. Anda menemukan data seperti tabel dalam #temp2. Dari sini seharusnya mudah untuk UPDATE ,INSERT atau MERGE data Anda ke target Anda.

Perhatian:Saya hanya bisa menebak arti kolom Anda, jadi nama kolomnya mungkin sampah...

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;

Hasilnya

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



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Hindari kolom NULL menggunakan DEFAULT Empty String

  2. Menggunakan Pernyataan CASE di dalam Klausa IN

  3. SQL:Tampilan dinamis dengan nama kolom berdasarkan nilai kolom di tabel sumber

  4. SQL Select, Baris Tertentu berdasarkan beberapa kondisi?

  5. Pemikiran tentang SQL Server 2019 Editions