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