Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use master
- go
- DECLARE @Server1 VARCHAR(100) ='[LD38SQLEXPRESS2005].'; --include a dot at the end
- DECLARE @DB1 VARCHAR(100) = '[TestDB]';
- DECLARE @Table1 VARCHAR(100) = 'Customer';
- DECLARE @Server2 VARCHAR(100) ='[LD38SQLEXPRESS2005].'; --include a dot at the end
- DECLARE @DB2 VARCHAR(100) = '[TestDB2]';
- DECLARE @Table2 VARCHAR(100) = 'Customer';
- DECLARE @SQL NVARCHAR(MAX);
- SET @SQL =
- '
- SELECT Table1.ServerName,
- Table1.DBName,
- Table1.SchemaName,
- Table1.TableName,
- Table1.ColumnName,
- Table1.name DataType,
- Table1.Length,
- Table1.Precision,
- Table1.Scale,
- Table1.Is_Identity,
- Table1.Is_Nullable,
- Table2.ServerName,
- Table2.DBName,
- Table2.SchemaName,
- Table2.TableName,
- Table2.ColumnName,
- Table2.name DataType,
- Table2.Length,
- Table2.Precision,
- Table2.Scale,
- Table2.Is_Identity,
- Table2.Is_Nullable
- FROM
- (SELECT ''' + @Server1 + ''' ServerName,
- ''' + @DB1 + ''' DbName,
- SCHEMA_NAME(t.schema_id) SchemaName,
- t.Name TableName,
- c.Name ColumnName,
- st.Name,
- c.Max_Length Length,
- c.Precision,
- c.Scale,
- c.Is_Identity,
- c.Is_Nullable
- FROM ' + @Server1 + @DB1 + '.sys.tables t
- INNER JOIN ' + @Server1 + @DB1 + '.sys.columns c ON t.Object_ID = c.Object_ID
- INNER JOIN sys.types st ON St.system_type_id = c.System_Type_id AND st.user_type_id = c.user_type_id
- WHERE t.Name = ''' + @Table1 + ''') Table1
- FULL OUTER JOIN
- (SELECT ''' + @Server2 + ''' ServerName,
- ''' + @DB2 + ''' DbName,
- SCHEMA_NAME(t.schema_id) SchemaName,
- t.name TableName,
- c.name ColumnName,
- st.Name,
- c.max_length Length,
- c.Precision,
- c.Scale,
- c.Is_Identity,
- c.Is_Nullable
- FROM ' + @Server2 + @DB2 + '.sys.tables t
- INNER JOIN ' + @Server2 + @DB2 + '.sys.columns c ON t.Object_ID = c.Object_ID
- INNER JOIN sys.types st ON St.system_type_id = c.System_Type_id AND st.user_type_id = c.user_type_id
- WHERE t.Name = ''' + @Table2 + ''') Table2
- ON Table1.ColumnName = Table2.ColumnName
- ORDER BY CASE WHEN Table1.ColumnName IS NULL THEN 2 ELSE 1 END, Table1.ColumnName
- '
- EXEC sp_executesql @SQL
Add Comment
Please, Sign In to add comment