GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Zsiga Attila
-- Create date: 2016.10.21.
-- Description:	Az input tanulókhoz tartozó gondviselőket adja vissza.
-- =============================================
IF OBJECT_ID('sp_GetTanulokhozTartozoGondviselok') IS NOT NULL BEGIN
  DROP PROCEDURE sp_GetTanulokhozTartozoGondviselok
END
GO

CREATE PROCEDURE sp_GetTanulokhozTartozoGondviselok
	@pXml xml, 
	@pTanevId INT
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	SET XACT_ABORT ON

	SELECT 
	TanuloId = sor.value('.', 'int')
	INTO
	#TEMPTANULOID
	FROM @pXml.nodes('/Tanulok/IdLista/Id') as sorok(sor)
	
	--TEMP TABLES
	SELECT * INTO #TEMPTANULO FROM T_TANULO 
	WHERE TOROLT = 'F' AND C_ALTANEVID = @pTanevId
	---------------
	SELECT * INTO #TEMPTANULOK FROM #TEMPTANULOID
	LEFT JOIN #TEMPTANULO ON #TEMPTANULO.ID = #TEMPTANULOID.TanuloId
	---------------
	SELECT * INTO #TEMPGONDVISELO FROM T_GONDVISELO
	WHERE TOROLT = 'F' AND C_TANEVID = @pTanevId
	---------------
	SELECT 
	* 
	INTO 
	#TEMPERINTETTGONDVISELOK 
	FROM
	#TEMPGONDVISELO
	WHERE
	C_TANULOID IN (SELECT TanuloId FROM #TEMPTANULOID)
	---------------

	SELECT
	ID GondviseloId,
	C_NEV Nev,
	C_TANULOID TanuloId
	FROM
	#TEMPERINTETTGONDVISELOK

	DROP TABLE #TEMPTANULOID
	DROP TABLE #TEMPTANULO
	DROP TABLE #TEMPTANULOK
	DROP TABLE #TEMPGONDVISELO
	DROP TABLE #TEMPERINTETTGONDVISELOK
END
