Wednesday, July 29, 2009

Looping in Cities to insert

Alter proc TestCity_Assigned --'bihar'
(@state varchar(100))
as begin
declare @mycity varchar(100)

declare cursor1 cursor for
select distinct city from area (nolock) where state=@state
open cursor1
fetch next from cursor1 into @mycity

while @@fetch_status=0
begin
if not exists(select * from city_assigned (nolock) where name='Bikash' and assigned_city=@mycity)
insert into city_assigned(name,assigned_city,status) values('Bikash',@mycity,'Super Manager')
fetch next from cursor1 into @mycity
end
close cursor1
deallocate cursor1
end

TestCity_Assigned 'bihar'

No comments: