我正在尝试执行插入操作,并且在尝试使用SS#在where语句中添加附加子句时收到错误。我正在复制的表是所有varchar列,它正常工作,直到我添加行来检查SS#的匹配。我理解得到转换错误,所以我尝试在子查询中使用转换函数,然后我得到截断错误。
--Grab the max record_serial_number from DESTINATION
declare @rsnOwner numeric
set @rsnOwner = (select max(RECORD_SERIAL_NUMBER)
from DESTINATION)
--Insert values from TEMP to DESTINATION
insert into DESTINATION
(
RECORD_SERIAL_NUMBER,
AGENT_NUMBER,
SS_NO,
LAST_NAME_OWNER,
FIRST_NAME_OWNER,
TITLE,
BIRTHDAY,
HOME_STREET_ADDRESS,
HOME_TOWN,
HOME_STATE,
HOME_ZIP_CODE,
HOME_PHONE,
BANKRUPT,
MODIFY_DATE,
MODIFY_TIME,
USER_ID,
SHARE_PCT,
CELL_PHONE,
CONTACT_TYPE,
CONTACT_DESC,
CH_APPROVED,
CH_DATE,
ADDRESS_VERIFY,
VERIFY_DATE
)
Select
@rsnOwner + ROW_NUMBER() over (order by agent_number),
convert(numeric, AGENT_NUMBER),
convert(numeric, replace(ss_num,'-','')), --remove dashes and convert
CONTACT_LAST_NAME,
CONTACT_FIRST_NAME,
CONTACT_TITLE,
case when isdate(DOB)=1 then convert(datetime, '19'+right(DOB, 2)+left(DOB, 2)+substring(DOB,3,2)) else null end, --rearrange the 6 digit date to 8 digit in YYYYMMDD format
ADDRESS1,
CITY,
STATE,
case when isnumeric(ZIP_CODE)=1 then convert(numeric, ZIP_CODE) else null end,
case when isnumeric (CONTACT_PHONE_NUM)=1 then replace(convert(numeric, CONTACT_PHONE_NUM),'-','') else null end, --remove dashes and convert
')', --BANKRUPT
convert(datetime, GetDate()) as MODIFY_DATE,
replace(Convert (varchar(8), GetDate(), 108),':',''),
'dbo_update', --USERID
cast(PERCENT_OWNERSHIP as numeric (5,2)) as PERCENT_OWNERSHIP,
case when isnumeric(CONTACT_CELL_NUM)=1 then replace(convert(numeric, CONTACT_CELL_NUM),'-','') else null end, --remove dashes and convert
TYPE,
TYPE_DESC,
CH_APPROVED,
case when isdate(CH_DATE)=1 then convert(datetime, '20'+right(CH_DATE, 2)+left(CH_DATE, 2)+substring(CH_DATE,3,2)) else null end, --rearrange the 6 digit date to 8 digit in YYYYMMDD format
ADD_VERIFICATION,
case when isdate(ADD_VERIFICATION_DATE)=1 then convert(datetime, '20'+right(ADD_VERIFICATION_DATE, 2)+left(ADD_VERIFICATION_DATE, 2)+substring(ADD_VERIFICATION_DATE,3,2)) else null end --rearrange the 6 digit date to 8 digit in YYYYMMDD format
from TEMP as tmp
where NOT EXISTS(select NULL
from DESTINATION as dest
where tmp.AGENT_NUMBER = dest.AGENT_NUMBER
and
convert(numeric, replace(SS_NUM,'-','')) = dest.SS_NO
)
这些表没有主键,使用agent_number或SS#是多对多的关系,但它们中的两个是唯一的。我也尝试为SS_NUM创建一个变量,所以我可以在select和where子句中使用它,但显然我不能这样做,并且必须为我的select中的所有内容创建变量。