如何在通过usql连接的Sybase上查找一个只知道column value不知道column name的数据

如何在通过usql连接的Sybase上查找一个只知道column value不知道column name的数据

  • 起因
  • 条件
  • 开始工作
    • 找到所有包含了类似“chk_seq”字段的表
      • 列出数据库的系统表
        • 发现有个叫ISYSTABCOL的表,可能包含了所有表的column name
      • 找到名字类似chk_seq的column_name及其相关的table_id
      • 找出这些table_id关联的表名
      • 因为数据与订单表关联,数据量不可能太小,我们来列出每个表的数据量
        • 用ruby拼装sql语句
        • 执行sql后发现这些表的数据量值得考虑
    • 找到这些表中chk_seq = 187196的数据
      • 用ruby拼装sql
    • 锁定目标

起因

笔者因为工作,需要在通过usql远程连接的Sybase上找到某条column value为18628486但是column name未知的数据。因为用了usql,Sybase的命令统统用不了,只能用sql解决。
这个数据库有1000多张表,用寻常的方法找肯定是不现实的,比如列出每个表里所有的column name,然后再一个一个试:

select * from sys.syscolumns where tname = 'mi_def';

看看表的数量:

select name from sysobjects where type='U' ;
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.......
 mfd_check_ttl
 mfd_check_dtl
 mfd_check_opn
 mfd_check_status
 itm_phys_cnt_dtl_temp
 sp_parm_temp
 sp_parm_lst_temp
 ord_tmp_table
(1004 rows)

条件

根据这个任务的前期工作,笔者掌握到了如下信息:

  1. 笔者需要找到的是一条订单数据,现在订单已经找到,但是这条订单的发起人未知,只知道他的id是18628486;
  2. 订单表有一个比较通用的标记,column name为chk_seq,column value为187196;
  3. 其他表很可能是通过chk_seq与订单表关联的;

开始工作

找到所有包含了类似“chk_seq”字段的表

列出数据库的系统表

 select name from sysobjects where type = 'S';
 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  name
-----------------------
 ISYSTAB
 ISYSTABCOL
 ISYSOBJECT
 ISYSIDX
 ISYSPHYSIDX
 ISYSIDXCOL
 ISYSFKEY
 ISYSDBSPACE
 ISYSDBFILE
 ISYSDOMAIN
 ISYSUSER
 ISYSLOGINPOLICY
 ISYSLOGINPOLICYOPTION
 ISYSTABLEPERM
 ISYSCOLPERM
 ISYSOPTION
 ISYSGROUP
 ISYSUSERAUTHORITY
 ISYSDBSPACEPERM
 ISYSPROCEDURE
 ISYSPROCPARM
 ISYSTRIGGER
 ISYSPROCPERM
 ISYSREMOTETYPE
 ISYSREMOTEOPTIONTYPE
 ISYSREMOTEOPTION
 ISYSPUBLICATION
 ISYSARTICLE
 ISYSARTICLECOL
 ISYSREMOTEUSER
 ISYSSUBSCRIPTION
 ISYSUSERMESSAGE
 ISYSUSERTYPE
 ISYSSQLSERVERTYPE
 ISYSTYPEMAP
 ISYSSYNC
 ISYSSYNCSCRIPT
 ISYSLOGINMAP
 ISYSEVENT
 ISYSSCHEDULE
 ISYSMVOPTIONNAME
 ISYSMVOPTION
 ISYSJAR
 ISYSJAVACLASS
 ISYSJARCOMPONENT
 ISYSATTRIBUTENAME
 ISYSATTRIBUTE
 ISYSSERVER
 ISYSPROXYTAB
 ISYSEXTERNLOGIN
 ISYSCAPABILITY
 ISYSEXTERNENV
 ISYSEXTERNENVOBJECT
 ISYSSYNCPROFILE
 ISYSWEBSERVICE
 ISYSCOLSTAT
 ISYSOPTSTAT
 ISYSHISTORY
 ISYSCONSTRAINT
 ISYSCHECK
 ISYSVIEW
 ISYSDEPENDENCY
 ISYSREMARK
 ISYSSOURCE
 ISYSTEXTCONFIG
 ISYSTEXTIDX
 ISYSTEXTIDXTAB
 DUMMY
(68 rows)

可以看的系统表的表名前都加了大写的i,查找是去掉I就好了。

发现有个叫ISYSTABCOL的表,可能包含了所有表的column name

select top 1 * from SYSTABCOL;
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
table_id | column_id | domain_id | nulls | width | scale | object_id | max_identity | column_name                                                                                                                      | default | user_type | column_type | compressed | collect_stats | inline_max | inline_long | lob_index
----------+-----------+-----------+-------+-------+-------+-----------+--------------+----------------------------------------------------------------------------------------------------------------------------------+---------+-----------+-------------+------------+---------------+------------+-------------+-----------
       1 |         1 |        21 | N     |     4 |     0 |         2 |            0 | table_id                                                                                                                         |         |           | R           |          0 |             1 |            |             |
(1 row)

看来可以找到column_name和对应的table_id

找到名字类似chk_seq的column_name及其相关的table_id

