当我运行以下查询时:
update ri_res
set name_last = 'onion',
name_first = 'Forest',
addr1 = '2872 Loki Laufeyson',
city = 'Queens',
[state] = 'NY',
zip = '11374',
county = 'West',
phone = '4152366293',
ssn = '912308719',
sex = 'M',
birthplace = 'Madison',
birth_date = '3/1/1947',
uniqueid = newid()
where cono = 'JC' and resno = '216'
我遇到以下错误:
消息245,级别16,状态1,过程SYNC_ri_res_to_WC_PersonAddress, 第24行[Batch Start Line 0]转换 varchar值'1094_2'转换为int数据类型。
下面列出的是存储过程:
USE [WestCounty]
GO
/****** Object: StoredProcedure [Base360].[SYNC_ri_res_to_WC_PersonAddress] Script Date: 2/25/2019 9:12:39 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [Base360].[SYNC_ri_res_to_WC_PersonAddress]
@ConoResnos XML
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Payload as XML = @ConoResnos;
/* Address (ri_res address fields) */
INSERT INTO Base360.Address_Base (Address1, Address2, City, AddressStateID, Zipcode, AddressCountyID, EnterpriseID)
select distinct i.addr1, i.addr2, i.city, S.AddressStateID, i.zip, county.AddressCountyID,(select top 1 EnterpriseID from Base360.Facility_Base where FacilityID > 0)
FROM ri_res i with(nolock)
INNER JOIN (SELECT x.value('cono[1]','varchar(4)') as cono, x.value('resno[1]','INT') as resno FROM @Payload.nodes('/Inserted/Row') AS T(x)) as cr
on cr.cono = i.cono and cr.resno = i.resno
LEFT OUTER JOIN [Base360].[AddressState] S ON i.state = S.StateCode
LEFT OUTER JOIN Base360.AddressCounty county on i.county = county.CountyName and S.AddressStateID = county.AddressStateID
LEFT OUTER JOIN [Base360].[Address_Base] A with(nolock)
ON ISNULL(addr1,'') = ISNULL(A.Address1 ,'')
AND isnull(addr2,'') = isnull(A.Address2,'')
AND isnull(S.AddressStateID,'') = ISNULL(A.AddressStateID,'')
AND isnull(i.city,'') = isnull(A.City,'')
AND isnull(SUBSTRING(i.[zip], 1, 5),'') = isnull(SUBSTRING(A.ZipCode,1,5),'')
AND isnull(county.AddressCountyID,'') = ISNULL(A.AddressCountyID,'')
WHERE A.AddressID IS NULL and (i.addr1 is not null or i.addr2 is not null OR i.city is not null or i.state is not null or i.zip is not null or i.county is not null)
UPDATE PA SET EndDAte = GetUTCDate()
FROM ri_res rc with(nolock)
INNER JOIN (SELECT x.value('cono[1]','varchar(4)') as cono, x.value('resno[1]','INT') as resno FROM @Payload.nodes('/Inserted/Row') AS T(x)) as cr
on cr.cono = rc.cono and cr.resno = rc.resno
INNER JOIN Base360.PersonIdentification PID with(nolock) on rc.cono = SUBSTRING(PID.PersonIdentificationValue, 0,(charindex('_',PID.PersonIdentificationValue)))
AND rc.resno = CAST(SUBSTRING(PID.PersonIdentificationValue, (charindex('_',PID.PersonIdentificationValue))+1,10) as int)
AND PID.PersonIdentificationTypeID = -1
INNER JOIN Base360.PersonAddress PA on PID.PersonID = PA.PersonID and PA.AddressTypeID = -2 and PA.EndDate IS NULL
;with addyCTEInsert2 as (
select ROW_NUMBER() OVER (Partition by isnull(A.Address1,''), isnull(A.Address2,''), isnull(A.City,''), ISNULL(A.AddressStateID,''), isnull(SUBSTRING(A.ZipCode,1,5),''), isnull(county.CountyName,'') ORDER BY CreateUTCDate) as RowNum,
A.AddressID, A.Address1, A.Address2, A.City, A.AddressStateID, A.Zipcode, county.AddressCountyID
FROM ri_res i with(nolock)
INNER JOIN (SELECT x.value('cono[1]','varchar(4)') as cono, x.value('resno[1]','INT') as resno FROM @Payload.nodes('/Inserted/Row') AS T(x)) as cr
on cr.cono = i.cono and cr.resno = i.resno
LEFT OUTER JOIN [Base360].[AddressState] S ON i.state = S.StateCode
LEFT OUTER JOIN Base360.AddressCounty county on i.county = county.CountyName and S.AddressStateID = county.AddressStateID
INNER JOIN [Base360].[Address_Base] A with(nolock)
ON ISNULL(addr1,'') = ISNULL(A.Address1 ,'')
AND isnull(addr2,'') = isnull(A.Address2,'')
AND isnull(S.AddressStateID,'') = ISNULL(A.AddressStateID,'')
AND isnull(i.city,'') = isnull(A.City,'')
AND isnull(SUBSTRING(i.[zip], 1, 5),'') = isnull(SUBSTRING(A.ZipCode,1,5),'')
AND isnull(county.AddressCountyID,'') = ISNULL(A.AddressCountyID,'')
WHERE (i.addr1 is not null or i.addr2 is not null OR i.city is not null or i.state is not null or i.zip is not null or i.county is not null)
)
INSERT INTO Base360.PersonAddress ([PersonID], [AddressID], [AddressTypeID], [BeginDate], [EnterpriseID])
select distinct PB.PersonID, AB.AddressID, -2 AS AddressTypeID, GetUTCDate() AS BeginDate, PB.EnterpriseID
FROM ri_res rc with(nolock)
INNER JOIN Base360.PersonIdentification PID with(nolock) on rc.cono = SUBSTRING(PID.PersonIdentificationValue, 0,(charindex('_',PID.PersonIdentificationValue)))
AND rc.resno = CAST(SUBSTRING(PID.PersonIdentificationValue, (charindex('_',PID.PersonIdentificationValue))+1,10) as int)
AND PID.PersonIdentificationTypeID = -1
INNER JOIN [Base360].[Person_Base] PB with(nolock) ON PID.PersonID = PB.PersonID
LEFT OUTER JOIN [Base360].[AddressState] S ON rc.state = S.StateCode
LEFT OUTER JOIN Base360.AddressCounty county on rc.county = county.CountyName and s.AddressStateID = county.AddressStateID
INNER JOIN addyCTEInsert2 AB
ON ISNULL(rc.addr1,'') = ISNULL(AB.Address1 ,'')
AND isnull(rc.addr2,'') = isnull(AB.Address2,'')
AND isnull(S.AddressStateID,'') = ISNULL(AB.AddressStateID,'')
AND isnull(rc.city,'') = isnull(AB.City,'')
AND isnull(SUBSTRING(rc.[zip], 1, 5),'') = isnull(SUBSTRING(AB.ZipCode,1,5),'')
AND isnull(county.AddressCountyID,'') = ISNULL(AB.AddressCountyID,'')
and ab.rownum = 1
INNER JOIN (SELECT x.value('cono[1]','varchar(4)') as cono, x.value('resno[1]','INT') as resno FROM @Payload.nodes('/Inserted/Row') AS T(x)) as cr
on cr.cono = rc.cono and cr.resno = rc.resno
LEFT OUTER JOIN Base360.PersonAddress pa with(nolock) on PB.PersonID = pa.personid and pa.AddressTypeID = -2 and pa.EndDate is null and AB.AddressID = pa.AddressID
WHERE pa.PersonAddressID IS NULL AND (rc.[name_first] IS NOT NULL OR rc.[name_last] IS NOT NULL)
END