Wednesday, 26 July 2017

Cursor in MsSql





ALTER PROCEDURE [dbo].[UpdateStateFromTable1toTable2]
AS

DECLARE @Code NVARCHAR(250)
DECLARE @Name NVARCHAR(450)

DECLARE @StateCodeCount NVARCHAR(50)

DECLARE cur_emp CURSOR
STATIC FOR
SELECT [Code],[Description]
FROM [dbo].[State]
OPEN cur_emp
IF @@CURSOR_ROWS > 0
 BEGIN
FETCH NEXT FROM cur_emp INTO @Code,@Name
WHILE @@Fetch_status = 0
BEGIN

SET @StateCodeCount=''
SET @StateCodeCount=ISNULL((SELECT TOP 1 ISNULL(Code,'')
FROM [dbo].[State_Mst]
WHERE LOWER(Code)=LOWER(@Code)),'')


IF(LEN(@StateCodeCount)=0)
BEGIN
INSERT INTO [dbo].[State_Mst]
(
[Code],[Name]
)
VALUES
(
@Code,@Name
)
END
ELSE
BEGIN
UPDATE [dbo].[State_Mst]
SET [Name]=@Name
WHERE LOWER(Code)=LOWER(@Code)
END
FETCH NEXT FROM cur_emp INTO  @Code,@Name
END
END
CLOSE cur_emp
DEALLOCATE cur_emp
SET NOCOUNT OFF 

No comments:

Post a Comment

React Hooks - custom Hook

  v CustomHook Ø React allows us to create our own hook which is known as custom hook. Example – 1 localStorage Demo Step-1 Create ...