Example of If Else usage in SQL Server Stored Procedure

Source: Internet
Author: User

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:

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

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.