I have a requirement to insert/update bulk number of records into a table. For that, i have written a stored procedure as follows. But it is taking much time to execute. Could any one suggest changes to SP to get better performance in execution.
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
-- do update
end
else
begin
-- do insert
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