0

So, this is for debugging only. Its for looking at someone else's mess. Its not production code, it'll never see production, its not part of a wider scheme to try to do anything. Its purely debugging only.

I want to be lazy.

Overall query is something like this

SELECT 
    tableA."Col1"
    ,tableB."Col2"
    ,tableC."Col5"

from public."BigLongTableName" as tableA
join public."BigLongTableName2" tableB on tableB.colA = tableA.colB
join public."BigLongTableName3" tableC on tableC.colX = tableB.colY

But, due to the awful layout, not all relationships carry through and there is a web of relationships across the databases that filter out some data.

I want to be able to comment out tables without having to comment out the columns returned to GUI (they'd just not appear), so something like

SELECT 
    tableA."Col1"
    ,snazzySyntaxThatOnlyDisplaysIFHere(tableB."Col2")
    , snazzySyntaxThatOnlyDisplaysIFHere(tableC."Col5")

from public."BigLongTableName" as tableA
join public."BigLongTableName2" tableB on tableB.colA = tableA.colB
--join public."BigLongTableName3" tableC on tableC.colX = tableB.colY

So, because I didn't join BigLongTableName3, tableC alias doesn't exist. Therefore the snazzySyntax would save me having to comment out that line.

Anyone any ideas off the top of their head?

I tried exists on data within tableC(), didn't like it, as tableC isn't yet declared - which makes sense. Don't know if I can make exists work directly on tableC, so tried below, no joy either - not sure if it'd work here even if it I got it right:

Select
    tableA."Col1"
    ,if(OBJECT_ID(tableC, 'U') is not NULL); BEGIN tableC."Name"; END;
Amiga500
  • 1,258
  • 1
  • 6
  • 11
  • Sounds like you are looking for `LEFT [OUTER] JOIN` instead of your `[INNER] JOIN`. – Thorsten Kettner Apr 04 '22 at 10:53
  • Hi Thorsten, Thanks but not quite. Some of the relationships appear uncontrolled (i.e. one relationship value returns many) - and I need to step around the DBs in various means and ways to see how the linkages could be (re)constructed. So I'm adding and dropping joined tables all the time and getting annoyed with commenting out lines (needed to keep returned window under some sort of size control)! As said - I **AM** being lazy :D – Amiga500 Apr 04 '22 at 11:12
  • Okay, I got it. As a query is always build on existing tables, your best bet may be a dynamic query, i.e. two steps: query the system table in order to know which tables exist, then use this result to build the final query. I suppose in PostgreSQL this is done with a stored function. – Thorsten Kettner Apr 04 '22 at 11:28
  • See https://stackoverflow.com/questions/20582500/how-to-check-if-a-table-exists-in-a-given-schema –  Apr 04 '22 at 13:01
  • You could build up a query string to only include tables which exist. –  Apr 04 '22 at 13:02
  • Yow want to *comment out tables without having to comment out the columns **(from those tables**)* (emphases mine). You cannot do that. Commenting the table means the columns do not exist in the query and you cannot reference them. If you comment the table you must either comment the columns as well or put those column names in single quotes - thus making them string place holders but result will have the column name not a value. – Belayer Apr 04 '22 at 18:19
  • Cheers folks - thought it was a bit of a long shot. Looking on the bright side, it'll give me something to complain about! :-D – Amiga500 Apr 05 '22 at 08:56

0 Answers0