peri hokiperihokiclarustologisticsrimashaop.comangelpaintingtools.co.invrjanaenergies.comcara scatter hitam maxwinspesifikasi scatter emas mahjongrtp mahjong wins 3 medanpola malam pgsoft cairkan 813jtkeberuntungan tukang parkir cek rtp mahjong ways lebarankisah inspiratif kuli bangunan menang mahjong winspeluang profit rtp mahjong ways 2 naiksopir angkot jackpot 89 juta mahjong winstemuan scatter jumat mahjong wayskeajaiban malam menghidupkan suasanamenang dengan gaya strategi cerdas mahjongputaran ajaib mahjong wins tumbang bahagiaraup untung dengan trik scatter mahjongscatter hitam mahjong ways mengantarkan kemenangan tak terdugafenomena scatter hitam saat jam sepi serverkemenangan besar dari scatter hitam mahjong wayspekerja serabutan raih jackpot mengagumkan di mahjong wayspotensi emas setiap spin mahjong wayswaktu spin terbaik untuk scatter hitamtukang becak raih rejeki di mahjong

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 282

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Cannot change session cookie parameters when headers already sent

Filename: Session/Session.php

Line Number: 294

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 304

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 314

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 315

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 316

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 317

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 375

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Cannot change save handler when headers already sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot start session when headers already sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

Lệnh rẻ nhánh IF ELSE trong SQL Server - Freetuts
VIEW
STORED PROCEDURE
CONDITION & STATEMENT
FUNCTION
TRIGGER
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

Lệnh rẻ nhánh IF ELSE trong SQL Server

Trong bài này bạn sẽ được học một lệnh rất hay dùng để rẻ nhánh chương trình đó là lệnh IF ELSE. Lệnh IF ELSE được sử dụng rất nhiều trong procedure, trigger, function vì nó rất linh động khi xây dựng một chương trình phức tạp.

test php

banquyen png
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

1. Lệnh IF

Lệnh if sẽ kiểm tra một biểu thức có đúng  hay không, nếu đúng thì thực thi nội dung bên trong của IF, nếu sai thì bỏ qua.

IF boolean_expression   
BEGIN
    { statement_block }
END

Trong đó:

  • Nội dung bên trong BEGIN ... END chính là phần thân của lệnh IF.
  • boolean_expression là biểu thức điều kiện, nếu giá trị biểu thức là TRUE thì phần thân sẽ được chạy, ngược lại thì bỏ qua

Hãy xem ví dụ dưới đây:

Bài viết này được đăng tại [free tuts .net]

BEGIN
    DECLARE @sales INT;
 
    SELECT 
        @sales = SUM(list_price * quantity)
    FROM
        sales.order_items i
        INNER JOIN sales.orders o ON o.order_id = i.order_id
    WHERE
        YEAR(order_date) = 2018;
 
    SELECT @sales;
 
    IF @sales > 1000000
    BEGIN
        PRINT 'Great! The sales amount in 2018 is greater than 1,000,000';
    END
END

Đoạn code này kiểm tra số tiền thu về trong năm 2008 có lớn hơn một triệu đô hay không, nếu hơn thì in ra thông báo "Great! The sales amount in 2018 is greater than 1,000,000".

2. Lệnh IF ELSE

Trường hợp điều kiện ở IF là false thì chương trình sẽ chạy ở phần ELSE.

IF Boolean_expression
BEGIN
    -- Statement block executes when the Boolean expression is TRUE
END
ELSE
BEGIN
    -- Statement block executes when the Boolean expression is FALSE
END

Mỗi lệnh IF đều có biểu thức điều kiện, tuy nhiên phần ELSE thì không cần vì nó là phần sẽ chạy nếu như phần IF không thỏa. Hãy xem ví dụ dưới đây:

BEGIN
    DECLARE @sales INT;
 
    SELECT 
        @sales = SUM(list_price * quantity)
    FROM
        sales.order_items i
        INNER JOIN sales.orders o ON o.order_id = i.order_id
    WHERE
        YEAR(order_date) = 2017;
 
    SELECT @sales;
 
    IF @sales > 10000000
    BEGIN
        PRINT 'Great! The sales amount in 2018 is greater than 10,000,000';
    END
    ELSE
    BEGIN
        PRINT 'Sales amount in 2017 did not reach 10,000,000';
    END
