Usage instances of if else in SQL Server stored procedures

Source: Internet
Author: User
Tags sort
To introduce you to the use of if esle in SQL Server stored procedures for your reference. There are two tables in the database, the primary key for table A is automatically growing and is the foreign key of table B and is allowed to be empty

Now you can programmatically insert data into table B, but in your program you are not allowed to assign null values to the INT type if you do not assign a value to default to 0.
To solve this problem, the if Else of the stored procedure is used, and the following is the complete stored procedure.

code example:

Copy Code code as follows:


Create PROCEDURE [dbo].[ P_form_control_info_add]
    @TypeName varchar,
    @Description varchar ( ,
    @CtlColSpan int,
    @Sort int,
    @SourceID int,
    @FieldID int,
    @TableID int
as
if @SourceID = 0
Begin
INSERT Into T_form_control_info (
    [TypeName],
    [Description],
  & nbsp [Ctlcolspan],
    [Sort],
    [FieldID],
    [TableID]
) VALUES (
    @TypeName,
    @Description,
    @CtlColSpan,
    @Sort,
    @FieldID,
    @TableID
)
End
Else br> begin
INSERT into T_form_control_info (
    [TypeName],
    [Description] , the
    [Ctlcolspan],
    [Sort],
    [SourceID],
    [FieldID],
    [TableID]
) VALUES (
    @TypeName,
    @Description,
    @CtlColSpan,
    @Sort,
    @SourceID,
     @FieldID,
    @TableID
)
End
Return scope_identity ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.