I'm having some difficulties implementing a Merge via SSMS. I understand the concept, but the real difficulty is deciphering the script of the existing Stored Proc. If I could get the script to print, then it would be smooth sailings from there. Using Print @SQL_Statement doesn't seem to work for the following SP:
************************
ALTER PROC Merge_Test@TableName AS nvarchar(100),
@DatabaseNameLocal AS nvarchar(100)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @LogTableName AS nvarchar(100)
DECLARE @SQL_Statement AS nvarchar(max)
SET @LogTableName = 'LOG_INT_' + @TableName
SET @SQL_Statement = '/* <<<I1>>> Update existing INT records*/'
SET @SQL_Statement = @SQL_Statement + CHAR(10) + 'UPDATE'
SET @SQL_Statement = @SQL_Statement + CHAR(10) + ' i'
SET @SQL_Statement = @SQL_Statement + CHAR(10) + 'SET'
SELECT @SQL_Statement = @SQL_Statement + CHAR(10) +
' i.' + COLUMN_NAME + ' = s.' + COLUMN_NAME + ','
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = @LogTableName
AND COLUMN_NAME NOT IN
(
'INSERT_DATE',
'LAST_MOD_DATE',
'HASH_VALUE',
'VALID_FROM_LOG_DATE',
'VALID_TO_LOG_DATE'
)
SET @SQL_Statement = @SQL_Statement + CHAR(10) + ' --i.INSERT_DATE = s.LAST_MOD_DATE ** USE ONLY FOR NEW RECORDS,'
SET @SQL_Statement = @SQL_Statement + CHAR(10) + ' i.LAST_MOD_DATE = GETDATE(),'
SET @SQL_Statement = @SQL_Statement + CHAR(10) + ' i.HASH_VALUE = s.HASH_VALUE,'
SET @SQL_Statement = @SQL_Statement + CHAR(10) + ' i.FILE_NAME = s.FILE_NAME'
SET @SQL_Statement = @SQL_Statement + CHAR(10) + 'FROM'
SET @SQL_Statement = @SQL_Statement + CHAR(10) + ' ' + @DatabaseNameLocal + '_stg..STG_' + @TableName + ' s'
SET @SQL_Statement = @SQL_Statement + CHAR(10) + 'INNER JOIN'
SET @SQL_Statement = @SQL_Statement + CHAR(10) + ' ' + @DatabaseNameLocal + '_stg..STG_' + @TableName + '_ROW_NUM r'
SET @SQL_Statement = @SQL_Statement + CHAR(10) + 'ON'
SET @SQL_Statement = @SQL_Statement + CHAR(10) + ' s.ROW_NUM = r.ROW_NUM'
SET @SQL_Statement = @SQL_Statement + CHAR(10) + 'INNER JOIN'
SET @SQL_Statement = @SQL_Statement + CHAR(10) + ' ' + @DatabaseNameLocal + '_int..INT_' + @TableName + ' i'
SET @SQL_Statement = @SQL_Statement + CHAR(10) + 'ON'
SELECT @SQL_Statement = @SQL_Statement + CHAR(10) + ' s.' + ccu.COLUMN_NAME + ' = i.' + ccu.COLUMN_NAME + ' AND'
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu ON tc.CONSTRAINT_NAME = ccu.Constraint_name
JOIN INFORMATION_SCHEMA.COLUMNS c ON ccu.TABLE_NAME = c.TABLE_NAME AND ccu.COLUMN_NAME = c.COLUMN_NAME
WHERE tc.CONSTRAINT_TYPE = 'Primary Key' and ccu.COLUMN_NAME NOT IN ('VALID_FROM_LOG_DATE', 'FILE_NAME') and ccu.TABLE_NAME = @LogTableName
SET @SQL_Statement = @SQL_Statement + CHAR(10) + ' 1 = 1'
SET @SQL_Statement = @SQL_Statement + CHAR(10) + 'WHERE'
SET @SQL_Statement = @SQL_Statement + CHAR(10) + ' s.HASH_VALUE <> i.HASH_VALUE;'
SET @SQL_Statement = @SQL_Statement + CHAR(10) + ''
SET @SQL_Statement = @SQL_Statement + CHAR(10) + 'SELECT @@ROWCOUNT;'
print @SQL_Statement
execute sp_executesql @SQL_Statement
END
Is there any solution to printing this query so that i can see it in a more readable/presentable manner?