Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL and Parameter Sets

Posted on 2000-04-05
12
Medium Priority
?
188 Views
Last Modified: 2010-04-04
Greetings,

I have a table with three numeric fields and I am trying to return a result set based specific values of one of these fields.

e.g.

Table

FIELD1
FIELD2
FIELD3

SQL

SELECT * FROM table
WHERE (CAST(FIELD3 AS CHAR(2))IN :Parameter)

I can pass through a single value to the :Parameter looking like this (10) and it works fine. If I pass through (10,20) I get an empty result set. I have tried all the various forms of quotes and nothing works.

Please help. This is urgent.

Thanks,

Robert Lancaster
0
Comment
Question by:RLancaster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +4
12 Comments
 

Expert Comment

by:ShadowFax
ID: 2686570
Hi Robert,

Why not just define the parameter as a numeric and use the following statement:
SELECT * FROM TABLE WHERE FIELD3 = :Parameter
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2686573
hi rlancester,

you can't use a set as parameter,
you've to build the query with or(s) in your where clause for each value un the set.

meikl
0
 
LVL 9

Accepted Solution

by:
ITugay earned 600 total points
ID: 2686636
Hi all,
meikl;)

Yes, it doesn't work, Meikl is right, I tried it before with all variants, without any success. There are only way to do is:

Query1.Close;
Query1.SQL.Clear;
Query1.SQL.Add('select * from table where field in ('+**your set**+')';
Query1.Open;
....
----
Igor.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 17

Expert Comment

by:geobul
ID: 2686641
Hi,

  You can use empty SQL property at design time.
  When you need to call the query simply build SQL statement at run time instead of using parameters:

var
  Parameter : String;
begin
  ...
  Parameter := '(10,20)';
  ...
  Query1.SQL.Clear;
  Query1.SQL.Add('SELECT * FROM table WHERE (CAST(FIELD3 AS CHAR(2))IN');
  Query1.SQL.Add(Parameter);
  Query1.Open;
  ...
end;

Regards, Geo
0
 
LVL 17

Expert Comment

by:geobul
ID: 2686654
Well done Igor ;)  and faster :(
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2686748
yup of course,

if you build up the statement on the fly
you can do it as igor commented.

meikl
0
 
LVL 2

Expert Comment

by:Felixin
ID: 2686923
Well, hello

What I use to do is to construct the skeleton of the SQL Query like a format string like this

MyQueryString = 'SELECT ListFields
FROM MyTable
WHERE Id IN %s';

When I want to run the query I make this

MyQuery.SQL.Text := Format (MyQueryString, [MyParameter]);

In my parameter you can make a call to a function converting your set into a string

function SetToString (MySet : TMySet) : string;

returning each member of the set separated by comma and the whole thing between parenthesis.
0
 
LVL 2

Expert Comment

by:kubeerja
ID: 2688440
try this if you are using MSSQL

put this in your query sql string :

declare @S varchar(50)
set @s= :xParam
set @s='SELECT * FROM table WHERE FIELD3 IN ' + @s
exec(@s)

define your parameter xParam as input string in the query params

then run your code

procedure TForm1.Button1Click(Sender: TObject);
begin
 with Query1 do
 begin
  ParamByName('xParam').AsString:='(10,20)' ;
  open ;
 end ;
end;
0
 
LVL 1

Author Comment

by:RLancaster
ID: 2688787
thanks!
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2689406
Felixin,
Good idea to use text formatting route.
-----
Igor.
0
 
LVL 2

Expert Comment

by:Felixin
ID: 2689540
Thank you, Igor

It's not mine. I've learnt it from a consultant here in my company.

Sometimes it helps to make the code easier to read.

Felixin
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2689573
I'll going to use it. What about 20 points for you?
----
Igor.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

688 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