question

Hero avatar image
Hero asked

Stored Prcedure issue

Hello I am trying to insert some values into a table through a stored procedure. Which I can do, BUT then before the name goes into the table. I want it to check if that first/last name is already in the table? Then of course raise an error letting the user know the name is already in the tabe. Cheers for any help!

-- This is what I have so far --

Create Procedure AddCarrier (@CarrierFirstname varchar (30) = null, @CarrierLastName varchar (30) = null, @Phone char(10 = null) as if

-- End --

stored-procedures
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Squirrel 1 avatar image
Squirrel 1 answered
if not exists (select * from sometable where somecol = @somevalue)            
begin            
   <perform the insert>            
end            
else            
begin            
  <raise the error>            
end            
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.