Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 raiseerror

Author  Topic 

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2007-10-23 : 08:54:13
Hi,
As you see below, the stored procedure inserts a record if @CountExisting = 0
This is done so that no duplicates are entered into the table.
The question is:
How do I return an error message if the @CountExisting > 0 ?
Is what I have done correct inside the else statement?

Thanks



create procedure [dbo].[uspSecurityAdd]


@SecurityName varchar(50)


as

declare @CountExisting tinyint

begin try

select
Security_Name
from
tblSecurities
where
Security_Code = @SecurityCode

set @CountExisting = @@ROWCOUNT

if (@CountExisting = 0)
begin

insert into
tblSecurities
(
Security_Name
)
values
(

@SecurityName
)

end
else
begin
RaiseError('Record already exists.')
end

end try

begin catch
declare @msg nvarchar(200)
SET @msg = ('Error during Adding Security')

EXECUTE uspErrorReporter @msg
end catch

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-10-23 : 08:56:00
RaiseError('Record already exists.', 16, 1)



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-10-23 : 09:00:04
www.sommarskog.se/error-handling-I.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -