/*=======================================================
EXEC GETNEWTABLES_BY_COMPARE2DBS 'old_db','new_db'
==========================================================*/
CREATE PROC GETNEWTABLES_BY_COMPARE2DBS
(
@OLD VARCHAR(1000)
,@NEW VARCHAR(1000)
)
AS
BEGIN
/*=====================================================================================
THIS PROC GIVES NAME OF THOSE TABLES WHICH EXISTS IN NEW DATABASE
BUT DOES NOT EXISTS IN OLD DATABASE..
*===================================================================================*/
IF NOT EXISTS(SELECT 1 FROM SYS.DATABASES WHERE NAME= @OLD)
BEGIN
SELECT 'OLD DATABASE DOESN''T EXISTS.'
RETURN
END
IF NOT EXISTS(SELECT 1 FROM SYS.DATABASES WHERE NAME= @NEW)
BEGIN
SELECT 'NEW DATABASE DOESN''T EXISTS.'
RETURN
END
DECLARE @X VARCHAR(MAX)
SET @X='SELECT TABLE_NAME
FROM ' + @NEW + '.INFORMATION_SCHEMA.COLUMNS AS NEW
WHERE NOT EXISTS (SELECT ''X'' FROM ' + @OLD + '.INFORMATION_SCHEMA.COLUMNS AS OLD
WHERE OLD.TABLE_NAME=NEW.TABLE_NAME
AND OLD.COLUMN_NAME=NEW.COLUMN_NAME)
AND TABLE_NAME NOT IN (SELECT DISTINCT TABLE_NAME
FROM ' + @OLD + '.INFORMATION_SCHEMA.COLUMNS AS OLD)
GROUP BY TABLE_NAME'
EXEC(@X)
END
No comments:
Post a Comment