CREATE PROCEDURE MIG_ZZID_TABLE_GUID_Sp AS
BEGIN

	DECLARE @ITR_TBL_QRY VARCHAR(MAX)
	DECLARE @GUID_TABLE_INFO TABLE ( TBL_QRY_OUTPUT VARCHAR(MAX) NOT NULL );
	
	DELETE FROM MIG_ZZID_TABLE_GUID

	INSERT INTO @GUID_TABLE_INFO
	SELECT  CONCAT('SELECT ', COL.COLUMN_NAME,', ''', TBL.TABLE_NAME, ''' AS TABLE_NAME, NEWID() AS GUID FROM ', TBL.TABLE_NAME)  AS TBL_QRY_OUTPUT 
	FROM INFORMATION_SCHEMA.TABLES  TBL
	JOIN INFORMATION_SCHEMA.COLUMNS COL 
	ON TBL.TABLE_NAME = COL.TABLE_NAME 
	WHERE TBL.TABLE_CATALOG = DB_NAME() AND TBL.TABLE_TYPE = 'BASE TABLE' AND COL.COLUMN_NAME = 'ZZ_ID' AND TBL.TABLE_NAME NOT LIKE 'MIG_%' 
	ORDER BY TBL.TABLE_NAME


	DECLARE RecordsCursor CURSOR  LOCAL
	FOR  
		SELECT TBL_QRY_OUTPUT
		FROM @GUID_TABLE_INFO

	OPEN RecordsCursor  
	WHILE 1 = 1   
	BEGIN
		FETCH NEXT FROM RecordsCursor INTO @ITR_TBL_QRY
		IF @@FETCH_STATUS <> 0
			BREAK

		INSERT INTO MIG_ZZID_TABLE_GUID
		exec (@ITR_TBL_QRY)

	END

	CLOSE RecordsCursor


	-- Replace LAST 'UNION' WITH 'ORDER BY TABLE_NAME, ZZ_ID'


END

