Hi,
I have a made a sample for you.
As per your Table i have created and Inserted the sample Data.
create table tbl_Staging (
ID int not null primary key,
Name varchar(100) ,
Status varchar(10) ,
Address1 varchar(100) ,
Address2 varchar(100) ,
Address3 varchar(100) ,
Telephone varchar(20)
);
create table tbl_Master (
ID int not null primary key,
Name varchar(100) ,
Status varchar(10) ,
Address1 varchar(100) ,
Address2 varchar(100) ,
Address3 varchar(100) ,
Telephone varchar(20)
);
create table tbl_Validation (
FieldName varchar(100)
);
insert into tbl_Staging ( ID,
Name ,
Status,
Address1,
Address2,
Address3,
Telephone) values
(1, 'XYZ', 'Active', 'asdf', 'xyz' ,'hju' ,'1234')
insert into tbl_Validation (FieldName) values
('Name'),
('Status'),
('Telephone')
Now i have used Cursor here to fetch each record from your validation Table.
And used the Dynamic Query in Cursor and pass each row Column Name to Staging Table and get the result data and check for the validation.
DECLARE @fieldName varchar(100)
Declare @Result varchar(100)
Declare @Field_Data varchar(100)
DECLARE @SQLQuery nvarchar(max)
DECLARE Validation_Cursor CURSOR
LOCAL SCROLL STATIC
FOR
Select FieldName FROM tbl_Validation
OPEN Validation_Cursor
FETCH NEXT FROM Validation_Cursor
INTO @fieldName
PRINT @fieldName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQLQuery=N'select @Result = ' + @fieldName + ' from tbl_Staging';
EXECUTE sp_executesql @SQLQuery, N'@Result varchar(max) out', @Field_Data out
IF(@fieldName='Telephone')
BEGIN
IF(LEN(@Field_Data) < 6)
BEGIN
Select 'You can not Insert Here'
END
ELSE
BEGIN
Select 'Your Insert Query'
END
END
FETCH NEXT FROM Validation_Cursor
INTO @fieldName
PRINT @fieldName
END
CLOSE Validation_Cursor
DEALLOCATE Validation_Cursor
i have done the validation only for checking the telephone rest you can do same like above .hope this will help you.