詳解SQL Server導出表為csv或txt,postgresql導入csv或txt


 目錄:

1、導出SQL Server 數據庫的表為 .txt
2、源表數據記錄中帶有空白(對源表進行處理)
3、SQL Server 數據庫中的記錄去掉前后空白
4、txt 文件編碼 GBK 轉成 UTF-8
5、在 postgresql 新建 test111 數據庫,新建表 ods.sl_account_00000000001 (字段與SQL Server對應,注意前后順序一致)
  5.1、啟動 postgresql
  5.2、powershell 連接 postgresql
  5.3、postgresql 操作
  5.4、postgresql 導入 .txt
6、中間遇到的問題

1、導出SQL Server 數據庫的表為 .txt    <--返回目錄

  源表

CREATE TABLE [dbo].[SL_Account] (
  [id] bigint  IDENTITY(1,1) NOT NULL,
  [company_name] varchar(55) COLLATE Chinese_PRC_CI_AS  NULL,
  [company_code] varchar(11) COLLATE Chinese_PRC_CI_AS  NULL,
  [business_type] varchar(15) COLLATE Chinese_PRC_CI_AS  NULL,
  [merchant_code] varchar(11) COLLATE Chinese_PRC_CI_AS  NULL,
  [account_num] varchar(20) COLLATE Chinese_PRC_CI_AS  NULL,
  [account_type] varchar(10) COLLATE Chinese_PRC_CI_AS  NULL,
  [create_datetime] datetime  NULL,
  [expire_datetime] datetime  NULL,
  [name] varchar(25) COLLATE Chinese_PRC_CI_AS  NULL,
  [phone_num] varchar(20) COLLATE Chinese_PRC_CI_AS  NULL,
  [certificate_num] varchar(20) COLLATE Chinese_PRC_CI_AS  NULL,
  [gender] varchar(10) COLLATE Chinese_PRC_CI_AS  NULL,
  CONSTRAINT [PK__SL_Accou__3213E83FA7F76B5A] PRIMARY KEY CLUSTERED ([id])
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)  
ON [PRIMARY]
)  
ON [PRIMARY]
GO

ALTER TABLE [dbo].[SL_Account] SET (LOCK_ESCALATION = TABLE)

 

  新建 sl_account.txt 文件

  開始 => SQL Server Management Studio

 

  連接

 

   選中數據庫 => 任務 => 導出數據

 

  代碼頁若指定 utf-8,后面導出時會報錯,(這個和源表的編碼有關?)

 

 

 

 

  導出速度和內存有關系(有次打開了之前導出的sl_account.txt, 再次導出的時候比較慢,關掉sl_account.txt后速度很快,170萬數據不到20秒)

 

   導出數據到 sl_account.txt,格式為

 

2、源表數據記錄中帶有空白(對源表進行處理)    <--返回目錄

  結果導出的 txt中名字不對,后面也沒有分隔符 "|"

 

 

   修改導出文件的分隔符為 ",",但是還是遇到其他的問題:

 

   查詢數據庫的對應的記錄,發現有空白(可能是回車)

  導出該條記錄的查詢結果看看:

"401244"    "十堰車城通"    "00000000001"    "IC卡"    "00000000001"    "10068263"    "員工卡"    "17/6/2009 08:40:47"    "1/4/2021 00:00:00"    "楊xx"        "8667480
"    "0"

 

3、SQL Server 數據庫中的記錄去掉前后空白    <--返回目錄

sql server中查詢刪除含有回車換行制表符的記錄
--   制表符             CHAR(9)    
--   換行符             CHAR(10)    
--   回車               CHAR(13)    
-- 在表 table 中過濾包含回車換行符的字段 filed 的記錄
select * from table where charindex(char(10)+char(13),field)>0  
-- 在表 table 中把包含回車換行符的字段 filed 的記錄的回車換行符去掉
update table set field=replace(field,char(10)+char(13),'')

  查詢 某個字段包含回車換行 的所有記錄

select * from dbo.SL_Account where charindex(char(10),certificate_num)>0
select * from dbo.SL_Account where charindex(char(13),certificate_num)>0
select * from dbo.SL_Account where charindex(char(13)+char(10),certificate_num)>0

  上面三條執行得到結果一致,所以空白是 回車換行符

  解決:

