SQL - help checking for bug

Status
Not open for further replies.
Hey all --

I've recently discovered a bug whereby no data is ever loaded into part of a data object which is subsequently written to the database. As this data is never loaded into that part of the object, our code sees "nulls" and changes those to "-1" to be written to the db. Again, this part of the object is put into a supplemental table, looked up by the "parent" record-id. So the only thing in this supplemental table appears to be "-1" in all 47 columns with the 48th column for the foreign key reference.

Can I construct a relatively simple SQL query which will return the id's of those rows which actually have something other than -1 in their fields?

(using Oracle if it matters...)
 

C0rinthian

Ars Tribunus Angusticlavius
6,858
Should any of the fields ever be -1? If you have a field that you know should never be -1, the SQL is straightforward:

Code:
SELECT
	record_id
FROM
	tablename
WHERE
	some_field <> -1;

If any of the fields could be -1, then you're just going to have more conditions in your where clause.

Code:
SELECT
	record_id
FROM
	tablename
WHERE
	some_field <> -1
	AND some_other_field <> -1
	...
	AND last_field <> -1;

This is all avoiding the question of "Why is the code turning null values into -1 in the first place?" as I assume that's not something that can be changed.
 
Yeah, well, obviously I could do the query "where field1 != -1 or field2 != -1 ..." etc... but I didn't want to have to type out all 48 field names :p Was hoping there was some non-obvious/tedious way to do it...

I want to find if any record has any field that's not -1, or, in other words, might have some viable data. Otherwise, I'm going to remove the 53 million rows and change the code so that it doesn't create effectively empty rows.

ORM? Hah! Its a "wish it was an ORM but we made it ourselves so it sucks" "API"
 

C0rinthian

