check mailid exists or not

Posted by Gopal_nivas under Sql Server on 9/14/2010 | Points: 10 | Views : 888 | Status : [Member] | Replies : 2
hi..

how to check mailid exists or not using stored procedure..

if exists i want to take the count value
if not exists i want to insert tat mailid ..

how to do this in single stored procedure..

need ur suggestions..

regards
gopal.s




Responses

Posted by: T.saravanan on: 9/14/2010 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi Gopal,

In Your Procedure write this following code,


@smailid-->is your mailid

Declare @nCount int
Set @nCount=(select count(mailid) from table where mailid=@smailid)
if(@nCount >= 1)
begin
//return @nCount
end
else
begin
// Insert Condition
end


Cheers :)

Thanks,
T.Saravanan

Gopal_nivas, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Peermohamedmydeen on: 9/14/2010 [Member] Bronze | Points: 25

Up
0
Down
Hi,

As Saravanan Said, need not to declare an @nCount variable. This will cause an additional resource.

Declaring a variable and set the value to the variable may slow down the performance of the SP.

For last fetched data count use @@RowCount . This will give you the count.

Do like this


@smailid-->is your mailid

IF EXISTS (select * from table where mailid like '@smailid')
begin
-- Modify query
end
else
begin
-- Insert query
end



OR


@smailid-->is your mailid

select * from table where mailid like '@smailid'

IF(@@ROWCOUNT > 0)
begin
-- Modify query
end
else
begin
-- Insert query
end





Gopal_nivas, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response