Wednesday, March 7, 2012

Fast Query

Hi,
What's the fastest way of checking if a value exists in a table? How is this
done in a production enviornment? Is it better ot return the result or a 1 o
r
0 as yes or no via an output parameter.
I am currently using
Create Proc test(
@.searchVal nvarchar(10))
as
select id from tablea where id = @.searchVal
ThanksIts better to select the key or something like this that I always choose
select 1 from tablea where id = @.searchVal
and you can assign it to an output parameter or type BIT. Better than
getting a dataset. And in the calling application (with respect to C#.NET
here) use ExecuteScalar() and get the value.
Hope this helps.|||You can also use exists
Create Proc test(
@.searchVal nvarchar(10))
as
if exists (select * from tablea where id = @.searchVal)
select 1
else
select 0
Denis the SQL Menace
http://sqlservercode.blogspot.com/

No comments:

Post a Comment