免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1785 | 回复: 5
打印 上一主题 下一主题

如何取出表名? 搞了一上午,没搞定 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-07-07 12:10 |只看该作者 |倒序浏览
在一个SQL文件中取出所有相关的表名,sql非常复杂,带不定层的嵌套语句。

写了个这样的,但遇到嵌套的就不正常,哪位大侠指导一下!


  1. sed -n -e '
  2. /[Pp]rompt/ d
  3. /^$/ d
  4. s/^[ \t]\{1,\}//g
  5. s/[ \t]\{1,\}$//g
  6. s/--.*//g
  7. /^\/\*/ d
  8. /^\*/ d
  9. /^\\\*/ d
  10. s/[\(\)]/ /g
  11. s/[\n]/\t/g
  12. /[Ii]nsert/ s/[Ii]nsert [Ii]nto\(.*\)/\1/p
  13. /[Ff]rom/ s/[Ff]rom \(.*\) [Ww]here/\1/p
  14. /[Ff]rom/ s/[Ff]rom \(.*\) [Mm]inus/\1/p
  15. /[Uu]date/ s/[Uu]pdate \(.*\) [Ss]et/\1/p
  16. ' sqlfile

复制代码


$ cat sqlfile
.....
.....
Insert Into Asset_Classifications_Dc025_St
         (Asst_Id,
          Audit_Skeleton_Ind,
          Audit_Valid_From_Date,
          Audit_Valid_To_Date,
          Legal_Entity_Code,
          Id,
          Rac_Id,
          Rtp_Code_Archived_By,
          Rtp_Code_Created_By,
          Effective_From,
          Effective_To,
          Ract_Code,
          Rac_Code)
         Select Distinct
                ac.Asst_Id,
                Nvl(ac.Audit_Skeleton_Ind, 'N'),
                ac.Audit_Valid_From_Date,
                To_Date('9999-12-31', 'yyyy-mm-dd'),
                ac.Booking_Center,
                ac.Id,
                Decode(Rac.Code, 'PUT',  (Select b.ID From ASSET_CLASS_DC025_TEMP_USE b Where b.CODE = '029' and b.LEGAL_ENTITY_CODE = ac.BOOKING_CENTER),
                                 'CALL', (Select b.ID From ASSET_CLASS_DC025_TEMP_USE b Where b.CODE = '030' and b.LEGAL_ENTITY_CODE = ac.BOOKING_CENTER),
                                 ac.Rac_ID),
                ac.Rtp_Code_Archived_By,
                ac.Rtp_Code_Created_By,
                ac.Audit_Valid_From_Date,
                Decode(ac.Audit_Current_Ind,
                       'Y',
                       To_Date('9999-12-31', 'YYYY-MM-DD'),
                       ac.Audit_Valid_To_Date),
                Case When (In_Dc_Recon_Ind = 'Y' and Ract.Code in ('FXOPT', 'MH', 'OPTYPE')) Then 'SECTYPE' Else Ract.Code End,
                Decode(Rac.Code, 'PUT', '029', 'CALL', '030', Rac.Code)
           From Asset_Classifications_V131 Ac,
           (Select code, booking_center, id, ract_id
                   From (Select code, booking_center, id, ract_id,
                                Row_Number() Over(Partition By booking_center, id Order By code) Rn
                           From Ref_Asset_Classifications_V131
                          Where Audit_Current_Ind = 'Y')
                  Where Rn = 1) rac,
           (Select code, booking_center, id
                   From (Select code, booking_center, id,
                                Row_Number() Over(Partition By booking_center, id Order By code) Rn
                           From Ref_Asset_Class_Type_V131
                          Where Audit_Current_Ind = 'Y')
                  Where Rn = 1) ract
          Where ac.booking_center = rac.booking_center(+)
          and rac.booking_center = ract.booking_center(+)
          and ac.rac_id = rac.id(+)
          and rac.ract_id = ract.id (+)
          and NOT EXISTS
              (select 1
                 from Ref_Asset_Classifications_V131 rac, Ref_Asset_Class_Type_V131 ract
                where rac.ract_id            = ract.id
                  and rac.audit_current_ind  = 'Y'
                  and ract.audit_current_ind = 'Y'
                  AND ract.code              = 'SYMBOL'
                  and rac.id                 = ac.rac_id)
          and ac.Audit_Valid_From_Date <>
                Nvl(ac.Audit_Valid_To_Date,
                    To_Date('9999-12-31', 'YYYY-MM-DD'))
            And ((In_Dc_Recon_Ind = 'N' And
                ac.Audit_Valid_From_Date <= In_Rpt_Date And
                Nvl(ac.Audit_Valid_To_Date,
                      To_Date('9999-12-31', 'YYYY-MM-DD')) > In_Rpt_Date) Or
                (In_Dc_Recon_Ind = 'Y'));

      Commit;
......
.....

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
2 [报告]
发表于 2006-07-07 14:13 |只看该作者
没看懂

grep -io '[^ ]*Asset_Class[^ ]*'  file
不行么?

论坛徽章:
0
3 [报告]
发表于 2006-07-07 14:20 |只看该作者

回复 1楼 kelvin 的帖子

其实看不太懂sed
不过觉得似乎主要问题在于找规律,而不是书写命令

比如,似乎所有含有_Asset_的都是表名...

论坛徽章:
0
4 [报告]
发表于 2006-07-07 14:23 |只看该作者
hehe,写了一半干了些其它事情,结果waker大虾已经发贴了:)

论坛徽章:
0
5 [报告]
发表于 2006-07-07 15:06 |只看该作者
楼主的意思是,表里有嵌套,不完全符合Asset_Class模式吧。1!!

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
6 [报告]
发表于 2006-07-07 16:07 |只看该作者
特征抓准了,正则表达式写好了很多问题会变得很容易解决。上面waker兄的代码就是个很好的例子。下面给出的仍然是一个偷懒的办法:
  1. perl -ne 'print "$1\n" if (/(?:Insert Into|From) (\w+)/ig)'
复制代码

只要Insert into 和表名同行,且From和表名同行
就可以。
如果不同行,就还得改改。
上面perl代码等价于GNU grep的:
  1. grep -Po '(?i)(?<=insert into )\w+|(?<=from )\w+'
复制代码

[ 本帖最后由 woodie 于 2006-9-8 18:28 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP