• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 557
  • Last Modified:

Re Arranging SQL columns

Hi,

I have a window with selection criteria dw and resultset dw. When user enters some values in selection criteria dw fields, then i am forming the where clause in retrieve button clicked event, and setting the same to the resultset dw and then retreiving the result set.
Now the user requirement is...  
Example :
1)
If he enters City and Birthdate fileds in selection criteria then, these columns should come first in the resultset and rest should follows . I.e   City   BirthDate   Name   Phone   State ....
2)
If he enters state and phone then the resultset should be  State  Phone  City  Birthdate Name Etc...

I have all these columns in the resultset, i need to rearrange these columns as per selection criteria fields.


Thanks in Advance
Srinib
 
0
srinib
Asked:
srinib
  • 3
  • 3
1 Solution
 
namasi_navaretnamCommented:
Method 1)

Build the entire sql dynamically. Not just there where clause. This was you place the selected columns on top. Then use sytanfromsql, create functions to create the datawindow.

Method 2)
Use GetSQLSelect, SetSQLSelect functions. Use GetSQLSelect to obtain currect sql. Modify current sql so that selected columns are on the top.

Use Modify to set column headers etc.

If you make it a grid style dw then user can re-arrage themselves.

regards-
0
 
srinibAuthor Commented:
Hi,

I have all the selected fields in the array. Now i need to rearrange these fields in my original sql string.

Example:
I have to selected fileds in array i.e ls_array[1]= 'Address.City' and ls_array[2] = 'Address.State'

My original SQL is as follows...i have this in one string

Select address.name,address.lname,address.city,address.state,address.phone
where address.city = 'NewYork' and address.state = 'NY'

Now i need all the fileds which are there in array should come first and rest should come next.
Final SQL should be like this..
select address.city,address.state,address.name,address.lname,address.phone
where address.city = 'NewYork' and address.state = 'NY'

If i can get the above final sql then i can prepare the dynamic dw based on that.

So, please give me the logic for repositioning sql columns with the array columns.

Thanks
Srinib
0
 
namasi_navaretnamCommented:
Psedo-code: If using PFC: Just an idea. Refine it further as needed.

string ls_sql, ls_cols
long ll_ub
n_cst_string       lnv_string

ls_sql = dw_1.GetSqlSelect()
ll_ub = upperbound(ls_array)
ls_cols = ' '
ls_sql = lnv_string.of_GlobalReplace(ls_sql, 'from' , ', from ')

for ll_i = 1 to ll_ub
   ls_sql = lnv_string.of_GlobalReplace(ls_sql, ls_array[ll_i] + ',' , ' ')
   ls_cols = ls_cols + ls_array[ll_i] + ', '
next

ls_cols = left(ls_cols, len(ls_cols) - 2)
ls_sql = lnv_string.of_GlobalReplace(ls_sql, 'select ' , 'select ' + ls_cols)

messagebox("debug", ls_sql)

Debug it. This is the basic idea. Modify as needed still you get the right sql.

regards-


0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
srinibAuthor Commented:
Hi Navaretnam,

I am not using PFCs, can you please give me the user defined global function.

Thanks again
Srinib
0
 
srinibAuthor Commented:
Thanks Navaretnam.. I have written global function for replacing...
Your basic idea helped me a lot in this..

Still i have one doubt... why these line required?

ls_sql = lnv_string.of_GlobalReplace(ls_sql, 'from' , ', from ')
ls_cols = left(ls_cols, len(ls_cols) - 2)

Thanks again
Srinib
0
 
namasi_navaretnamCommented:

// This line is to add a comma beside all column names.
ls_sql = lnv_string.of_GlobalReplace(ls_sql, 'from' , ', from ')

// This line is to remove comma at the end.
ls_cols = left(ls_cols, len(ls_cols) - 2)

Let me know if this does not makes sense.
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now