update dbo.SL_Account set name=replace(name,char(13)+char(10),'') where id = 401881

update dbo.SL_Account set name=replace(name,char(13),'') where id = 401881
update dbo.SL_Account set name=replace(name,char(10),'') where id = 401881

update dbo.SL_Account set name=replace(name,char(9),'') where id = 401881
update dbo.SL_Account set name=trim(name) where id = 401881
update dbo.SL_Account set name=replace(name,char(13)+char(10),'')

  其他字段同樣處理。下面 sql 可以參考

-- 替換
update dbo.SL_Account set name=replace(name,char(13),'')
update dbo.SL_Account set name=replace(name,char(10),'')

update dbo.SL_Account set name=replace(name,char(9),'')
update dbo.SL_Account set name=trim(name)

  
-- 查詢
select * from dbo.SL_Account where charindex(char(10),name)>0;
select * from dbo.SL_Account where charindex(char(10),phone_num)>0;
select * from dbo.SL_Account where charindex(char(10),certificate_num)>0;
select * from dbo.SL_Account where charindex(char(10),gender)>0;

select * from dbo.SL_Account where charindex(char(13),name)>0;
select * from dbo.SL_Account where charindex(char(13),phone_num)>0;
select * from dbo.SL_Account where charindex(char(13),certificate_num)>0;
select * from dbo.SL_Account where charindex(char(13),gender)>0;

select * from dbo.SL_Account where charindex(char(13)+char(10),name)>0;
select * from dbo.SL_Account where charindex(char(13)+char(10),phone_num)>0;
select * from dbo.SL_Account where charindex(char(13)+char(10),certificate_num)>0;
select * from dbo.SL_Account where charindex(char(13)+char(10),gender)>0;

 

4、txt 文件編碼 GBK 轉成 UTF-8    <--返回目錄

  參考:批量將.txt編碼格式轉化為utf8

  170萬條記錄,GBK編碼時 187 M, 轉成 utf8 后 203 M

 

5、在 postgresql 新建 test111 數據庫,新建表 ods.sl_account_00000000001 (字段與SQL Server對應,注意前后順序一致)    <--返回目錄

5.1、啟動 postgresql    <--返回目錄

  自己寫的腳本:.\bin\pg_ctl -D D:/DevTools/pgsql/data -l logfile start

 

5.2、powershell 連接 postgresql    <--返回目錄

 

5.3、postgresql 操作    <--返回目錄

  \l: 查看所有庫

  \c test111: 切換庫

  新建 test111 數據庫,新建表 ods.sl_account_00000000001 (字段與SQL Server對應,注意前后順序一致)

5.4、postgresql 導入 .txt     <--返回目錄

  命令:COPY ods.sl_account_00000000001 from 'D:\DevTools\pgsql\sl_account.txt' with  csv header  delimiter ',' ENCODING 'utf8';

  COPY 命令的使用參考:http://www.postgres.cn/docs/9.3/sql-copy.html(在表和文件之間拷貝數據)

 

 

 

6、中間遇到的問題    <--返回目錄

1)SQL Server 源數據中有空白(比如回車換行)等特殊字符,導致導出的數據換行;

  解決:參考本文標題3

2)使用 "|" 作為分隔符,發現有一條記錄某個字段中間沒了分隔符;

  解決:使用 "," 或其他作為分隔符;單獨對這條數據進行操作:update dbo.SL_Account set name='劉昕' where id=129929,將原本的空白(也不是回車、換行、制表符、空格,不知道是啥)

3)導出的 txt 為GBK, 所以需要將txt 轉換為 UTF-8;

  解決:參考本文標題4

4)postgresql 導入 txt,使用 COPY 命令時,需要已經存在的表;

5)postgresql 導入 txt 時,txt 第一行時字段,此時需要使用 with  csv header

 

參考
  1)sql server中查詢刪除含有回車換行制表符的記錄

  2)sqlserver數據庫 去除字段中空格,換行符,回車符(使用replace語句)

  3)http://www.postgres.cn/docs/9.3/sql-copy.html(在表和文件之間拷貝數據)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM