Friday, June 22, 2012

A Little More Dynamic SQL - Condtitional WHERE

/*
TEST: Find Data
*/
--create a table variable to store the source/target mappings
DECLARE
@SourceTarget Table(ID INT Identity (1,1),TargetServer nVarchar(300),TargetDB nVarchar(300),TargetSchema nVarchar(300),TargetTable nVarchar(300),SourceServer Nvarchar (300),SourceDB nVarchar(300),SourceSchema nVarchar(300),SourceTable nVarchar(300),SourceKeyCols nVarchar(300),TrgKeyCols nVarchar(300))
--populate the table with the ETL table mappings for this project
INSERT INTO @SourceTarget(TargetServer ,TargetDB ,TargetSchema ,TargetTable,SourceServer ,SourceDB ,SourceSchema ,SourceTable
)
VALUES
(
(
'SERVER','DATABASE','dbo','Table1','Server2','databse2','schema','Table1.1'),'SERVER','DATABASE','dbo','Table2','Server2','databse2','schema','Table2.1')
--Declare the variables used in the test
DECLARE
@StartCnt
INT = 1,@MaxCnt INT = (SELECT COUNT (1) FROM @SourceTarget),@SourceServer nVarchar(100),@SourceDB nVarchar(100),@SourceSchema nVarchar(100),@SourceTable nVarchar(100),@TargetServer nVarchar(100),@TargetDB nVarchar(100),@TargetSchema
@TargetTable
nVarchar(100), nVarchar(100),@Today nVarchar(8),
--used to dynamically determine the column names for the row meta data
@Where nVarchar(MAX),
--use for creating the dynamic SQL
@sql
nVarchar(MAX)
WHILE
@StartCnt <= @MaxCnt
BEGIN
--set the values for each four part table name we want to testSET @SourceServer = (SELECT SourceServer FROM @SourceTarget WHERE ID = @StartCnt)SET @SourceDB = (SELECT SourceDB FROM @SourceTarget WHERE ID = @StartCnt)SET @SourceSchema = (SELECT SourceSchema FROM @SourceTarget WHERE ID = @StartCnt)SET @SourceTable = (SELECT SourceTable FROM @SourceTarget WHERE ID = @StartCnt)SET @TargetServer = (SELECT TargetServer FROM @SourceTarget WHERE ID = @StartCnt)SET @TargetDB = (SELECT TargetDB FROM @SourceTarget WHERE ID = @StartCnt)SET @TargetSchema = (SELECT TargetSchema FROM @SourceTarget WHERE ID = @StartCnt)SET @TargetTable = (SELECT TargetTable FROM @SourceTarget WHERE ID = @StartCnt)SET @Today = (SELECT CONVERT(CHAR(8), GetDate(), 112))IF @SourceTable IN ('table1')
BEGIN SET @Where = 'WHERE Created_Date > '''
SET @Where = @Where + @TodaySET @Where = @Where + ''' '
and DATEPART(mm, Created_Date) = DATEPART(mm, Updated_Date)
and DATEPART(yy, Created_Date) = DATEPART(YY, Updated_Date)'
SET @Where = @Where + 'and DATEPART(dd, Created_Date) = DATEPART(dd, Updated_Date)

ELSE
IF
END @SourceTable IN ('tabel2')
BEGINSET @Where = 'WHERE created_date > '''
SET @Where = @Where + @Today SET @Where = @Where + ''' '
and DATEPART(mm, Created_date) = DATEPART(mm, Updated_date)
and DATEPART(yy, Created_date) = DATEPART(YY, Updated_date)'
SET @Where = @Where + 'and DATEPART(dd, Created_date) = DATEPART(dd, Updated_date)

ELSE
END
BEGINSET @Where = 'WHERE createdDate > '''
SET @Where = @Where + @TodaySET @Where = @Where + ''' '
DATEPART(dd, CreatedDttm) = DATEPART(dd, UpdatedDttm)
AND DATEPART(mm, CreatedDttm) = DATEPART(mm, UpdatedDttm)
AND DATEPART(yy, CreatedDttm) = DATEPART(YY, UpdatedDttm)
)
OR (
DATEPART(dd, CreatedDttm) = DATEPART(dd, DeletedDttm)
AND DATEPART(mm, CreatedDttm) = DATEPART(mm, DeletedDttm)
AND DATEPART(yy, CreatedDttm) = DATEPART(YY, DeletedDttm)
)
OR (
DATEPART(dd, UpdatedDttm) = DATEPART(dd, DeletedDttm)
AND DATEPART(mm, UpdatedDttm) = DATEPART(mm, DeletedDttm)
AND DATEPART(yy, UpdatedDttm) = DATEPART(YY, DeletedDttm)
)
'
SET @Where = @Where + 'AND (
END
SET @sql = ' + @SourceServer + '.' + @SourceDB + '.' + @SourceSchema + '.' + @SourceTable+ space(1) + 'WITH (NOLOCK) '


'
+ space(1) + @Where + '


PRINT
EXEC sp_executesql @sql OUTPUT @Today PRINT @WhereSET @StartCnt = @StartCnt +1;END

SELECT * FROM '





 
 

 

No comments:

Post a Comment