Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
SQL

Hi, Below is my requirement.

I have a staging table tbl_Staging and a master table tbl_Master with same schema as staging table.

The staging table has 1 row in it as below

tbl_Staging
ID Name Status Address1 Address2 Address3 Telephone
-- ---- ------ -------- -------- -------- ---------
1 XYZ Active asdf xyz hju 1234

tbl_Master
ID Name Status Address1 Address2 Address3 Telephone
-- ---- ------ -------- -------- -------- ---------

Another table tbl_Validation having only 1 column "FieldName". The values for this column are the column names of tbl_Staging.

tbl_Validation
FieldName
---------
Name
Status
Telephone

Now i want to validate the data in tbl_Staging for the columns configured in tbl_Validation (Name, Status and Telephone fields) before pushing into tbl_Master table.

Let me know how to implement this in SQL Server

Thanks in advance.
Posted
Updated 23-Sep-14 20:21pm
v2
Comments
RossMW 24-Sep-14 1:54am    
How is the validation table configured. If you are looking for the same record in the validation table then you can join on the staging table and the validation table and insert on the correct data that matches.
syam217 24-Sep-14 2:22am    
I have updated my question with more details. Please have a look and let me know. Thanks.
syed shanu 24-Sep-14 2:15am    
You can do this by using Stored Procedure .If you could provide more details of your 3 tables and sample data It will be more easy to do it.
syam217 24-Sep-14 2:22am    
I have updated my question with more details. Please have a look and let me know. Thanks.
syed shanu 24-Sep-14 2:27am    
What kind of validation you want to do for example tbl_Staging column Name
Status
Telephon should not be empty ?

1 solution

Hi,

I have a made a sample for you.
As per your Table i have created and Inserted the sample Data.

SQL
-- Create your sample table

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) 
);

-- Your Sample Insert Query.

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.

SQL
--select * from tbl_Staging

--select * from tbl_Validation


DECLARE @fieldName varchar(100)
Declare @Result varchar(100)
Declare @Field_Data varchar(100)
DECLARE @SQLQuery nvarchar(max)
 
DECLARE Validation_Cursor CURSOR -- Declare cursor

LOCAL SCROLL STATIC
 
FOR
 
Select FieldName FROM tbl_Validation
 
OPEN Validation_Cursor -- open the 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;

EXECUTE sp_executesql @SQLQuery, N'@Result varchar(max) out', @Field_Data out

--select @Field_Data as Validation_result,LEN(@Field_Data) Length_Validation

	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  -- print the name
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900