Appian Community
Site
Search
Sign In/Register
Site
Search
User
DISCUSS
LEARN
SUCCESS
SUPPORT
Documentation
AppMarket
More
Cancel
I'm looking for ...
State
Not Answered
Replies
9 replies
Subscribers
7 subscribers
Views
4932 views
Users
0 members are here
Share
More
Cancel
Related Discussions
Home
»
Discussions
»
General
We have a need to do a select query. The "WHERE" of the SQL statement
scotte
over 10 years ago
We have a need to do a select query. The "WHERE" of the SQL statement will need to be dynamic.The reason is that this is a "search" that our users are doing. They may select to search on 1 to 5 fields. In other words, the columns in our where clause will vary depending on the user situation. When using the Query Database smart node, we are struggling to find a way to accomplish this. Is there a way to get this done? Thanks!...
OriginalPostID-131608
OriginalPostID-131608
Discussion posts and replies are publicly visible
0
Tim
Certified Lead Developer
over 10 years ago
Try using queryentity() or queryrecord() instead. These functions provide much more powerful query options and allow for dynamic where conditions. See the examples here for a starter:
forum.appian.com/.../Query_Recipes.html
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
Chris
over 10 years ago
I've accomplished this in the Query DB smart node by including all columns in the WHERE clause with 'LIKE', such as:
..WHERE Column1 LIKE ac!col1Filter AND Column2 LIKE ac!col2Filter AND Column3 LIKE ac!col3Filter AND Column4 LIKE ac!col4Filter AND Column5 LIKE ac!col5Filter
You can populate any search variables (one to all) with text, leaving ones not entered blank.
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
scotte
over 10 years ago
We are on release 7.2. Can someone give me an example of building a Select Query where using queryrecord or queryentity. We have 5 fields that may be searchable. The user can search on 1 to 5 of these fields. How is this done? So the WHERE clause will be dynamic... 1-5 fields. Thanks!
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
scotte
over 10 years ago
csteward... You said you did this. This is not working as you say. If you leave blank we are getting NO results? We would like to use the way you mention but no luck. In fact if blank I put the value "%" in the search field. This does not give us the correct results ethier. When using % there it only returns records if a value is in field.?
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
Chris
over 10 years ago
Sorry, you will need "%%" in your inputs. Using my SQL above, you can set your Query DB node inputs to ="%" & pv!Col1Filter & "%" - etc, and that should work for you.
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
scotte
over 10 years ago
Yes, that is what we thought. But when we put in a % as a value, you only get returned records that have something in that field. Try it yourself? So if I have 10 records and 3 of the records Last_Name is null and WHERE FIRST_NAME LIKE ac!acFirstName and acFirstName has a value of % I only get 7 records returned. I should get all 10?
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
Chris
over 10 years ago
Ah yes, in my use case we do not have null situations. LIKE will not return records with null values. To work around that, instead of "Column4 LIKE ac!col4Filter" you should be able to use:
Oracle: COALESCE(Column4, ' ') LIKE ac!col1Filter
SQL: IsNull(Column4, ' ') LIKE ac!col1Filter
.. with your ac! input for col1Filter set to: ="%" & pv!Col1Filter & "%"
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
scotte
over 10 years ago
Thanks, I think this is working. We are using Oracle. Confirming: So in the Oracle sql above you are saying to make the col1Filter variable = to % ? Also, is that a space between the two quotes as in ' '? I'm trying to understand the COALESCE how it works? And what if some of the fields we are searching on are numeric or dates types? Thanks!
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
Chris
over 10 years ago
Great! On your form or search selection, you can set whatever is entered to variables (normal text, no '%'). Then on the ac! Data Inputs for the Query DB node, set as:
="%" & pv!col1Filter & "%"
Where pv!col1Filter is the exact text you would like to filter on, surrounded by 2 "%" signs.
COALESCE() takes 2 arguments and returns the first that is not null - so if the row is null, it will return whatever you enter as the second argument (basically replacing null values during the search) - or it will return the actual data. I do use a space in ' ' for text data. I have tried no space and get better results with a space. Also, for other data types you can use convert to string, such as "COALESCE(to_char(Column3),' ').
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel