BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

WHERE clause
Goto page 1, 2  Next
 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (SQL Servers)
View previous topic :: View next topic  
Author Message
Ramtin Kazemi
Guest





PostPosted: Thu Jul 08, 2004 10:07 pm    Post subject: WHERE clause Reply with quote



Hi
consider following SQL command :

SELECT * FROM MyTable WHERE KeyId = :KeyId

What should the value of parameter :KeyId be so that WHERE clause has no
effect?
(i.e. returns all records)

Thanks
Ramtin Kazemi


Back to top
Iman L Crawford
Guest





PostPosted: Thu Jul 08, 2004 10:19 pm    Post subject: Re: WHERE clause Reply with quote



"Ramtin Kazemi" <Ramtin76Hz (AT) hotmail (DOT) com> wrote in
news:40edc596 (AT) newsgroups (DOT) borland.com:
Quote:
What should the value of parameter :KeyId be so that WHERE clause has
no effect?

This changes according to DB server you're using.

You could have a where clause that is like

WHERE KeyId LIKE :KEY_ID

and settting the paramater to % if it is blank. This only works for
columns of type varchar/char.

With oracle you can do something like

WHERE KeyID = DECODE(:KEY_ID, 0, KeyId, :KEY_ID)

and set the parameter to 0 to get all the records. You should be able
to do similar things with other DBs.

Other than that you can have a stored procedure return a result set, or
rebuild the SQL statment everytime.

--
Iman



Back to top
Ramtin Kazemi
Guest





PostPosted: Fri Jul 09, 2004 10:48 am    Post subject: Re: WHERE clause Reply with quote



Hi Iman
I am using MS-SQL Server and column type is 'int' .
What should I do now?

Thank you


Back to top
Paulo Juliano Horlle
Guest





PostPosted: Fri Jul 09, 2004 12:22 pm    Post subject: Re: WHERE clause Reply with quote

Hi Ramtin!

You can start a trace with Profiler to get the command that Delphi is send
to SQLServer.

Regards,
Paulo Juliano Horlle

"Ramtin Kazemi" <Ramtin76Hz (AT) hotmail (DOT) com> wrote

Quote:
Hi
consider following SQL command :

SELECT * FROM MyTable WHERE KeyId = :KeyId

What should the value of parameter :KeyId be so that WHERE clause has no
effect?
(i.e. returns all records)

Thanks
Ramtin Kazemi





Back to top
Viatcheslav V. Vassiliev
Guest





PostPosted: Fri Jul 09, 2004 12:23 pm    Post subject: Re: WHERE clause Reply with quote

To return all records, you should just remove WHERE.

SELECT * FROM MyTable

There is no way to specify parameter for KeyId and return all rows.

//------------------------------------------
Regards,
Vassiliev V. V.
http://www.managed-vcl.com - using .Net objects in Delphi for Win32 +
ADO.Net
http://www.oledbdirect.com - The fastest way to access MS SQL Server,
MS Jet (Access) and Interbase (through OLEDB)

"Ramtin Kazemi" <Ramtin76Hz (AT) hotmail (DOT) com> сообщил/сообщила в новостях
следующее: news:40edc596 (AT) newsgroups (DOT) borland.com...
Quote:
Hi
consider following SQL command :

SELECT * FROM MyTable WHERE KeyId = :KeyId

What should the value of parameter :KeyId be so that WHERE clause has no
effect?
(i.e. returns all records)

Thanks
Ramtin Kazemi





Back to top
Paulo Juliano Horlle
Guest





PostPosted: Fri Jul 09, 2004 12:30 pm    Post subject: Re: WHERE clause Reply with quote

I'm not understood before...

To get all records you can use this select:

select t.* from mytable t
where t.KeyID = isnull( :KeyID, t.KeyID)

Now, if you send a nul value to parameter, the statement compare with KeyID
field.

Regards,
Paulo Juliano Horlle



"Ramtin Kazemi" <Ramtin76Hz (AT) hotmail (DOT) com> wrote

Quote:
Hi
consider following SQL command :

SELECT * FROM MyTable WHERE KeyId = :KeyId

What should the value of parameter :KeyId be so that WHERE clause has no
effect?
(i.e. returns all records)

Thanks
Ramtin Kazemi





