简体   繁体   中英

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). 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.

First, the condition in the ELSE part isn't needed: 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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