0% found this document useful (0 votes)
39 views1 page

Checkid Cursor

This stored procedure checks if an employee ID already exists based on the email domain before inserting a new record into the employees table, using a cursor to loop through all existing employee emails and compare the domain to the new record.

Uploaded by

venkatbj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views1 page

Checkid Cursor

This stored procedure checks if an employee ID already exists based on the email domain before inserting a new record into the employees table, using a cursor to loop through all existing employee emails and compare the domain to the new record.

Uploaded by

venkatbj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

create procedure pAddEmp(@id int,@name char(10),@em varchar(50))

as
begin
declare @idpart varchar(50),@orgidpart varchar(50),
@flag char(10)

select @idpart=substring(@em,1,CHARINDEX('@',@em,1)-1)

declare mycur cursor


for
select substring(email,1,CHARINDEX('@',email,1)-1)
from emp

open mycur

fetch next from mycur into @orgidpart

while(@@FETCH_STATUS=0)
begin
if(@idpart=@orgidpart)
begin
set @flag='true'
break
end
else
begin
set @flag='false'
fetch next from mycur into @orgidpart
end
end

close mycur

deallocate mycur

if(@flag='true')
print 'ID already exists!!'
else
begin
insert into emp values(@id,@name,@em)
print 'ID is available!!!Inserted Record Successfully!!'
end
end

You might also like