lam*_*ora 3 sql t-sql sql-server stored-procedures switch-statement
我对t sql的经验很少,我必须写一个存储的.
这是我存储的:
USE myDatabase
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[myStored]
(
@myPar1 INT,
@myPar2 SMALLDATETIME
)
AS
BEGIN
SET NOCOUNT ON
IF EXISTS (
SELECT
1
FROM
myTable1
WHERE
myPar1 = @myPar1
AND myPar2 = @myPar2
)
BEGIN
DELETE FROM
myTable1
WHERE
myPar1 = @myPar1
AND myPar2 = @myPar2
END
ELSE
IF EXISTS (
SELECT
1
FROM
myTable2
WHERE
myPar2 = @myPar2
)
BEGIN
INSERT INTO
myTable1
(myField1, myField2, myField3, myField4)
VALUES
(@myPar1, @myPar2, '', 1)
END
ELSE
IF EXISTS (
SELECT
1
FROM
myTable3
WHERE
myPar2 = @myPar2
)
BEGIN
INSERT INTO
myTable1
(myField1, myField2, myField3, myField4)
VALUES
(@myPar1, @myPar2, '', 1)
END
END
Run Code Online (Sandbox Code Playgroud)
这些是我的问题:
1 - 是否存在宏观错误?
2 - 有人建议使用"SELECT CASE"其他人使用"IF ... ELSE",有什么区别?什么是我存储的最佳选择?
3 - 我不确定使用"BEGIN ... END"语句,特别是结合"IF ... ELSE"语句.这是什么意思?是否有必要在"IF ... ELSE"声明中加入"BEGIN ... END"?还用于执行单个指令?
对于单个IF声明
IF (Some Condition) --<-- If condition is true control will get inside the
BEGIN -- BEGIN ..END Block and execute the Code inisde
/* Your Code Here*/
END
Run Code Online (Sandbox Code Playgroud)
所有单一的IF语句都将独立检查条件.
ONE IF with ONE ELSE
IF (Some Condition) --<-- If condition is true control will get inside the
BEGIN -- BEGIN ..END Block and execute the Code inisde
/* Your Code Here*/ -- IF not true control will jump to Else block
END
ELSE --<-- You dont mention any condition here
BEGIN
/* Your Code Here*/
END
Run Code Online (Sandbox Code Playgroud)
只有一个代码块将执行IF true然后第一个块的Otherwsie ELSE代码块.
多个IF和ELSE
IF (Some Condition) --<--1) If condition is true control will get inside the
BEGIN -- BEGIN ..END Block and execute the Code inisde
/* Your Code Here*/ -- IF not true control will check next ELSE IF Blocl
END
ELSE IF (Some Condition) --<--2) This Condition will be checked
BEGIN
/* Your Code Here*/
END
ELSE IF (Some Condition) --<--3) This Condition will be checked
BEGIN
/* Your Code Here*/
END
ELSE --<-- No condition is given here Executes if non of
BEGIN --the previous IFs were true just like a Default value
/* Your Code Here*/
END
Run Code Online (Sandbox Code Playgroud)
只有第一个代码块才会被执行WHERE如果条件为真则休息将被忽略.
开始..END块
在任何IF,ELSE IF或ELSE之后如果你执行多个语句,你必须将它们包装在一个BEGIN..END块中.如果您只执行一个语句,则没有必要但是始终使用BEGIN END块是一个好习惯,这样可以更容易地读取代码.
你的程序
我已经取出ELSE语句使每个IF语句都检查给定的条件独立现在你有一些想法如何处理IF和ELSE所以请自己动手,因为我不知道你在这里尝试应用什么逻辑.
CREATE PROCEDURE [dbo].[myStored]
(
@myPar1 INT,
@myPar2 SMALLDATETIME
)
AS
BEGIN
SET NOCOUNT ON
IF EXISTS (SELECT 1 FROM myTable1 WHERE myPar1 = @myPar1
AND myPar2 = @myPar2)
BEGIN
DELETE FROM myTable1
WHERE myPar1 = @myPar1 AND myPar2 = @myPar2
END
IF EXISTS (SELECT 1 FROM myTable2 WHERE myPar2 = @myPar2)
BEGIN
INSERT INTO myTable1(myField1, myField2, myField3, myField4)
VALUES(@myPar1, @myPar2, '', 1)
END
IF EXISTS (SELECT 1 FROM myTable3 WHERE myPar2 = @myPar2)
BEGIN
INSERT INTO myTable1(myField1, myField2, myField3, myField4)
VALUES(@myPar1, @myPar2, '', 1)
END
END
Run Code Online (Sandbox Code Playgroud)