END

Ví dụ này viết lại ở trên, tuy nhiên mình bổ sung phần xuất thông báo ra màn hình như sau: Nếu tổng tiền lớn hơn 1 triệu đô thì in ra thông báo "Great! The sales amount in 2018 is greater than 10,000,000", ngược lại thì in ra thông báo "Sales amount in 2017 did not reach 10,000,000".

3. Lệnh IF ELSE lồng nhau

IF ELSE lồng nhau tức là lệnh IF này nằm bên trong một lệnh IF khác, các sử dụng này sẽ giúp chương trình rẻ rất nhiều nhánh khác nhau.

Hãy xem ví dụ dưới đây:

BEGIN
    DECLARE @x INT = 10,
            @y INT = 20;
 
    IF (@x > 0)
    BEGIN
        IF (@x < @y)
            PRINT 'x > 0 and x < y';
        ELSE
            PRINT 'x > 0 and x >= y';
    END 
END

Như bạn thấy lệnh IF ELSE rất hữu ích vì nó giúp chúng ta kiểm soát chương trình một cách hiệu quả, giải quyết được nhiều bài toán phức tạp.

Nếu bạn là lập trình viên và đã từng học qua các ngôn ngữ như C, C++ thì không còn xa lạ gì với lệnh này, chỉ là cú pháp nó được thể hiện ở SQL Server hơi khác.

Cùng chuyên mục:

Hàm CHAR trong SQL Server

Hàm CHAR trong SQL Server

Cách sử dụng hàm CHAR trong SQL Server

Hàm CHARINDEX trong SQL Server

Hàm CHARINDEX trong SQL Server

Cách sử dụng hàm CHARINDEX trong SQL Server

Hàm CONCAT trong SQL Server

Hàm CONCAT trong SQL Server

Cách sử dụng hàm CONCAT trong SQL Server

Hàm + trong SQL Server

Hàm + trong SQL Server

Cách sử dụng hàm + trong SQL Server

Hàm DATALENGTH trong SQL Server

Hàm DATALENGTH trong SQL Server

Cách sử dụng hàm DATALENGTH trong SQL Server

Hàm LEFT trong SQL Server

Hàm LEFT trong SQL Server

Cách sử dụng hàm LEFT trong SQL Server

Hàm LEN trong SQL Server

Hàm LEN trong SQL Server

Cách sử dụng hàm LEN trong SQL Server

Hàm LOWER trong SQL Server

Hàm LOWER trong SQL Server

Cách sử dụng hàm LOWER trong SQL Server

Hàm LTRIM trong SQL Server

Hàm LTRIM trong SQL Server

Cách sử dụng hàm LTRIM trong SQL Server

Hàm NCHAR trong SQL Server

Hàm NCHAR trong SQL Server

Cách sử dụng hàm NCHAR trong SQL Server

Hàm PATINDEX trong SQL Server

Hàm PATINDEX trong SQL Server

Cách sử dụng hàm PATINDEX trong SQL Server

Hàm REPLACE trong SQL Server

Hàm REPLACE trong SQL Server

Cách sử dụng hàm REPLACE trong SQL Server

Hàm RIGHT trong SQL Server

Hàm RIGHT trong SQL Server

Cách sử dụng hàm RIGHT trong SQL Server

Hàm RTRIM trong SQL Server

Hàm RTRIM trong SQL Server

Cách sử dụng hàm RTRIM trong SQL Server

Hàm SPACE trong SQL Server

Hàm SPACE trong SQL Server

Cách sử dụng hàm SPACE trong SQL Server

Hàm STR trong SQL Server

Hàm STR trong SQL Server

Cách sử dụng hàm STR trong SQL Server

Hàm UPPER trong SQL Server

Hàm UPPER trong SQL Server

Cách sử dụng hàm UPPER trong SQL Server

Hàm SUBSTRING trong SQL Server

Hàm SUBSTRING trong SQL Server

Cách sử dụng hàm SUBSTRING trong SQL Server

Hàm STUFF trong SQL Server

Hàm STUFF trong SQL Server

Cách sử dụng hàm STUFF trong SQL Server

Hàm ABS trong SQL Server

Hàm ABS trong SQL Server

Cách sử dụng hàm ABS trong SQL Server

Top