-->

02 April 2018

Kompare Tabel Kolom Di Dua Database SQL Server



Hi, Mas bro,....

Aku mau berbagi ilmu lagi nich mas bro,
mumpung aku inget ya...

Bagaimana caranya Kompare Kolom yang ada di tabel  tetapi dari dua database menggunakan SQL Server :)

Gini....ni..cara ngebuatnya mas bro :)

Langkahnya :
Buka SQL SERVER Editor, lalu Copy Paste Script dibawah ini mas bro

DECLARE @Db1 NVARCHAR(MAX)
DECLARE @Table1 NVARCHAR(MAX)
DECLARE @Db2 NVARCHAR(MAX)
DECLARE @Table2 NVARCHAR(MAX)
DECLARE @Sql NVARCHAR(MAX)

SET @Db1 = '{Nama Database 1}'
SET @Table1 = '{Nama Tabel l}'
SET @Db2 = '{Nama Database 2}'
SET @Table2 = '{Nama Tabel 2}'
SET @Sql = ' ' +
' SELECT ''in ' + @Db1 + '.' + @Table1 + ' --- not in ' + @Db2 + '.' + @Table2 + ''' AS TITLE, a.TABLE_CATALOG, a.column_name ' +
' FROM ' + @Db1 + '.INFORMATION_SCHEMA.COLUMNS a ' +
' WHERE a.column_name NOT IN (SELECT column_name ' +
' FROM ' + @Db2 + '.INFORMATION_SCHEMA.COLUMNS b ' +
' WHERE b.table_name IN (''' + @Table2 + ''')) ' +
' AND a.table_name IN (''' + @Table1 + ''') ' +

' UNION ALL ' +

' SELECT ''in ' + @Db2 + '.' + @Table2 + ' --- not in ' + @Db1 + '.' + @Table1 + ''' AS TITLE, a.TABLE_CATALOG, a.column_name ' +
' FROM ' + @Db2 + '.INFORMATION_SCHEMA.COLUMNS a ' +
' WHERE a.column_name NOT IN (SELECT column_name ' +
' FROM ' + @Db1 + '.INFORMATION_SCHEMA.COLUMNS b ' +
' WHERE b.table_name IN (''' + @Table1 + ''')) ' +
' AND a.table_name IN (''' + @Table2 + ''') ' +
''

EXEC (@Sql)

Ini Hasilnya :





Selamat mencoba Mas Bro :)




Tidak ada komentar:

Posting Komentar