SQL Drip 28-a simple stored procedure

Source: Internet
Author: User

Source: SQL Drip 28-a simple stored procedure

Write a data in a table a colleague wants to write information to another table, so a transaction is used. When you actually use it, you also inhale multiple pieces of data into one table at a time, and the following stored procedure splits the string into an array and writes it to the table.

/** * * * object:storedprocedure [dbo].    [Sp_insertemployee] Script date:09/17/2012 23:28:42 * * * * **/SETAnsi_nulls onGOSETQuoted_identifier onGO-- =============================================--Author: <Author,,Name>--Create Date: <create date,,>--Description: Inserting an employee data-- =============================================CREATE PROCEDURE [dbo].[Sp_insertemployee]--ADD The parameters for the stored procedure here@Name varchar( -),@UserName varchar( -),@Password varchar( -),@Hierarchy Char(1),@EmployeeTypeID int,@Sex varchar(5),@Telphone varchar( -),@CellPhone varchar( -),@QQ varchar( -),@Email varchar( -),@Statue varchar( -),@Remark varchar( -),@ManagerID int,@Regions varchar( +) asBEGIN--SET NOCOUNT on added to prevent extra result sets from--interfering with SELECT statements.SETNOCOUNT on;Declare @ID intDeclare @tempSql varchar( -)--Insert Statements for procedure hereif exists(Select *  fromEmployeewhereName=@Name)    return 0begin Transaction    Insert  into [Employee](    [Name],[UserName],[Password],[Hierarchy],[Employeetypeid],[Sex],[Telphone],[CellPhone],[QQ],[Email],[statue],[Remark],[ManagerID]    )Values(    @Name,@UserName,@Password,@Hierarchy,@EmployeeTypeID,@Sex,@Telphone,@CellPhone,@QQ,@Email,@Statue,@Remark,@ManagerID    )    Set @ID = @ @IDENTITY    if exists(Select *  fromPermissionwhereId=@ID)    begin        Delete  fromPermissionwhereEmployeeID=@ID    End    Else    begin        Set @tempSql='INSERT INTO Permission select'+Str(@ID)+', " "+Replace(@Regions,','," "Union Select'+Str(@ID)+'," ")+" '"        exec(@tempSql)    End    if @ @ERROR>0    begin    rollback Transaction    EndElse    begin    Commit Transaction    End    ENDGO

SQL Drip 28-A simple stored procedure

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.