Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
SSurfer
Starting Member
1 Post |
Posted - 2002-05-26 : 16:32:46
|
| Hi,I'm trying to build an SQL UNION querie depending on the options that the users choose.The user have n options and he can choose 1, 2 or the n options. SQL1 = "Select * from table1"UNION1 = "UNION"SQL2 = "Select * from table2"UNION2 = "UNION" SQLn = "Select * from tablen"FinalSQL = SQL1 & UNION1 & SQL2 & UNION2 & SQL1nNow imagine that the user select only the 1 option, or the first and the last, or the 2 only......Who to build this? I dont want to build an IF statment because n could be 20!Is any way to do a dinamic querie for executing the right choice of the user.Thank's for the time.S |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2002-05-26 : 20:33:26
|
| Can you change the definition of the database so that instead of n tables you have 1 with an identifier.As all the tables have the same structure this would seem natural and then the problem becomes trivial.You could create a view to combine the tables to have the same effectselect tid = 1, * from table1unionselect tid = 2, * from tablen....the query then becomesselect * from tbl where tid in (options)you would have to build this as a dynamic string or if it's a comma delimitted string use a like operator.==========================================Cursors are useful if you don't know sql.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|
|