Sybase ASE15.0之前的版本中利用bcp這個實用程序只能夠導出整表或視圖的數據。要想利用bcp有條件得導出表內數據,只能根據條件建立視圖,然後再導出該視圖的數據;或者根據條件建立臨時表,再導出臨時表的數據。但是,都必須在執行bcp命令之前到數據庫內部去創建對象(視圖或者臨時表),然後再執行bcp命令導出視圖或者臨時表數據。顯然,不是太方便。而其它的數據庫管理系統,比如sqlserver早在sqlserver2000就實現了根據條件queryout數據的功能。利用Sybase ASE的第三方工具按條件導出數據也是一種辦法。
在Sybase ASE 15.0及以後版本中,Sybase終於提供了按照條件導出表內數據的辦法,雖然實現起來還稍微有一點點小麻煩,但是終究是有這個功能了。參數:--initstring 就是為實現這個功能而加的。
關於參數:--initstring的注意事項有:
1. 參數--initstring之後的SQL語句會在數據導出之前被發送到ASE引擎;
2. 參數--initstring之後的SQL語句被當做會話級別的SQL語句一樣處理;
3. 參數--initstring之後的SQL語句在整個bcp導出數據會話期間始終有效;
4. 真正導出的數據是關鍵字bcp和out之間的表的數據,而不是參數--initstring中select列表的數據。
下面開始舉幾個例子:
bcp導出的是關鍵字bcp和out之間的表的數據,而不是--initstring中select列表的內容
bcp master..sysobjects out sysobjects.txt --initstring "select id,name,type from master..sysobjects where type='U' " -c -Usa -P -Stest
- C:\Documents and Settings\Administrator>bcp master..sysobjects out sysobjects.tx
- t --initstring "select id,name,type from master..sysobjects where type='U' " -c
- -Usa -P -Stest
- Starting copy...
- 163 rows copIEd.
- Clock Time (ms.): total = 16 Avg = 0 (10187.50 rows per sec.)
- C:\Documents and Settings\Administrator>more sysobjects.txt
- sysobjects 1 1 S 0 97 1 0 229376
- Dec 2 2009 6:58:34:590PM Dec 2 2009 6:58:34:590PM 0 0
- 0 0 0 0 0 0 1
- 0000000000000000
- sysindexes 2 1 S 0 97 0 0 229376
- Dec 2 2009 6:58:34:590PM Dec 2 2009 6:58:34:590PM 0 0
- 0 0 0 0 0 0 1
- 0000000000000000
- syscolumns 3 1 S 0 97 0 0 229376
- Dec 2 2009 6:58:34:590PM Dec 2 2009 6:58:34:590PM 0 0
- 0 0 0 0 0 0 1
- 0000000000000000
- systypes 4 1 S 0 97 1 0 229376
- Dec 2 2009 6:58:34:590PM Dec 2 2009 6:58:34:590PM 0 0
- 0 0 0 0 0 0 1
- 0000000000000000
- syslogs 8 1 S 0 1 0 0 73728 Dec 2 2
- 009 6:58:34:590PM Dec 2 2009 6:58:34:590PM 0 0 0
- 0 0 0 0 0
- syspartitions 28 1 S 0 97 2 0 229376
- Dec 2 2009 6:58:34:590PM Dec 2 2009 6:58:34:590PM 0 0
- 0 0 0 0 0 0 1
- 0000000000000000
- sysgams 14 1 S 0 1 0 0 73728 Dec 2 2
- 009 6:58:34:590PM Dec 2 2009 6:58:34:590PM 0 0 0
- 0 0 0 0 0
- systabstats 23 1 S 0 97 1 0 229888
- Dec 2 2009 6:58:34:590PM Dec 2 2009 6:58:34:590PM 0 0
- 0 0 0 0 0 0 1
- 0000000000000000
- sysusages 31 1 S 0 97 2 0 229376
- Dec 2 2009 6:58:34:590PM Dec 2 2009 6:58:34:590PM 0 0
- 0 0 0 0 0 0 1
- 0000000000000000
- sysdatabases 30 1 S 0 97 2 0 229376
- Dec 2 2009 6:58:34:590PM Dec 2 2009 6:58:34:590PM 0 0
- 0 0 0 0 0 0 1
- 0000000000000000
- sysdevices 35 1 S 0 97 1 0 229376
- Dec 2 2009 6:58:34:590PM Dec 2 2009 6:58:34:590PM 0 0
- 0 0 0 0 0 0 1
- 0000000000000000
- ^C
- C:\Documents and Settings\Administrator>
- C:\Documents and Settings\Administrator>bcp master..sysobjects out sysobjects.tx
- t --initstring "select id,name,type from master..sysobjects where type='U' " -c
- -Usa -P -Stest
- Starting copy...
- 163 rows copIEd.
- Clock Time (ms.): total = 16 Avg = 0 (10187.50 rows per sec.)
- C:\Documents and Settings\Administrator>more sysobjects.txt
- sysobjects 1 1 S 0 97 1 0 229376
- Dec 2 2009 6:58:34:590PM Dec 2 2009 6:58:34:590PM 0 0
- 0 0 0 0 0 0 1
- 0000000000000000
- sysindexes 2 1 S 0 97 0 0 229376
- Dec 2 2009 6:58:34:590PM Dec 2 2009 6:58:34:590PM 0 0
- 0 0 0 0 0 0 1
- 0000000000000000
- syscolumns 3 1 S 0 97 0 0 229376
- Dec 2 2009 6:58:34:590PM Dec 2 2009 6:58:34:590PM 0 0
- 0 0 0 0 0 0 1
- 0000000000000000
- systypes 4 1 S 0 97 1 0 229376
- Dec 2 2009 6:58:34:590PM Dec 2 2009 6:58:34:590PM 0 0
- 0 0 0 0 0 0 1
- 0000000000000000
- syslogs 8 1 S 0 1 0 0 73728 Dec 2 2
- 009 6:58:34:590PM Dec 2 2009 6:58:34:590PM 0 0 0
- 0 0 0 0 0
- syspartitions 28 1 S 0 97 2 0 229376
- Dec 2 2009 6:58:34:590PM Dec 2 2009 6:58:34:590PM 0 0
- 0 0 0 0 0 0 1
- 0000000000000000
- sysgams 14 1 S 0 1 0 0 73728 Dec 2 2
- 009 6:58:34:590PM Dec 2 2009 6:58:34:590PM 0 0 0
- 0 0 0 0 0
- systabstats 23 1 S 0 97 1 0 229888
- Dec 2 2009 6:58:34:590PM Dec 2 2009 6:58:34:590PM 0 0
- 0 0 0 0 0 0 1
- 0000000000000000
- sysusages 31 1 S 0 97 2 0 229376
- Dec 2 2009 6:58:34:590PM Dec 2 2009 6:58:34:590PM 0 0
- 0 0 0 0 0 0 1
- 0000000000000000
- sysdatabases 30 1 S 0 97 2 0 229376
- Dec 2 2009 6:58:34:590PM Dec 2 2009 6:58:34:590PM 0 0
- 0 0 0 0 0 0 1
- 0000000000000000
- sysdevices 35 1 S 0 97 1 0 229376
- Dec 2 2009 6:58:34:590PM Dec 2 2009 6:58:34:590PM 0 0
- 0 0 0 0 0 0 1
- 0000000000000000
- ^C
- C:\Documents and Settings\Administrator>
上面的這個例子中,sql語句select id,name,type from master..sysobjects where type='U' 雖然被執行了, 但是並沒有被反映到導出的結果中。
想導出sysobjects表中用戶表的id和name兩列數據,可以利用臨時表和視圖來實現。但是這和ase15以前版本中的臨時表和視圖還是不一樣的。ase15之前版本中臨時表或者視圖是顯示創建的,而在ase15及後續版本中可以利用隱式創建的臨時表或者視圖來實現根據條件導出數據的要求。
方法一: 利用隱式創建的臨時表來由條件導出數據
要求是:導出sysobjects表中用戶表的id和name兩列數據。bcp命令語句如下:
- bcp #temptbl out sysobjects_id_name.txt
- --initstring "select id,name into #temptbl
- from sysobjects where type='U' order by name " -c -Usa -P -Stest
效果如下:
- C:\Documents and Settings\Administrator>bcp #temptbl out sysobjects_id_name.txt
- --initstring "select id,name into #temptbl from sysobjects where type='U' order
- by name " -c -Usa -P -Stest
- Starting copy...
- 87 rows copIEd.
- Clock Time (ms.): total = 16 Avg = 0 (5437.50 rows per sec.)
- C:\Documents and Settings\Administrator>more sysobject_id_name.txt
- 233048835 HP_PAGENO_RANGE
- 1065051799 IVCMPLX
- 553049975 IVCON
- 601050146 IVCPROP
- 425049519 IVONEROW
- 793050830 IVPARAM
- 377049348 IVSCHM
- 457049633 IVSCON
- 937051343 IVSHARE
- 889051172 IVSPROC
- 649050317 IVSPROP
- 1033051685 IVSRVR
- 505049804 IVSSYN
- 985051514 IVSTAT
- 841051001 IVSTR
- 697050488 IVSVIEW
- 745050659 IVVCOL
- 121048436 errorlog
- 1337052768 ijdbc_function_escapes
- 1093575903 jdbc_function_escapes
- 265048949 lzflzf
- 21572084 monCachePool
- 2137055618 monCachedObject
- 533573908 monCachedProcedures
- 725574592 monCachedStatement
- 1753054250 monDataCache
- 2041055276 monDeadLock
- 117572426 monDeviceIO
- 1721054136 monEngine
- 1977055048 monErrorLog
- 85572312 monIOQueue
- 661574364 monLicense
- 2009055162 monLocks
- 1945054934 monNetworkIO
- 1881054706 monOpenDatabases
- 53572198 monOpenObjectActivity
- 629574250 monOpenPartitionActivity
- 757574706 monPCIBridge
- 821574934 monPCIEngine
- 789574820 monPCISlots
- 1785054364 monProcedureCache
- 1817054478 monProcedureCacheMemoryUsage
- ^C
- C:\Documents and Settings\Administrator>
- C:\Documents and Settings\Administrator>bcp #temptbl out sysobjects_id_name.txt
- --initstring "select id,name into #temptbl from sysobjects where type='U' order
- by name " -c -Usa -P -Stest
- Starting copy...
- 87 rows copIEd.
- Clock Time (ms.): total = 16 Avg = 0 (5437.50 rows per sec.)
- C:\Documents and Settings\Administrator>more sysobject_id_name.txt
- 233048835 HP_PAGENO_RANGE
- 1065051799 IVCMPLX
- 553049975 IVCON
- 601050146 IVCPROP
- 425049519 IVONEROW
- 793050830 IVPARAM
- 377049348 IVSCHM
- 457049633 IVSCON
- 937051343 IVSHARE
- 889051172 IVSPROC
- 649050317 IVSPROP
- 1033051685 IVSRVR
- 505049804 IVSSYN
- 985051514 IVSTAT
- 841051001 IVSTR
- 697050488 IVSVIEW
- 745050659 IVVCOL
- 121048436 errorlog
- 1337052768 ijdbc_function_escapes
- 1093575903 jdbc_function_escapes
- 265048949 lzflzf
- 21572084 monCachePool
- 2137055618 monCachedObject
- 533573908 monCachedProcedures
- 725574592 monCachedStatement
- 1753054250 monDataCache
- 2041055276 monDeadLock
- 117572426 monDeviceIO
- 1721054136 monEngine
- 1977055048 monErrorLog
- 85572312 monIOQueue
- 661574364 monLicense
- 2009055162 monLocks
- 1945054934 monNetworkIO
- 1881054706 monOpenDatabases
- 53572198 monOpenObjectActivity
- 629574250 monOpenPartitionActivity
- 757574706 monPCIBridge
- 821574934 monPCIEngine
- 789574820 monPCISlots
- 1785054364 monProcedureCache
- 1817054478 monProcedureCacheMemoryUsage
- ^C
- C:\Documents and Settings\Administrator>
方法二: 利用隱式創建的視圖來由條件導出數據
- bcp master..V_sysobjects_id_name out V_sysobjects_id_name.txt
- --initstring " create vIEw V_sysobjects_id_name as
- select id,name from sysobjects where type='U' " -c -Usa -P -Stest
效果如下:
- C:\Documents and Settings\Administrator>bcp master..V_sysobjects_id_name out V_s
- ysobjects_id_name.txt --initstring " create vIEw V_sysobjects_id_name as select
- id,name from sysobjects where type='U'" -c -Usa -P -Stest
- Starting copy...
- 87 rows copIEd.
- Clock Time (ms.): total = 16 Avg = 0 (5437.50 rows per sec.)
- C:\Documents and Settings\Administrator>more V_sysobjects_id_name.txt
- 1097051913 spt_values
- 1129052027 spt_monitor
- 1177052198 spt_limit_types
- 1088003876 syblicenseslog
- 1225052369 spt_ijdbc_table_types
- 1257052483 spt_ijdbc_mda
- 1305052654 spt_ijdbc_conversion
- 1337052768 ijdbc_function_escapes
- 1593053680 monTables
- 1625053794 monTableParameters
- 1657053908 monTableColumns
- 1689054022 monState
- 1721054136 monEngine
- 1753054250 monDataCache
- 1785054364 monProcedureCache
- 1817054478 monProcedureCacheMemoryUsage
- 1849054592 monProcedureCacheModuleUsage
- 1881054706 monOpenDatabases
- 1913054820 monSysWorkerThread
- 1945054934 monNetworkIO
- 1977055048 monErrorLog
- 2009055162 monLocks
- 2041055276 monDeadLock
- 2073055390 monWaitClassInfo
- 2105055504 monWaitEventInfo
- 2137055618 monCachedObject
- 21572084 monCachePool
- 53572198 monOpenObjectActivity
- 85572312 monIOQueue
- 117572426 monDeviceIO
- 149572540 monSysWaits
- 181572654 monProcess
- 213572768 monProcessLookup
- 245572882 monProcessActivity
- 277572996 monProcessWorkerThread
- 309573110 monProcessNetIO
- 341573224 monProcessObject
- 373573338 monProcessWaits
- 405573452 monProcessStatement
- 437573566 monSysStatement
- 469573680 monProcessSQLText
- 501573794 monSysSQLText
- ^C
- C:\Documents and Settings\Administrator>
- C:\Documents and Settings\Administrator>bcp master..V_sysobjects_id_name out V_s
- ysobjects_id_name.txt --initstring " create vIEw V_sysobjects_id_name as select
- id,name from sysobjects where type='U'" -c -Usa -P -Stest
- Starting copy...
- 87 rows copIEd.
- Clock Time (ms.): total = 16 Avg = 0 (5437.50 rows per sec.)
- C:\Documents and Settings\Administrator>more V_sysobjects_id_name.txt
- 1097051913 spt_values
- 1129052027 spt_monitor
- 1177052198 spt_limit_types
- 1088003876 syblicenseslog
- 1225052369 spt_ijdbc_table_types
- 1257052483 spt_ijdbc_mda
- 1305052654 spt_ijdbc_conversion
- 1337052768 ijdbc_function_escapes
- 1593053680 monTables
- 1625053794 monTableParameters
- 1657053908 monTableColumns
- 1689054022 monState
- 1721054136 monEngine
- 1753054250 monDataCache
- 1785054364 monProcedureCache
- 1817054478 monProcedureCacheMemoryUsage
- 1849054592 monProcedureCacheModuleUsage
- 1881054706 monOpenDatabases
- 1913054820 monSysWorkerThread
- 1945054934 monNetworkIO
- 1977055048 monErrorLog
- 2009055162 monLocks
- 2041055276 monDeadLock
- 2073055390 monWaitClassInfo
- 2105055504 monWaitEventInfo
- 2137055618 monCachedObject
- 21572084 monCachePool
- 53572198 monOpenObjectActivity
- 85572312 monIOQueue
- 117572426 monDeviceIO
- 149572540 monSysWaits
- 181572654 monProcess
- 213572768 monProcessLookup
- 245572882 monProcessActivity
- 277572996 monProcessWorkerThread
- 309573110 monProcessNetIO
- 341573224 monProcessObject
- 373573338 monProcessWaits
- 405573452 monProcessStatement
- 437573566 monSysStatement
- 469573680 monProcessSQLText
- 501573794 monSysSQLText
- ^C
- C:\Documents and Settings\Administrator>
在--initstring中的sql語句中創建了視圖V_sysobjects_id_name。 我們來看看bcp導出數據完成後,該視圖V_sysobjects_id_name還存在否?
- 1> use master
- 2> go
- 1> select name from sysobjects where name='V_sysobjects_id_name'
- 2> go
- name
- -------------------------------------------------------------------------------
- --------------------------------------------------------------------------------
- --------------------------------------------------------------------------------
- ----------------
- V_sysobjects_id_name
- (1 row affected)
- 1>
- 1> use master
- 2> go
- 1> select name from sysobjects where name='V_sysobjects_id_name'
- 2> go
- name
- -------------------------------------------------------------------------------
- --------------------------------------------------------------------------------
- --------------------------------------------------------------------------------
- ----------------
- V_sysobjects_id_name
- (1 row affected)
- 1>
視圖V_sysobjects_id_name仍然是存在的。此種方法和ase12.x中利用顯示創建的視圖導出數據的方法比較類似,只不過稍微簡便些罷了。
總結一下:
1. ase15.0及後續版本中實現了根據條件導出表的數據,這點很值得慶幸。
2. 此bcp導出的不是參數--initstring中sql語句的結果,而仍然是關鍵字bcp和out之間的表的數據。
3. 增加的參數--initstring實際上就是實現了能夠利用bcp工具向ASE引擎發送sql命令而已。
4. 參數--initstring中的sql語句在導出數據整個會話期間有效。所以,導出--initstring中創建的臨時表是可能的。
5. 歸根結底,bcp增加的這個新特性,僅僅是能夠通過bcp向ase引擎發送sql命令罷了。
作者簡介:andkylee,5年Sybase管理、維護經驗。現任職於北京一IT運維管理公司,Sybase DBA。熟悉Sybase的安裝、配置、調優、監控與排錯,尤其精通Sybase數據庫的災難恢復。自己深入研究Sybase數據庫的內部物理存儲結構,開發了能夠從Sybase數據庫設備文件中提取數據的工具;還編寫了一個能夠分析Sybase日志文件內容,反解析出相應SQL語句的程序。可以提供Sybase數據庫非常規恢復技術支持。Sybase非常規數據庫恢復包括:設備文件故障(如:頁面邏輯損壞,頁面物理損壞等,605、692錯誤等等),誤操作(包括:誤更新update,誤刪除drop table,誤清空數據truncate table,等)等,本人都有相應的處理辦法。