我有6個表:LS_CLIENT_INSEE_A,B,C等。每個表只包含1個字段:INSEE。SQL CASE SELECT返回字符
我只想知道我的輸入是否存在於其中一個表中,並從表名(A,B,C等)返回相應的字母。
是否有其他原因重寫此:
@TheInsee int
AS
BEGIN
declare @Zone char(1)
declare @CountA int
declare @CountB int
declare @CountC int
declare @CountD int
declare @CountF int
declare @CountP int
SELECT @CountA = COUNT(*) FROM LS_CLIENT_INSEE_A WHERE NO_INSEE = @TheInsee
SELECT @CountB = COUNT(*) FROM LS_CLIENT_INSEE_B WHERE NO_INSEE = @TheInsee
SELECT @CountC = COUNT(*) FROM LS_CLIENT_INSEE_C WHERE NO_INSEE = @TheInsee
SELECT @CountD = COUNT(*) FROM LS_CLIENT_INSEE_D WHERE NO_INSEE = @TheInsee
SELECT @CountF = COUNT(*) FROM LS_CLIENT_INSEE_F WHERE NO_INSEE = @TheInsee
SELECT @CountP = COUNT(*) FROM LS_CLIENT_INSEE_P WHERE NO_INSEE = @TheInsee
set @Zone =
CASE
WHEN @CountA > 0 THEN 'A'
WHEN @CountB > 0 THEN 'B'
WHEN @CountC > 0 THEN 'C'
WHEN @CountD > 0 THEN 'D'
WHEN @CountF > 0 THEN 'F'
WHEN @CountP > 0 THEN 'P'
END
END
是上面的查詢實現這一目標的最佳途徑?
感謝你在前進, 甜菊
我建議你考慮規範化這個數據庫! – Simon