Ars Tribunus Angusticlavius
6,858
[url=http:/civis/viewtopic.php?p=26866089#p26866089:2997j4s8 said:
zelmak[/url]":2997j4s8]Yeah, well, obviously I could do the query "where field1 != -1 or field2 != -1 ..." etc... but I didn't want to have to type out all 48 field names :p Was hoping there was some non-obvious/tedious way to do it...

I don't think there's any way to avoid that in straight SQL.
 

Fristie Blade

Ars Tribunus Militum
1,722
Subscriptor++
In oracle, you can get column names from tables (if you have rights to query that)
SELECT column_name FROM user_tab_cols WHERE table_name = 'myTableName'

Then tune your query to something like
SELECT ' AND ' || column_name || ' <> -1' FROM user_tab_cols WHERE table_name = 'myTableName'

Then copy the resulting grid to a new SQL window.
Filter out unwanted columns and remove all CR/LF or whatever end-lines you use.

There is your restriction to be used.
 

AndrewZ

Ars Legatus Legionis
11,860
I don't think there's any way to avoid that in straight SQL.
While this is true there are a few possible shortcuts. In Oracle you could Describe the table, then copy and paste the field names into notepad/textpad and formulate the query. On in SQL Server Management Studio you could export the first 200 rows to a text file, read the column names, copy/paste/edit into a query.
 
[url=http:/civis/viewtopic.php?p=26866849#p26866849:2zrf9co2 said:
wallinbl[/url]":2zrf9co2]How the hell do you get 53 million rows of bad data?

The original programmer(s?) of this feature didn't test it? Created a one-to-one foreign key and, by virtue of the implementation, made the supplemental table mandatory regardless of the emptiness of the records. And most of our code uses "-1" as a sentinel which means "null" ... so, you still have to check for nullness by checking return values against -1 (even strings... grrr), but you don't have to worry about null pointer exceptions.

Yeah, super crappy design without any actual thought...
 
[url=http:/civis/viewtopic.php?p=26867783#p26867783:2ts6av0t said:
Fristie Blade[/url]":2ts6av0t]In oracle, you can get column names from tables (if you have rights to query that)
SELECT column_name FROM user_tab_cols WHERE table_name = 'myTableName'

Then tune your query to something like
SELECT ' AND ' || column_name || ' <> -1' FROM user_tab_cols WHERE table_name = 'myTableName'

Then copy the resulting grid to a new SQL window.
Filter out unwanted columns and remove all CR/LF or whatever end-lines you use.

There is your restriction to be used.

I'll probably use this... thanks!
 

Nazgutek

Ars Scholae Palatinae
1,298
If you're working within SQL Server Management Studio (or at least have it installed), a quick way to get all the columns is to right click the table and select one of the 'Script Table As...' options 'To New Query Window'/'To Clipboard'. Although you'll need to copypasta bits in and abuot, it's three seconds of clicking without having to remember anything.
 

Fristie Blade

Ars Tribunus Militum
1,722
Subscriptor++
[url=http:/civis/viewtopic.php?p=26874739#p26874739:1cgoy293 said:
Nazgutek[/url]":1cgoy293]If you're working within SQL Server Management Studio (or at least have it installed), a quick way to get all the columns is to right click the table and select one of the 'Script Table As...' options 'To New Query Window'/'To Clipboard'. Although you'll need to copypasta bits in and abuot, it's three seconds of clicking without having to remember anything.

On sql server, I d rather do
select ' and ' + c.name + ' <> -1' from sys.columns c where object_name(c.object_id) = 'myTableName'

I've got too many years of fine-tuning my expertise in professional laziness efficiency ;)
 
[url=http:/civis/viewtopic.php?p=26869331#p26869331:24ajlfi2 said:
zelmak[/url]":24ajlfi2]
[url=http:/civis/viewtopic.php?p=26867783#p26867783:24ajlfi2 said:
Fristie Blade[/url]":24ajlfi2]In oracle, you can get column names from tables (if you have rights to query that)
SELECT column_name FROM user_tab_cols WHERE table_name = 'myTableName'

Then tune your query to something like
SELECT ' AND ' || column_name || ' <> -1' FROM user_tab_cols WHERE table_name = 'myTableName'

Then copy the resulting grid to a new SQL window.
Filter out unwanted columns and remove all CR/LF or whatever end-lines you use.

There is your restriction to be used.

I'll probably use this... thanks!

So, I used this, and it worked well for my needs.

I went to use it on a second, similarly suspicious table which also seemed to be filled with empty data. However, three of the fields were strings instead of numbers so I had to change things up a bit.

Code:
select * from example_table where year <> -1 OR sample_size <> -1 OR survey_name is not null;

  YEAR  SAMPLE_SIZE  SURVEY_NAME
------  -----------  -----------
    -1           -1         null
    -1           -1         null
    -1           -1         null
    -1           -1         null
    -1           -1         null
    -1           -1         null
    .             .            .

However, no matter what I did, I found that I was getting all rows back no matter how I tweaked the query. To my horror, I realized what happened: When the code was written, the programmers used simple string-concatenation to build their queries:

Code:
StringBuilder sb = new StringBuilder();
sb.append('INSERT INTO EXAMPLE_TABLE ( YEAR, SAMPLE_SIZE, SURVEY_NAME ) VALUES ( ");
sb.append(survey.getYear());
sb.append(", ");
sb.append(survey.getSampleSize());
sb.append(", ");
sb.append("'");
sb.append(survey.getSurveyName());
sb.append("'");
sb.append(")");
query = sb.toString();

Anyone who's coded in Java knows that most string building routines convert the value null to the string "null". My predecessors have proceeded to fill yet another, parallel table with even more worthless information: -1's and "null" strings.

:sigh:
 

Fristie Blade

Ars Tribunus Militum
1,722
Subscriptor++
Well, if you have number columns with -1 representing nulls and text columns with 'null' representing null, you could adapt the meta query to generate a statement for them based upon the columns data type.

Code:
 SELECT 
  'and nullif(' || column_name || ', ' || case when data_type in ('VARCHAR', 'VARCHAR2', 'CHAR') then '''null''' else '-1' end || ') is not null'
  end
FROM 
  user_tab_cols 
WHERE 
  table_name = 'yourTableName'

It will convert -1 and 'null' values to real nulls and then you test with a classic is null
 
Status
Not open for further replies.