在執行少量的sql寫入時,可以直接貼到Microsoft SQL Server Management Studio執行
但若是檔案太大時,可以使用sqlcmd的方式來執行
指令如下:
sqlcmd -S 資料庫IP -U 使用者名稱 -P 使用者密碼 -d 資料庫名稱 -i .sql檔案路徑
執行時會跳出錯誤,需開啟QUOTED_IDENTIFIER:
訊息 1934, 層級 16, 狀態 1, 伺服器 User, 行 4
INSERT 失敗,因為下列 SET 選項的設定錯誤: 'QUOTED_IDENTIFIER'。請確認與 索引檢視表及/或計算資料行上的索引及/或篩選的索引及/或查詢通知及/或 XML 資料類型方法及/或空間索引作業 一起使用的 SET 選項是否正確。
example:
C:\>sqlcmd -I -S localhost -U test -P test123 -d dbo -i C:\sql.sql
另外,若資料當中有中文,sqlcmd會直接結束
sqlcmd執行必須加入
-f 65001 代表對應中文編碼
SQL當中的中文欄位前方也要加
N
最後sqlcmd執行的參數會變成這樣子
若要將執行的結果匯出成txt檔,可以加上-o C:\sql.log example:
C:\>sqlcmd -I -S localhost -U test -P test123 -d dbo -f 65001 -i C:\sql.sql -o C:\sql.log
Ref:
http://blog.aihuadesign.com/2014/01/15/solve-sql-file-too-large-to-import-to-sql-server/
https://docs.microsoft.com/zh-tw/sql/tools/sqlcmd-utility?view=sql-server-2017
http://www.anujchaudhary.com/2011/10/sqlcmd-quotedidentifier-is-off.html
https://blog.csdn.net/roy_88/article/details/52595854
sqlcmd
-a packet_size
-A (dedicated administrator connection)
-b (terminate batch job if there is an error)
-c batch_terminator
-C (trust the server certificate)
-d db_name
-e (echo input)
-E (use trusted connection)
-f codepage | i:codepage[,o:codepage] | o:codepage[,i:codepage]
-g (enable column encryption)
-G (use Azure Active Directory for authentication)
-h rows_per_header
-H workstation_name
-i input_file
-I (enable quoted identifiers)
-j (Print raw error messages)
-k[1 | 2] (remove or replace control characters)
-K application_intent
-l login_timeout
-L[c] (list servers, optional clean output)
-m error_level
-M multisubnet_failover
-N (encrypt connection)
-o output_file
-p[1] (print statistics, optional colon format)
-P password
-q "cmdline query"
-Q "cmdline query" (and exit)
-r[0 | 1] (msgs to stderr)
-R (use client regional settings)
-s col_separator
-S [protocol:]server[instance_name][,port]
-t query_timeout
-u (unicode output file)
-U login_id
-v var = "value"
-V error_severity_level
-w column_width
-W (remove trailing spaces)
-x (disable variable substitution)
-X[1] (disable commands, startup script, environment variables, optional exit)
-y variable_length_type_display_width
-Y fixed_length_type_display_width
-z new_password
-Z new_password (and exit)
-? (usage)
sqlcmd [-U 登入識別碼] [-P 密碼]
[-S 伺服器] [-H 主機名稱] [-E 信任連線]
[-N 加密連線][-C 信任伺服器憑證]
[-d 使用資料庫名稱] [-l 登入逾時] [-t 查詢逾時]
[-h 標頭] [-s 資料行分隔符號] [-w 螢幕寬度]
[-a 封包大小] [-e 回應輸入] [-I 啟用引號識別項]
[-c cmdend] [-L[c] 伺服器清單[清除輸出]]
[-q "命令行查詢"] [-Q "命令行查詢" 並結束]
[-m 錯誤等級] [-V 嚴重性等級] [-W 移除句尾空格]
[-u unicode 輸出] [-r[0|1] 訊息傳至 stderr]
[-i 輸入檔] [-o 輸出檔] [-z 新密碼]
[-f <字碼頁> | i:<字碼頁>[,o:<字碼頁>]] [-Z 新密碼並結束]
[-k[1|2]移除 [replace] 控制項字元]
[-y 可變長度類型顯示寬度]
[-Y 固定長度類型顯示寬度]
[-p[1] 列印統計資料[冒號格式]]
[-R 使用用戶端地區設定]
[-K 應用程式意圖]
[-M 多重子網路容錯移轉]
[-b 批次中止錯誤時發生]
[-v var = "值"...] [-A 專屬的管理連線]
[-X[1] 停用命令、啟動指令碼、環境變數 [並結束]]
[-x 停用變數替代]
[-j 列印原始錯誤訊息]
[-g 啟用資料行加密]
[-G 為驗證使用 Azure Active Directory]
[-? 顯示語法摘要]