我在表格中有四列
Id Int
Name varchar(2)
Address varchar (4)
Active bit
和源表具有相同的列,但具有varchar数据类型。
Id varchar(100)
Name varchar(100)
Address varchar (100)
Active varchar(100)
我必须将数据从源表传输到目标表,但在传输时我需要检查我所拥有的行是否具有正确的目标数据类型。如果不是,我需要将完整的行传输到某个错误表。例如:
ID Name Address Active
1 A A 1
C B B 0
3 AAA C 1
4 D D 0
5 K K 102
如果上面重新表示源表,并且只有第1行和第4行有资格转移到目标表,则其他行将被移动到错误表(如果可能,可以使用有效描述)
答案 0 :(得分:2)
像这样的东西
insert into destination
select * from source
where (isnumeric(ID)=1
and
(ID not like '%[^0-9]%')
and
RIGHT('00000000000'+ID,10) <= '2147483647'
)
and
len(name)<=2
and
len(Address)<=4
and
active in ('0','1')
因此,要插入ERRORS表,请在WHERE中使用NOT
insert into ERRORS
select * from source
where
NOT
(
(isnumeric(ID)=1
and
(ID not like '%[^0-9]%')
and
RIGHT('00000000000'+ID,10) <= '2147483647'
)
and
len(name)<=2
and
len(Address)<=4
and
active in ('0','1')
)