我对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
这些是我的问题:
1 - 是否存在宏观错误?
2 - 有人建议使用“SELECT CASE”其他人使用“IF ... ELSE”,有什么区别?什么是我存储的最佳选择?
3 - 我不确定使用“BEGIN ... END”语句,特别是结合“IF ... ELSE”语句。这是什么意思?是否有必要在“IF ... ELSE”声明中加入“BEGIN ... END”?还用于执行单个指令吗?
答案 0 :(得分:8)
对于单个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
所有单一的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
只有一个代码块将执行IF,然后执行第一个块的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
只有第一个代码块才会被执行WHERE如果条件为真,则将忽略休息。
BEGIN ..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
答案 1 :(得分:3)
答案 2 :(得分:2)
此脚本中没有错误。
案例陈述用于表达式评估,而不用于语句执行。因此,不能用于当前的要求。有关案例陈述的更多详细信息,请查看http://msdn.microsoft.com/en-us/library/ms181765.aspx
通常,陈述可以是单一或复合。复合词是语句的组合。对于IF条件,可以指定单个或复合语句,SQL服务器选择将其分组到BEGIN .. END(与其他少数数据库/编程语言不同)。 ELSE也是如此。因此,IF应该跟随BEGIN ... END和ELSE之后应该是BEGIN ... END。有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/ms182717(v=sql.120).aspx