0

I have this simple query:

  var select = "select * from mainTable where username = '" + username + "'" 

where I am then checking this:

 for (var i = 0; i < rows.length; i++) {
           if (rows[i]["CHILDTABLEID"] != undefined) {
              //here I need to check if childtable2id has been created within that table yet
   }
  }

I need to check if childtable2id has been created within that table yet. How is this possible. I was hoping it wouldn't require a modification to my query, and I could just do something along the lines of:

if (rows[I]["CHILDTABLE2ID"]) //check if its been created????

if you get where im trying to go with this.

thanks for the help

Gianluca
  • 900
  • 8
  • 27
  • Can you provide more detail as to what you're trying to achieve? Also, are you trying to see if the table _has a specific column defined in the schema_ or _has a value set in that column_? – Anthony Forloney May 31 '21 at 19:25
  • I am basically querying my table, and then wanting to check if a column has been created yet. If the column does not exist in the table, then right now it will just present an error, but I want to be able to add the column if it doesn't exist. Not sure if that helps – Gianluca May 31 '21 at 19:26
  • You might be interested in, https://stackoverflow.com/questions/133031/how-to-check-if-a-column-exists-in-a-sql-server-table to query against the schema for a particular column rather than getting all columns from a `SELECT` – Anthony Forloney May 31 '21 at 19:29

1 Answers1

0

If your front end knows it wants CHILDTABLE2ID then it should select it:

SELECT CHILDTABLE2ID, ... FROM mainTable WHERE ...

One of two things* happens:

  • it works
  • it errors because the column doesn't exist

Your front end will be able to be coded so it can tell the difference

*well, of course it could error for other reasons.. but the front end will still be able to be programmed to tell the difference

Caius Jard
  • 72,509
  • 5
  • 49
  • 80