Are there any SQL statement generators/examples?
If you have somewhat infinate amount of querying you can do - combination of certain fields matching values... how can you construct it without writing so many separate SELECT statements? Is there any SQL generator code which you give it a column name and X number of values and it constructs it? How does it handle different field types?
[345 byte] By [
smiles78a] at [2007-11-27 11:06:54]

# 1
> If you have somewhat infinate amount of querying you
> can do - combination of certain fields matching
> values... how can you construct it without writing so
> many separate SELECT statements?
You write dynamic SQL (check out Google)! All reporting tools or search engines of course cannot predict all the possible searches that might be submitted so the SQL or search statements are constructed on-the-fly.
> Is there any SQL
> generator code which you give it a column name and X
> number of values and it constructs it? How does it
> handle different field types?
There are lots of SQL "generators" if that's what you want to call them. There's lots of tools for lots of stuff. For example, Entity EJB's generate SQL automagically. Again, I'd search Google. Start with this:
http://www.google.com/search?q=java+dynamic+sql+jdbc&ie=utf-8&oe=utf-8&rls=org.mozilla:en-US:official&client=firefox-a
# 2
Thanks. EJBs aren't going to be used though.
I guess the solution would be some type of object which has fields for your column, value you want to match?
# 3
> Thanks. EJBs aren't going to be used though.
Good, I don't recommend them for most requirements anyway.
> I guess the solution would be some type of object
> which has fields for your column, value you want to
> match?
No, that would be your data model solution. You still need to write the code to acess the database so you can populate the data model with data. i.e. you need to write some code that dynamically creates SQL statements, parameterize it as a function, whatever.
# 4
> i.e. you need
> to write some code that dynamically creates SQL
> statements, parameterize it as a function, whatever.
Yes, that is what I realized and I'm doing that now. I also came up with taking into account that the query could be anything -- =, <> , <, etc so I made a field that can be set accordingly. Kind of a pain to write, but it's done now.
I can't believe there isn't already some code out there to do this!
# 5
> I can't believe there isn't already some code out
> there to do this!
Sure there is. Ever heard of Crystal Reports?