-- =============================================
-- Author:		Dőrr Tamás
-- Create date: 
-- Description:	
-- =============================================
IF OBJECT_ID('dbo.sp_JelszoResetTanulo') IS NOT NULL BEGIN
  DROP PROCEDURE dbo.sp_JelszoResetTanulo
END
GO

CREATE PROCEDURE [dbo].[sp_JelszoResetTanulo]
  @xml xml,
  @tanevID int,
  @intezmenyID int,
  @userId int
AS
BEGIN

  BEGIN TRY
    SET NOCOUNT ON
    SET XACT_ABORT ON
  
    DECLARE @tbl TABLE (
      C_ID int,
      C_USERNAME nvarchar(50),
	  C_PASSWORD nvarchar(50),
      C_NEMGENERALHATO char(1))
    INSERT INTO @tbl
      SELECT 
      sor.value('number((Id)[1])', 'int') AS C_ID,
      sor.value('(UserName)[1]', 'nvarchar(50)') AS C_USERNAME,
      sor.value('(Password)[1]', 'nvarchar(50)') AS C_PASSWORD,
      sor.value('(NemGeneralhato)[1]', 'char(1)') AS C_NEMGENERALHATO
  	FROM @xml.nodes('/BelepesiAdatok/BelepesiAdatokLista/BelepesiAdat') as sorok(sor)
    
	BEGIN TRANSACTION
   
		UPDATE [T_FELHASZNALOBELEPES]
		SET
			[C_NEPTUNNAPLOJELSZO] = NULL
			,[C_SO] = NULL
			,[C_JELSZO] = x.C_PASSWORD
			,[C_JELSZOVALTOZTATASIDEJE] = GETDATE()
		FROM [T_FELHASZNALOBELEPES] fb
		INNER JOIN @tbl x ON x.C_ID = fb.C_FELHASZNALOID
		WHERE
			fb.TOROLT = 'F'
   
  COMMIT TRANSACTION
  END TRY
  BEGIN CATCH
    IF @@TRANCOUNT > 0 AND XACT_STATE() <> 0 
      ROLLBACK TRANSACTION;
    THROW
  END CATCH
END