IfElse usage instance in SQL Server Stored Procedure

Source: Internet
Author: User
This section describes how to use ifesle in the SQL Server Stored Procedure for your reference. There are two tables in the database. The primary key of Table A is automatically increased and the foreign key of Table B is allowed to be empty. Now, you need to insert data to table B through programming, however, in a program, null values cannot be assigned to the Int type. If no value is assigned, the default value is 0. To solve this problem

This section describes how to use if esle in the SQL server Stored Procedure for your reference. There are two tables in the database. The primary key of Table A is automatically increased and the foreign key of Table B is allowed to be empty. Now, you need to insert data to table B through programming, however, in a program, null values cannot be assigned to the Int type. If no value is assigned, the default value is 0. To solve this problem

This section describes how to use if esle in the SQL server Stored Procedure for your reference. There are two tables in the database. The primary key of Table A is automatically increased and the foreign key of Table B is allowed to be blank.

Now we want to insert data to table B through programming, but in the program, it is not allowed to assign null values to the Int type. If no value is assigned, the default value is 0.
To solve this problem, If Else of the stored procedure is used. The following describes the complete stored procedure.

Sample Code:

The Code is as follows:


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 ()

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.