我有1個databases 如下:
我需要在新增資料時檢查 Tel1 / Tel2 / Tel3 是否在我的 databases 內出現過.
謝謝幫忙
是這個吧?
若有沒出現過的資料就新增~
insert into databases(
[Order_No]
,[Customer Name]
,Tel1
,Tel2
,Tel3
)
select '' as [Order_No]
,'' as [Customer Name]
,'' as Tel1
,'' as Tel2
,'' as Tel3
where not exists(
select 0
from databases
where Tel1 = ''
or Tel2 = ''
or Tel3 = ''
)
CREATE TABLE [XX](
OrderNO NVARCHAR(20) NULL,
CustomerName NVARCHAR(20) NULL,
Tel1 NVARCHAR(20) NULL,
Tel2 NVARCHAR(20) NULL,
Tel3 NVARCHAR(20) NULL
);
INSERT INTO XX (OrderNO,CustomerName,Tel1,Tel2,Tel3)
VALUES
('EGS000001','j','88','22','33'),
('EGS000002','p','99','88','66'),
('EGS000003','m','45','22','55'),
('EGS000004','j','91','65','88'),
('EGS000001','n','45','66','22');
DECLARE @INPUT NVARCHAR(20)
SET @INPUT='22'
SELECT ISNULL(SUM(CASE WHEN TRIM(Tel1)=TRIM(@INPUT) THEN 1 ELSE 0 END +
CASE WHEN TRIM(Tel2)=TRIM(@INPUT) THEN 1 ELSE 0 END +
CASE WHEN TRIM(Tel3)=TRIM(@INPUT) THEN 1 ELSE 0 END),0) AS NUM
FROM XX
WHERE TRIM(Tel1)=TRIM(@INPUT) OR TRIM(Tel2)=TRIM(@INPUT) OR TRIM(Tel3)=TRIM(@INPUT)