I have to handle bulk number of records in SP. For that, i am using WHILE loop as in the following SP. But, it is taking more time to execute. Please help me by suggesting other alternate approach which can improve the performance.
create procedure sp_save_user
(
@a_i_lang_id integer,
@a_s_data ntext
)
WITH ENCRYPTION
as
begin
set nocount on
SET QUOTED_IDENTIFIER ON
--Declaring local variables
declare @l_s_USER_ID NVARCHAR(30)
declare @l_s_USER_NAME NVARCHAR(255)
declare @l_n_rmStatusCount numeric(10)
declare @l_n_XMLDoc XML
set @l_n_XMLDoc = cast(@a_s_data as xml)
CREATE TABLE #DW_TEMP_TABLE_SAVE(
[USER_ID] [NVARCHAR](30),
[USER_NAME] [NVARCHAR](255)
)
insert into #DW_TEMP_TABLE_SAVE
select A.B.value('(USER_ID)[1]', 'nvarchar(30)' ) [USER_ID],
A.B.value('(USER_NAME)[1]', 'nvarchar(30)' ) [USER_NAME]
from @l_n_XMLDoc.nodes('//ROW') as A(B)
--Get total number od records
select @l_n_rmStatusCount = count(*) from #DW_TEMP_TABLE_SAVE
--loop through records and insert/update table
while (@l_n_rmStatusCount > 0)
begin
SELECT @l_s_USER_ID = [USER_ID] ,
@l_s_USER_NAME = [USER_NAME]
FROM ( SELECT ROW_NUMBER() OVER (ORDER BY [USER_ID]) AS rownumber,
[USER_ID],[USER_NAME] FROM #DW_TEMP_TABLE_SAVE) as temptablename
WHERE rownumber = @l_n_rmStatusCount
if exists(
select 'X' from table_user_info(nolock)
where [user_id] = @l_s_USER_ID
)
begin
-- call another SP to do UPDATE multiple tables
end
else
begin
-- call another SP to do INSERT multiple tables
end
set @l_n_rmStatusCount = @l_n_rmStatusCount -1
end
drop table #DW_TEMP_TABLE_SAVE
SET QUOTED_IDENTIFIER OFF
set nocount off
end
go