﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE procedure [dbo].[TrimTable]
	@table VARCHAR(128)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	DECLARE @sql NVARCHAR(MAX)
	SET @sql = 'SELECT *
INTO #Temp
FROM ' + @table + '
WHERE ID_ELAB = (
	SELECT MAX(ID_ELAB)
	FROM ' + @table +
')
TRUNCATE TABLE ' + @table +
'
INSERT INTO ' + @table + '
SELECT *
FROM #Temp
DROP TABLE #Temp'
EXEC sp_executesql @sql
END