简体   繁体   English

在 SQL 中不使用多个 if else 的多个条件

[英]Multiple conditions without using multiple if else in SQL

I have to write a program in SQL that has multiple conditions but I shouldn't write it with multiple if else (because of the cost that if has).我必须在 SQL 中编写一个具有多个条件的程序,但我不应该用多个 if else 编写它(因为 if 的成本)。 I'm new in this field and I have no idea how else could I write it which would be better here is my sample code:我是这个领域的新手,我不知道我还能怎么写它会更好这是我的示例代码:

IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA= 'dbo' AND TABLE_NAME = 'Food_tbl')
BEGIN
    IF NOT EXISTS (SELECT TOP 1 FID FROM dbo.Food_tbl)
     BEGIN
        INSERT INTO dbo.Food_tbl
        SELECT *
        FROM DataFoodView                       
     END
    ELSE IF (SELECT COUNT(*) FROM dbo.Food_tbl)=20
     BEGIN
        PRINT N'Table Exists'
        SELECT *
        FROM Food_tbl
     END
    ELSE IF (SELECT COUNT(*) FROM dbo.FoodSara_tbl)<>20
     BEGIN
        print N'there isnt 20'
        INSERT INTO Food_tbl (Food_tbl.FID, Food_tbl.Fname, Food_tbl.Ftype, Food_tbl.Fcount, Food_tbl.Datetype, Food_tbl.Fdescription)
        SELECT DataFoodView.*
        FROM DataFoodView 
        LEFT JOIN FoodSara_tbl ON Food_tbl.FID = DataFoodView.FID
        WHERE Food_tbl.FID IS NULL;     
     END
END

PS: I have at first check if the table is exits and if it hasn't any record insert all the data, if it has 20 records show the table, if the table doesn't have 20 records find the missing data then insert that. PS:我首先检查表是否退出,如果没有任何记录插入所有数据,如果有 20 条记录显示表,如果表没有 20 条记录找到丢失的数据然后插入.

First, the condition in the ELSE part isn't needed: change首先,不需要 ELSE 部分的条件:change

ELSE IF (SELECT COUNT(*) FROM dbo.FoodSara_tbl)<>20
BEGIN

to

ELSE
BEGIN

Next, you may regroup the two INSERT parts because having no element is also having else than 20 elements.接下来,您可以重新组合这两个 INSERT 部分,因为没有元素也有 20 个以外的元素。

The result will be something like that:结果将是这样的:

IF EXISTS
(
    SELECT * FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_SCHEMA= 'dbo' AND TABLE_NAME = 'Food_tbl'
)
BEGIN
    IF (SELECT COUNT(*) FROM dbo.Food_tbl)=20
    BEGIN
        PRINT N'Table Exists'
        SELECT *
        FROM Food_tbl
    END
    ELSE
    BEGIN
        print N'there isnt 20'
        INSERT INTO Food_tbl (Food_tbl.FID, Food_tbl.Fname, Food_tbl.Ftype, 
Food_tbl.Fcount, Food_tbl.Datetype, Food_tbl.Fdescription)
        SELECT DataFoodView.*
        FROM DataFoodView 
        LEFT JOIN FoodSara_tbl ON Food_tbl.FID = DataFoodView.FID
        WHERE Food_tbl.FID IS NULL;     
    END
END

Not understood the exact requirement from the description what you have provided but just review the following logic从您提供的描述中无法理解确切的要求,但只需查看以下逻辑

IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA= 'dbo' AND TABLE_NAME = 'Food_tbl')
BEGIN
    DECLARE @RCount INT
    SELECT @RCount= COUNT(FID) FROM dbo.Food_tbl
    IF @RCount=0
     BEGIN
        //Your Logic 1 (for inserting data since no records found)                     
     END
    ELSE 
     BEGIN
        PRINT N'Table Exists'
        IF @RCount=20
        BEGIN
          //Your Logic 2 
        END
        ELSE
        BEGIN
          //Your Logic 3
        END

     END
   
END 

Please explain the requirement in details or build your queries based on the requirement and add in place of Logic 1,2,3请详细解释要求或根据要求构建查询并添加代替逻辑 1,2,3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM