Tuesday, March 20, 2012

Quickest query to know if a record exist or not

I have a lookup table like:
ID Desc
-- --
1 AAa
2 B
3 C
I know there are some queries to to see if an ID exist in this table or not
,but I just wondered which one is the quickest one.
ThanksSomething like this:
IF EXISTS (SELECT * FROM T_CorpData WHERE CorpID = 'acn1')
PRINT 'Row Exists'
Keith
"RayAll" <RayAll@.microsft.com> wrote in message
news:esE9X2BIFHA.1288@.TK2MSFTNGP10.phx.gbl...
> I have a lookup table like:
> ID Desc
> -- --
> 1 AAa
> 2 B
> 3 C
> I know there are some queries to to see if an ID exist in this table or
not
> ,but I just wondered which one is the quickest one.
> Thanks
>|||Try,
declare @.id ...
if exists(select * from t where [ID] = @.id)
print 'yes'
else
print 'no'
go
AMB
"RayAll" wrote:

> I have a lookup table like:
> ID Desc
> -- --
> 1 AAa
> 2 B
> 3 C
> I know there are some queries to to see if an ID exist in this table or no
t
> ,but I just wondered which one is the quickest one.
> Thanks
>
>|||Hi
This may depend on what the context is that you require to know the
existance, but try:
IF EXISTS ( SELECT 1 FROM [Lookup Table] where Id = 1)
...
Please post DDL as http://www.aspfaq.com/etiquette.asp?id=5006 and
example data as insert statements http://vyaskn.tripod.com/code.htm#inserts
John
"RayAll" wrote:

> I have a lookup table like:
> ID Desc
> -- --
> 1 AAa
> 2 B
> 3 C
> I know there are some queries to to see if an ID exist in this table or no
t
> ,but I just wondered which one is the quickest one.
> Thanks
>
>|||IF EXISTS (SELECT * FROM YourTable WHERE ID = xx)
Print 'It exists'
Andrew J. Kelly SQL MVP
"RayAll" <RayAll@.microsft.com> wrote in message
news:esE9X2BIFHA.1288@.TK2MSFTNGP10.phx.gbl...
>I have a lookup table like:
> ID Desc
> -- --
> 1 AAa
> 2 B
> 3 C
> I know there are some queries to to see if an ID exist in this table or
> not ,but I just wondered which one is the quickest one.
> Thanks
>

No comments:

Post a Comment