3

I have a very large SQL query. I am trying to scrape tables names using javascript out of SQL query. (I provide SQL query in a text file as input)

  select t1.name, t1.id, t1.address, t2.units,t2.sale
  from SCHMEA1.candy_customers_azim_056 as t1
  inner join SCHEMA1.candy_sales_history_set t2
    on (t1.custid = t2.ORIGTN_ACCT_NO)

What could be the regular expression to achieve something like this ?

Tables:
candy_customers_azim_056 as t1
candy_sales_history_set t2
4
  • 4
    Have you tried anything yet? Have you put your example text into regex101.com to see if you can work it out that way? Are you looking for just the regex, or are you looking for something that can help you to get the alias' to begin with? Commented Sep 5, 2018 at 18:40
  • Maybe like this: candy_[a-z0-9_]*\s(as\s)?t[0-9] Commented Sep 5, 2018 at 18:41
  • @Ajaypayne I have been trying something like this regex101.com/r/73a5GA/6 Commented Sep 5, 2018 at 18:55
  • Do you consider queries like: select * from table1, table2? Commented Sep 5, 2018 at 19:32

2 Answers 2

4
(from|join)\s+\w+\.((\w*)[as ]+(\w+))

should return what you're looking for in the second capture group.

Sign up to request clarification or add additional context in comments.

2 Comments

this is helpful
What about next case: left join candy_sales_history_set4?
0

You may try this one as well:

/(from|join)\s+(\w+\.)?(\w+)(\s+as)?(\s+\w+)?/gmi

You can see examples here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.