[4] SQL Server – bcp

31 stycznia 2022 Wyłączono przez Adam [zicherka] Nogły

Możliwe jest importowanie lub eksportowanie danych między instancją a plikiem w określonym formacie za pomocą narzędzia bcp [(bulk copy program)/(program do kopiowania zbiorczego)].

[1] Aby użyć polecenia [bcp], które jest zawarte w pakiecie [mssql-tools], możliwe jest skopiowanie danych.

[root@vlsr01 ~]# sqlcmd -S localhost -U SA -Q 'select name,create_date from sys.databases'
Password: #wpisz hasło
name                                                                                                                             create_date
-------------------------------------------------------------------------------------------------------------------------------- -----------------------
master                                                                                                                           2003-04-08 09:13:36.390
tempdb                                                                                                                           2022-01-30 10:40:09.863
model                                                                                                                            2003-04-08 09:13:36.390
msdb                                                                                                                             2022-01-12 22:54:24.927
SampleDB                                                                                                                         2022-01-29 18:13:58.667
(5 rows affected)

#wyeksportuj dane z [Sample_Table] w [SampleDB] do [test.txt]
[root@vlsr01 ~]# bcp SampleDB.dbo.Sample_Table out test.txt -c -t, -S localhost -U SA
Password: #wpisz hasło
Starting copy...
2 rows copied.
Network packet size (bytes): 4096
Clock Time (ms.) Total     : 721    Average : (2,8 rows per sec.)

[root@vlsr01 ~]# cat test.txt
00001,CentOS,Linux,2022-01-29
00002,RedHat,Linux,2022-01-29

#inne opcje [bcp]
[root@vlsr01 ~]# bcp -h
usage: bcp {dbtable | query} {in | out | queryout | format} datafile
  [-m maxerrors]            [-f formatfile]          [-e errfile]
  [-F firstrow]             [-L lastrow]             [-b batchsize]
  [-n native type]          [-c character type]      [-w wide character type]
  [-N keep non-text native] [-q quoted identifier]
  [-t field terminator]     [-r row terminator]
  [-a packetsize]           [-K application intent]
  [-S server name or DSN if -D provided]             [-D treat -S as DSN]
  [-U username]             [-P password or tokenfile]
  [-T trusted connection]   [-v version]             [-R regional enable]
  [-k keep null values]     [-E keep identity values][-G Azure Active Directory Authentication]
  [-h "load hints"]         [-d database name]