Home > Database > Mysql Tutorial > sqlserver 存储过程中If Else的用法实例

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 16:19:06
Original
3093 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:
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
Latest Issues
Problem with tp6 connecting to sqlserver database
From 1970-01-01 08:00:00
0
0
0
Unable to connect to SQL Server in Laravel
From 1970-01-01 08:00:00
0
0
0
Methods of parsing MYD, MYI, and FRM files
From 1970-01-01 08:00:00
0
0
0
SQLSTATE: User login failed
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template