Skip to content

Instantly share code, notes, and snippets.

@jondlm
Created March 28, 2013 23:35
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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.
-- =============================================
-- Author: Jon - jond@csgpro.com
-- 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