select table_id,column_name from SYSTABCOL where column_name like "%chk_seq%";
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
 table_id | column_name
----------+----------------------------------------------------------------------------------------------------------------------------------
      728 | chk_seq
      732 | chk_seq
      732 | standalone_chk_seq
      732 | void_chk_seq
      733 | chk_seq
      734 | chk_seq
      917 | chk_seq
     1469 | chk_seq
     1494 | chk_seq
     1522 | C_chk_seq
     1532 | chk_seq
     1582 | chk_seq
     1617 | chk_seq
     1631 | chk_seq
     2058 | C_chk_seq
     2073 | chk_seq
     2079 | xfer_chk_seq
     2079 | chk_seq
     2080 | chk_seq
     2131 | chk_seq
     2150 | chk_seq
     2151 | chk_seq
     2152 | chk_seq
     2153 | chk_seq
     2160 | chk_seq
     2161 | chk_seq
     2162 | chk_seq
     2163 | chk_seq
     2164 | chk_seq
     2165 | chk_seq
     2166 | chk_seq
     2167 | chk_seq
     2168 | chk_seq
     2169 | chk_seq
     2252 | chk_seq
     2051 | C_chk_seq
     2050 | C_chk_seq
(37 rows)

找出这些table_id关联的表名

select table_name from SYSTAB where table_id in (728, 732, 732, 732, 733, 734, 917,1469,1494,1522,1532,1582,1617,1631,2058,2073,2079,2079,2080,2131,2150,2151,2152,2153,2160,2161,2162,2163,2164,2165,2166,2167,2168,2169,2252,2051,2050);
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
 table_name
----------------------------------------------------------------------------------------------------------------------------------
 cc_index_dtl
 chk_dtl
 chk_info_dtl
 chk_xfer_dtl
 trans_dtl
 gst_info_dtl
 chk_event_dtl
 trans_archive_dtl
 tms_tbl_request_dtl
 chk_dispatch_dtl
 srm_ref_dtl
 mfd_check_opn
 vta_BaseDtl
 vta_base_baseDtl
 vta_subtotal_discount
 v_dtl
 v_get_chk_xfers
 v_get_sales_ttl
 v_R_discount_dtl
 v_R_kds_chk_dtl
 v_R_kds_dashboard_chk_dtl
 v_R_kds_dashboard_line_time
 v_R_kds_dashboard_order_time
 v_R_kds_qsr_chk_dtl
 v_R_kds_qsr_done_time
 v_R_kds_qsr_line_time
 v_R_kds_qsr_order_time
 v_R_kds_qsr_park_time
 v_R_kds_qsr_trk_chk_dtl
 v_R_kds_qsr_trk_done_time
 v_R_kds_qsr_trk_line_time
 v_R_kds_qsr_trk_order_time
 v_R_kds_qsr_trk_park_time
 mfd_v_dtl
(34 rows)

因为数据与订单表关联,数据量不可能太小,我们来列出每个表的数据量

用ruby拼装sql语句

 err =  %w(cc_index_dtl
     chk_dtl
     chk_info_dtl
     chk_xfer_dtl
     trans_dtl
     gst_info_dtl
     chk_event_dtl
     trans_archive_dtl
     tms_tbl_request_dtl
     chk_dispatch_dtl
     srm_ref_dtl
     mfd_check_opn
     vta_BaseDtl
     vta_base_baseDtl
     vta_subtotal_discount
     v_dtl
     v_get_chk_xfers
     v_get_sales_ttl
     v_R_discount_dtl
     v_R_kds_chk_dtl
     v_R_kds_dashboard_chk_dtl
     v_R_kds_dashboard_line_time
     v_R_kds_dashboard_order_time
     v_R_kds_qsr_chk_dtl
     v_R_kds_qsr_done_time
     v_R_kds_qsr_line_time
     v_R_kds_qsr_order_time
     v_R_kds_qsr_park_time
     v_R_kds_qsr_trk_chk_dtl
     v_R_kds_qsr_trk_done_time
     v_R_kds_qsr_trk_line_time
     v_R_kds_qsr_trk_order_time
     v_R_kds_qsr_trk_park_time
     mfd_v_dtl)

arr.each do |tb|
     puts "select count(*) from micros.#{tb};"
