以下查询完美无缺,
insert into [EGallery].dbo.[CustomerDetails]
Select Distinct B.CountyB as 'Mobile' , Cast(BuildingB as Varchar(100)) as 'Email' ,
A.CardCode , A.CardName as 'First Name' , '' as 'Last Name' ,
'' as Gender , Cast(A.Address as Varchar(1000)) as 'Address' , Convert(Varchar(10), A.U_BirthDay,105) as 'birthday' ,
Convert(Varchar(10), A.U_AnnivDay ,105) as 'Anniversary' ,
Case
When A.CardCode Like '%%'+ C.WhsCode +'%%' Then Convert(Varchar(10) , A.DocDate ,105)
Else Convert(Varchar(10), (Select X.CreateDate From OCRD X Where X.CardCode = A.CardCode) ,105) End as 'JoinDate' ,
C.WhsCode as 'JoinStore','Open' as Status ,(Select GETDATE()) as CreatedDateTime,(Select GETDATE()) as ProcessDateTime, '' as StatusMSg
From OINV A
Inner Join INV12 B On A.DocEntry = B.DocEntry
Inner Join INV1 C On A.DocEntry = C.DocEntry
Where C.LineNum = '0'
--B.CountyB not in(select D.Mobile from [EGallery].dbo.[CustomerDetails] D where D.Mobile=B.CountyB)
--not exists (select Mobile from [EGallery].dbo.[CustomerDetails] D where D.Mobile=B.CountyB)
但在我将记录插入[EGallery].dbo.[CustomerDetails]
表之前,我需要检查表中是否已存在电话号码。如果记录已存在,则无需再次插入。为此,我添加了一个条件(我在查询中注释掉了)但是它在运行查询时报告了这个错误:
Cannot resolve the collation conflict between "SQL_Latin1_General_CP850_CI_AS" and "Latin1_General_CI_AI" in the equal to operation.
答案 0 :(得分:1)
根据here,您必须将COLLATE DATABASE_DEFAULT
添加到这样的查询中:
Where C.LineNum = '0' AND
B.CountyB not in(select D.Mobile from [EGallery].dbo.[CustomerDetails] D where D.Mobile COLLATE DATABASE_DEFAULT = B.CountyB COLLATE DATABASE_DEFAULT) AND
not exists (select Mobile from [EGallery].dbo.[CustomerDetails] D where D.Mobile COLLATE DATABASE_DEFAULT = B.CountyB COLLATE DATABASE_DEFAULT)
答案 1 :(得分:1)
尝试在查询之前执行此操作:
USE [db name for object INV12]
GO
ALTER TABLE [EGallery].dbo.[CustomerDetails]
ALTER COLUMN Mobile
VARCHAR(100) COLLATE Latin1_General_CI_AS NOT NULL
ALTER TABLE INV12
ALTER COLUMN CountyB
VARCHAR(100) COLLATE Latin1_General_CI_AS NOT NULL
UPDATE1: 如果您对其中一个列或两个列都有索引,则需要删除它并在更改新的排序规则后再次创建索引。
答案 2 :(得分:0)
我建议您使用MERGE
语句,因为它
根据与源表的连接结果对目标表执行插入,更新或删除操作。例如,您可以根据另一个表中的差异在一个表中插入,更新或删除行来同步两个表。