Back to top
Iman L Crawford
Guest





PostPosted: Fri Jul 09, 2004 1:09 pm    Post subject: Re: WHERE clause Reply with quote

"Ramtin Kazemi" <Ramtin76Hz (AT) hotmail (DOT) com> wrote in news:40ee7810
@newsgroups.borland.com:
Quote:
I am using MS-SQL Server and column type is 'int' .
What should I do now?

See Paulo's message.

--
Iman



Back to top
Tim Radford
Guest





PostPosted: Fri Jul 09, 2004 1:47 pm    Post subject: Re: WHERE clause Reply with quote

You could try

SELECT * FROM MyTable WHERE :KeyID = -1 OR KeyId = :KeyId

Then supplying a value of -1 will return all rows

Tim


"Ramtin Kazemi" <Ramtin76Hz (AT) hotmail (DOT) com> wrote

Quote:
Hi
consider following SQL command :

SELECT * FROM MyTable WHERE KeyId = :KeyId

What should the value of parameter :KeyId be so that WHERE clause has no
effect?
(i.e. returns all records)

Thanks
Ramtin Kazemi





Back to top
Ramtin Kazemi
Guest





PostPosted: Fri Jul 09, 2004 3:50 pm    Post subject: Re: WHERE clause Reply with quote

Hi Tim
I tried your solution but got this error :
---------------------------
Error
---------------------------
dbExpress Error: Invalid Parameter.
---------------------------
OK
---------------------------

Regards
K.Ramtin


Back to top
Ramtin Kazemi
Guest





PostPosted: Fri Jul 09, 2004 4:00 pm    Post subject: Re: WHERE clause Reply with quote

Hi Paulo
How should I set the parameter value to "isnull( :KeyID, t.KeyID)" ?
I didn't understand.

Regards
K.ramtin


Back to top
Ramtin Kazemi
Guest





PostPosted: Fri Jul 09, 2004 4:02 pm    Post subject: Re: WHERE clause Reply with quote

Hi Tim
I tried your solution but got this error :
---------------------------
Error
---------------------------
dbExpress Error: Invalid Parameter.
---------------------------
OK
---------------------------

Regards
K.Ramtin



Back to top
Oscar Santiesteban Jr.
Guest





PostPosted: Sun Jul 11, 2004 12:44 am    Post subject: Re: WHERE clause Reply with quote

What I would do is have something like this: (some code,some pseudo-code)

adoquery1.sql.clear;
adoquery1.sql.add('select * from mytable');
if keyid > 0 then // or whatever condition indicating that you want
some specific records
begin
adoquery1.sql.add('where keyid = ' + inttostr(keyid))
end;
adoquery1.open;

This assumes that KEYID is an INT like you mentioned.


"Ramtin Kazemi" <Ramtin76Hz (AT) hotmail (DOT) com> wrote

Quote:
Hi
consider following SQL command :

SELECT * FROM MyTable WHERE KeyId = :KeyId

What should the value of parameter :KeyId be so that WHERE clause has no
effect?
(i.e. returns all records)

Thanks
Ramtin Kazemi





Back to top
Iman L Crawford
Guest





PostPosted: Sun Jul 11, 2004 3:49 am    Post subject: Re: WHERE clause Reply with quote

"Ramtin Kazemi" <Ramtin76Hz (AT) hotmail (DOT) com> wrote in
news:40efcf0e (AT) newsgroups (DOT) borland.com:

Quote:
How should I set the parameter value to "isnull( :KeyID, t.KeyID)" ?
I didn't understand.

You need to clear the KeyId parameter. ParamByName('KeyID').Clear;

Iman


Back to top
Dennis Passmore
Guest





PostPosted: Sun Jul 11, 2004 5:42 pm    Post subject: Re: WHERE clause Reply with quote


declare @fkeyid int
select @fKeyid = :Keyid
SELECT * FROM MyTable
WHERE @fKeyid is null
OR KeyId = @fKeyId



Dennis Passmore
Ultimate Software, Inc.
Back to top
Ramtin Kazemi
Guest





PostPosted: Mon Jul 12, 2004 6:54 pm    Post subject: Re: WHERE clause Reply with quote

Hi Tim !

You are right , It works

Thanks
K.Ramtin


Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (SQL Servers) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.