存储过程中常使用的逻辑控制语句
--先申明个变量 。
DECLARE @Num INT;
SET @Num=0;
1. IF 语句
IF(@Num=0)
BEGIN
PRINT('Num value is '+CAST(@Num AS VARCHAR(4)));
END
2. WHILE 语句(当......的时候,执行循环体)
WHILE(@Num<>0)
BEGIN
PRINT('Num value is '+CAST(@Num AS VARCHAR(4)));
END
3. CASE 语句(特殊)
--创建测试临时表
IF(OBJECT_ID('tempdb..#Books') IS NOT NULL)
BEGIN
DROP TABLE #Books;
END
CREATE TABLE #Books
(
BookName NVARCHAR(20),
BookCode NVARCHAR(20)
)
INSERT INTO #Books( BookName, BookCode ) VALUES(N'C# 本质论','JC556874');
INSERT INTO #Books( BookName, BookCode ) VALUES(N'JS 本质论','JS556874');
--简单 Case 函数
SELECT
(
CASE BookCode
WHEN 'JC556874' THEN 'C#'
WHEN 'JS556874' THEN 'JS'
END
) CustFiled
FROM #Books
--搜索 Case 函数
SELECT
(
CASE
WHEN BookCode LIKE '%C%' THEN 'C#'
WHEN BookCode LIKE '%S%' THEN 'JS'
END
) CustFiled
FROM #Books
CASE 语句还可以放在 WHERE,GROUP 等中