January 15, 2009 at 12:02 pm
Hi,
Please find our requirement below,
We have a staging table with the following structure
Tgt_stg:
---------
col1 varchar(100),
col2 varchar(100),
col3 varchar(100)
We have target table with the following desc
Tgt_tab:
---------
col1 Numeric(10),
col2 datetime,
col3 varchar(20)
We have a requirement to validate the values in stage table for all the records against the target table(i.e)
isnumeric(Row1.col1),isdate(Row1.col2),len (Row1.col3)<=20
isnumeric(Row2.col1),isdate(Row2.col2),len (Row2.col3)<=20
isnumeric(Row3.col1),isdate(Row3.col2),len (Row3.col3)<=20
isnumeric(Row4.col1),isdate(Row4.col2),len (Row4.col3)<=20
.
.
.
.
The records that failed to confirm the validation should redirected to a error table by concatenating all the error messages.
We are planning to write a SP for the same.As we are new SQL server,It could be helpful if you give the complete code.
Thanks.
January 15, 2009 at 12:24 pm
wouldn't you simply identify the offending rows like this?
SELECT *
FROM YOUR STAGINGTABLE
WHERE isnumeric(col1) = 0
OR isdate(col2) = 0
OR len (Row1.col3)> 20
Lowell
January 15, 2009 at 11:30 pm
Hi,
Thanks for reply.
The problem here we need to pass the table name dynamically.It means that the procedure should able to validate all the columns what ever the table we are passing.
For this purpose we are using 'INFORMATION_SCHEMA.COLUMNS' to get the metadata details for a table.We stuck up there.
We need a procedure for which the stage,target table names should be an argument.
Hope i cleared the requirement.
Thanks in advance.
Regards,
Muthu...
January 16, 2009 at 6:54 am
ok this nippet, based on using some FOR XML statements goves a SQL statement you could execute that looks like this:
SELECT * FROM MYSTAGE WHERE 1 = 2
OR ISNUMERIC(COUNTYTBLKEY) = 0
OR ISNUMERIC(INDEXTBLKEY) = 0
OR LEN(STATE) > 2
OR LEN(DESCRIP) > 30
OR LEN(CODE) > 3
OR ISNUMERIC(STATETBLKEY) = 0
OR ISNUMERIC(REGIONTBLKEY) = 0
OR ISNUMERIC(EDREGIONTBLKEY) = 0
OR ISNUMERIC(SPECDISTTBLKEY) = 0
this might get you started:
[font="Courier New"]DECLARE @STAGINGTABLE VARCHAR(125),
@TESTAGAINST VARCHAR(125)
SELECT @STAGINGTABLE ='MYSTAGE' ,
@TESTAGAINST ='TBCOUNTY'
SELECT
DISTINCT REPLACE(REPLACE('SELECT * FROM '
+ @STAGINGTABLE
+ ' WHERE 1 = 2 '
+ STUFF(
(
SELECT
' OR '
+ CASE
WHEN B.DATA_TYPE IN('varchar','nvarchar','char','nchar')
THEN ' LEN('+ B.COLUMN_NAME + ') > '+ CONVERT(VARCHAR,CHARACTER_MAXIMUM_LENGTH) + ' '
WHEN DATA_TYPE IN('datetime')
THEN ' ISDATE(' + B.COLUMN_NAME + ') = 0 '
WHEN DATA_TYPE IN('int','bigint','money','float','decimal','numeric')
THEN ' ISNUMERIC('+ B.COLUMN_NAME + ') = 0 '
END
FROM information_schema.columns B
WHERE A.TABLE_NAME = B.TABLE_NAME
FOR XML PATH(''))
,
1, 2, ' OR ') ,'>','>'
),'OR R ',' OR ') AS SQLSTATEMENT
FROM information_schema.columns A
WHERE TABLE_NAME = @TESTAGAINST
[/font]
Lowell
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply