Atul Patil

Atul Patil

  • NA
  • 586
  • 73.2k

Alternative for cursor?

Jan 17 2017 12:58 AM
please help me to remove cursor from procedure and find best alternative to it.
Here is my code
 
ALTER PROCEDURE [dbo].[CreateUser] 
@FirstName varchar(50),
@LastName varchar(50),
@Email varchar(50),
@SessionTime int,
@UserRole int
AS
Begin
DECLARE @WidgetId INT;
DECLARE @UserId INT
DECLARE @RoleId INT
DECLARE @TempId INT
SET NOCOUNT ON;
insert into [dbo].[Users] (FirstName,[LastName],Email,SessionTime,Password,[UserRole],[IsActive])
OUTPUT INSERTED.UserId,inserted.FirstName,INSERTED.LastName,INSERTED.Email,INSERTED.SessionTime, inserted.Password,inserted.UserRole,inserted.IsActive
values( @FirstName ,@LastName ,@Email,@SessionTime,'123',@UserRole,1)
set @UserId=SCOPE_IDENTITY()
DECLARE thecursor CURSOR FOR
select [dbo].[TemplatewiseWidget].WidgetId,[dbo].[TemplatewiseWidget].TempId from [dbo].[TemplatewiseWidget]
inner join [dbo].[RoleWiseTemplate]
on [dbo].[TemplatewiseWidget].TempId=[dbo].[RoleWiseTemplate].TempId where [dbo].[RoleWiseTemplate].RoleId=1
and [dbo].[RoleWiseTemplate].IsActive=1 and [dbo].[TemplatewiseWidget].IsActive=1
OPEN thecursor
FETCH NEXT FROM thecursor INTO @WidgetId,@TempId
WHILE @@Fetch_status = 0
BEGIN
insert into [dbo].[UserWidgetTransaction]
(
[UserId],WidgetId,RoleId,TempId,X,Y,Height,Width,[IsActive],IsDeleted
)
values
(
@UserId,@WidgetId,@UserRole,@TempId,0,0,1,4,0,0
)
FETCH NEXT FROM thecursor INTO @WidgetId,@TempId
END
CLOSE thecursor
DEALLOCATE thecursor
END
 

Answers (2)