Usage of stored procedures and differences with C # code

Source: Internet
Author: User

 

The following is a stored procedure application.

 

Set ANSI_NULLS ON

Set QUOTED_IDENTIFIER ON

GO

-- ===================================================== ======

-- Author: <Author, Name>

-- Create date: <Create Date,>

-- Description: <Description,>

-- ===================================================== ======

Create procedure <Procedure_Name, sysname, ProcedureName>

-- Add the parameters for the stored procedure here

@ Title nvarchar (100), -- news title

@ Content TEXT, -- news content

@ PubUser NVARCHAR (50), -- publisher

@ Catids varchar (200), -- news category list, separated ":"

@ Error nvarchar (200) OUTPUT -- used to return error information

AS

BEGIN

-- Set nocount on added to prevent extra result sets from

-- Interfering with SELECT statements.

Set nocount on;

Declare @ newsid int -- news id

Declare @ catid varchar (10) -- news category id

Declare @ pos int -- the position of the separator ":" In the category list

Begin transaction

Begin try

Insert into newscontent (title, [content], pubuser) values (@ title, @ content, @ pubUser)

Set @ newsid =@@ identity

While (len (@ catids)> 0)

Begin

Set @ pos = charindex (':', @ catids)

If (@ pos <> 0)

Begin

Set @ catid = substring (@ catids, 1, @ pos-1)

Set @ catids = substring (@ catids, @ pos + 1, @ pos)

End

Else

Begin

Set @ catid = @ catids

Set @ catids =''

End

Insert into newscategory (newsid, catid) values (@ newsid, cast (@ catid as int ))

End

Commit transaction

Return 0

End try

Begin catch

Set @ error = error_message ()

Rollback transaction

Return 1

End catch

END

 

The content of the stored statement must be written between begin and end.

For example:

If statement in c #

If (){};

The if statement in the Stored Procedure

If ()

Begin statement end

 

Write the catch statement

Begin catch

// Statement

End catch

 

Different methods are used to declare variables in the form of declare @ num nvarchar.

Use set to assign values

From: column yu851293483

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.