Home > Database > Mysql Tutorial > body text

sqlserver 存储过程中If Else的用法实例

WBOY
Release: 2016-06-07 16:19:06
Original
2993 people have browsed it

为大家介绍sql server存储过程中if esle的用法,供大家学习参考。数据库中有两张表,A表主键为自动增长的并且是B表的外键且允许为空 现在要通过编程向B表中插入数据,可是在程序中是不允许给Int类型赋空值的如果不赋值就默认为0。 为了解决这个问题,用到了

为大家介绍sql server存储过程中if esle的用法,供大家学习参考。数据库中有两张表,A表主键为自动增长的并且是B表的外键且允许为空

 

现在要通过编程向B表中插入数据,,可是在程序中是不允许给Int类型赋空值的如果不赋值就默认为0。
为了解决这个问题,用到了存储过程的If Else,下面是完整的存储过程。

代码示例:

复制代码 代码如下:


create PROCEDURE [dbo].[P_Form_Control_Info_Add]
    @TypeName varchar(20),
    @Description varchar(50),
    @CtlColSpan int,
    @Sort int,
    @SourceID int,
    @FieldID int,
    @TableID int
AS
if @SourceID = 0
begin
INSERT INTO T_Form_Control_Info (
    [TypeName],
    [Description],
    [CtlColSpan],
    [Sort],
    [FieldID],
    [TableID]
) VALUES (
    @TypeName,
    @Description,
    @CtlColSpan,
    @Sort,
    @FieldID,
    @TableID
)
end
else
begin
INSERT INTO T_Form_Control_Info (
    [TypeName],
    [Description],
    [CtlColSpan],
    [Sort],
    [SourceID],
    [FieldID],
    [TableID]
) VALUES (
    @TypeName,
    @Description,
    @CtlColSpan,
    @Sort,
    @SourceID,
    @FieldID,
    @TableID
)
end
return SCOPE_IDENTITY()

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!