Created
March 28, 2013 23:35
-
-
Save jondlm/5267682 to your computer and use it in GitHub Desktop.
A TSQL helper stored proc for comparing the column names of two tables.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- ============================================= | |
-- Author: Jon - [email protected] | |
-- Create date: 2013-03-05 | |
-- Description: Helper function for comparing columns between two tables. | |
-- ============================================= | |
CREATE FUNCTION [helper].[fnCompareTableColumns] ( | |
@baseTable varchar(250), | |
@compareTable varchar(250) | |
) | |
RETURNS TABLE | |
AS | |
RETURN ( | |
with cte as ( | |
select TABLE_NAME | |
, COLUMN_NAME | |
from INFORMATION_SCHEMA.COLUMNS | |
where TABLE_NAME like @compareTable | |
) | |
select | |
a.TABLE_NAME BaseTableName, | |
a.COLUMN_NAME BaseTableColumnName, | |
b.COLUMN_NAME CompareTableColumnName, | |
b.TABLE_NAME CompareTableName | |
from INFORMATION_SCHEMA.COLUMNS a | |
left join cte b | |
on a.COLUMN_NAME = b.COLUMN_NAME | |
where a.TABLE_NAME like @baseTable | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment