tags:

views:

13

answers:

2

I have a list of about 2,300K rows of bad data that I'd like to delete from my database. Is there a way that I can delete all of these rows using a single sql statement? I can 'WHERE IN' but the issue is that these values are not quoted and fail. Thanks

A: 

CSV? Could you instead make a list of the good rows, drop the table, and insert the good rows?

lod3n
Sure, I guess that's also an option. I didn't think of that. :)
Thinking about it now... I don't know how I'd do that. How would I distinguish between the good rows and bad? I'd still have to use the CSV list to get the good rows, no?
A: 

Use SQL and a little editor regex-fu:

  • Use excel or whatever to to get the list of keys you want to delete.
  • Copy that list into your favorite text editor. (Ultraedit, editplus, notepad++, heck even pfe)
  • Search replace string: \n => ',' (newline becomes quote comma quote)
  • Add a quote to the beginning and end of the list, surround by parenthesis, and stick in your WHERE IN clause.
  • Good to go.
Byron Whitlock
Thanks Byron. This sounds like a winner to me. I was using excel but wasn't aware of the search/replace \n.