Solved

Pervasive SQL query

Posted on 2014-07-18
4
232 Views
Last Modified: 2014-07-18
Suppose I am working on a people table.
I want to return the names of people.
I'm looking to prompt the user 3 times (don't ask... long story).

Based on the results of the prompts I'll return:
The names of everyone (Prompts 1, 2 and 3 are 0)
or, if Prompt1 <> 0 those who are Prompt1 years old.
Unless Prompt2 <> 0 then I want all names that are >= Prompt1 and <= Prompt2.
Unless Prompt3 <>0.  Then it could be >= Prompt1 and <= Prompt2 AND = Prompt3.

Answers would be, perhaps:

Everyone
or
Everyone who is  35
or
Between 35 and 45
or
Between 35 and 45 AND all those age 60.

This is actually a construction accounting query that is using cost codes... I just thought that people and ages would be a simpler example to provide.

Using Pervasive 11
0
Comment
Question by:classnet
  • 2
  • 2
4 Comments
 
LVL 28

Expert Comment

by:Bill Bach
ID: 40204633
I would expect this to be a fairly simple extension to this issue:
    http://www.experts-exchange.com/Database/MS-SQL-Server/Q_28478909.html

Extending either the accepted solution (by adding terms to the WHERE clause that exactly mirror your criteria above) or the unaccepted solution (where you could use IF statements to create the correct WHERE clause based on the input values) would work.
0
 

Author Comment

by:classnet
ID: 40204677
I am doing this through ODBC and thus cannot access the database to create stored procedures.
0
 
LVL 28

Accepted Solution

by:
Bill Bach earned 500 total points
ID: 40204868
You mention "construction" -- are you using Timberline?  Timberline uses a custom ODBC driver (i.e. NOT the Pervasive one), so I would have to agree with you -- stored procedures are not likely possible.  However, with any other database using the PSQL ODBC drivers, you should be able to issue the CREATE statement through ODBC just fine.  In fact, the PSQL installer does this very thing when it creates the DemoData and PervasiveSysDB databases using the "pvddl" utility.  Just put the CREATE statement into a text file and pvddl does the rest.  (If you need to modify the proc, issue a DROP PROCEDURE statement first in the file.)

The logic you need to do this from the WHERE clause is complicated, but not impossible.  You will have two components to each of your restrictions -- the restriction itself and the selection criteria.  Here's an example:
WHERE -- First criteria
((Prompt1=0 AND Prompt2=0 AND Prompt3=0) AND (1=1))
OR  -- Second Criteria
((Prompt1<>0 AND Prompt2=0 AND Prompt3=0) AND (age=35))
OR  -- Third Criteria
((Prompt1<>0 AND Prompt2<>0 AND Prompt3=0) AND (age >= Prompt1 AND age <=Prompt2))
OR  -- Fourth Criteria
((Prompt1<>0 AND Prompt2<>0 AND Prompt3<>0) AND ((age >= Prompt1 AND age <= Prompt2) OR age=Prompt3))

Obviously, you can tweak things and try to optimize if you wish -- but sometimes brute force is the simplest to write AND understand later.

Note that PSQL probably won't do a very good job of optimizing this query, and this query will likely run a full table-scan to look at every record.  If your data set is large, it will take a while.
0
 

Author Closing Comment

by:classnet
ID: 40204926
You are correct... we are using Timberline.
I will attempt to use your soltion
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now