end
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
select count(*) from micros.cc_index_dtl;
select count(*) from micros.chk_dtl;
select count(*) from micros.chk_info_dtl;
select count(*) from micros.chk_xfer_dtl;
select count(*) from micros.trans_dtl;
select count(*) from micros.gst_info_dtl;
select count(*) from micros.chk_event_dtl;
select count(*) from micros.trans_archive_dtl;
select count(*) from micros.tms_tbl_request_dtl;
select count(*) from micros.chk_dispatch_dtl;
select count(*) from micros.srm_ref_dtl;
select count(*) from micros.mfd_check_opn;
select count(*) from micros.vta_BaseDtl;
select count(*) from micros.vta_base_baseDtl;
select count(*) from micros.vta_subtotal_discount;
select count(*) from micros.v_dtl;
select count(*) from micros.v_get_chk_xfers;
select count(*) from micros.v_get_sales_ttl;
select count(*) from micros.v_R_discount_dtl;
select count(*) from micros.v_R_kds_chk_dtl;
select count(*) from micros.v_R_kds_dashboard_chk_dtl;
select count(*) from micros.v_R_kds_dashboard_line_time;
select count(*) from micros.v_R_kds_dashboard_order_time;
select count(*) from micros.v_R_kds_qsr_chk_dtl;
select count(*) from micros.v_R_kds_qsr_done_time;
select count(*) from micros.v_R_kds_qsr_line_time;
select count(*) from micros.v_R_kds_qsr_order_time;
select count(*) from micros.v_R_kds_qsr_park_time;
select count(*) from micros.v_R_kds_qsr_trk_chk_dtl;
select count(*) from micros.v_R_kds_qsr_trk_done_time;
select count(*) from micros.v_R_kds_qsr_trk_line_time;
select count(*) from micros.v_R_kds_qsr_trk_order_time;
select count(*) from micros.v_R_kds_qsr_trk_park_time;
select count(*) from micros.mfd_v_dtl;

执行sql后发现这些表的数据量值得考虑

micros.v_R_kds_qsr_trk_order_time
micros.v_R_kds_qsr_trk_chk_dtl
micros.v_R_kds_dashboard_order_time
micros.v_R_kds_dashboard_chk_dtl
micros.v_get_sales_ttl
micros.v_R_discount_dtl
micros.v_dtl
micros.vta_base_baseDtl
micros.vta_BaseDtl
micros.chk_dispatch_dtl
micros.mfd_v_dtl

找到这些表中chk_seq = 187196的数据

用ruby拼装sql

arr2 = %w(micros.v_R_kds_qsr_trk_order_time
micros.v_R_kds_qsr_trk_chk_dtl
micros.v_R_kds_dashboard_order_time
micros.v_R_kds_dashboard_chk_dtl
micros.v_get_sales_ttl
micros.v_R_discount_dtl
micros.v_dtl
micros.vta_base_baseDtl
micros.vta_BaseDtl
micros.chk_dispatch_dtl
micros.mfd_v_dtl)
arr2.each do |tb|
     puts "select * from #{tb} where chk_seq = 187196;"
end
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
select * from micros.v_R_kds_qsr_trk_order_time where chk_seq = 187196;
select * from micros.v_R_kds_qsr_trk_chk_dtl where chk_seq = 187196;
select * from micros.v_R_kds_dashboard_order_time where chk_seq = 187196;
select * from micros.v_R_kds_dashboard_chk_dtl where chk_seq = 187196;
select * from micros.v_get_sales_ttl where chk_seq = 187196;
select * from micros.v_R_discount_dtl where chk_seq = 187196;
select * from micros.v_dtl where chk_seq = 187196;
select * from micros.vta_base_baseDtl where chk_seq = 187196;
select * from micros.vta_BaseDtl where chk_seq = 187196;
select * from micros.chk_dispatch_dtl where chk_seq = 187196;
select * from micros.mfd_v_dtl where chk_seq = 187196;

锁定目标

select * from micros.v_dtl where chk_seq = 187196;
>>>>>>>>>>>>>>>>>>
........
  187196 |       4 |    644527 | R        | \x00\x00\x80\x00\x00 |            |                 | @\x10\xe5[    |    1 | A           |        -1 |                  |                    |                 0 |        0 |       0 |       0 |          |              |             |                   |                   |                   |                   |                   |                   |                   |                   |                 |             |             |             |                             |                     |                 |                     |            |                |                   |                   |                   |                   |                   |                   |                   |                   |              |               |                                                  |                    |                               |                |                |                     |       |                        |                     |                    | 18628486            |                3 |             644527 |            |                |              |                   |                   |                   |                   |                   |                   |                   |                   |                  |                    |                       |                 |           |               |              |              |              |               |                |                  |                |            |                |               |                 |              |             |                  |                 |                 |                 |                 |                 |                 |                 |                 |                 |                  |                   |                  |                        |              |              |                 |                 |
  187196 |       1 |    644529 | T        | \x00\x00\xc0\x00\x00 |            |                 | ^\x10\xe5[    |    1 | A           |        -1 |                  |                    |                 0 |        0 |       1 |     450 |          |              |             |                   |                   |                   |                   |                   |                   |                   |                   |                 |             |             |             |                             |                     |                 |                     |          1 |                | F                 | F                 | F                 | F                 | F                 | F                 | F                 | F                 |              |             0 | \xa0\x00\x1b\x04\x00\x00\x00\x00\x06\x00\x00\x00 |                    |                               |                | F              |                   0 |       |                        |                     |                    |                     |                  |                    |            |                |              |                   |                   |                   |                   |                   |                   |                   |                   |                  |                    |                       |                 |           |               |              |              |              |               |                |                  |                |            |                |               |                 |              |             |                  |                 |                 |                 |                 |                 |                 |                 |                 |                 |                  |                   |                  |                        |              |              |                 |                 |
(7 rows)

可以看到有一条数据的R_ref = 18628486,大功告成!!

你可能感兴趣的:(sql)