版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27790011/article/details/84832447
写的存储过程,接收的参数的一个字符串,通过分割字符串从而实现批量删除。
IF (@operation = 'delete')
BEGIN
DECLARE @temp nvarchar(200)
DECLARE @splitchar nvarchar(1)
SET @splitchar=','
SET @userId=@userId+@splitchar
WHILE CHARINDEX(@splitchar,@userId)>0
BEGIN
SET @temp=SUBSTRING(@userId,1,CHARINDEX(@splitchar,@userId)-1)
IF(LEN(@temp)>0)
BEGIN
PRINT(@temp)
DELETE from TB_user WHERE user_id =@temp;
END
SET @userId=RIGHT(@userId,LEN(@userId)-CHARINDEX(@splitchar,@userId))
PRINT(@userId)
END
END
用到的函数有:CHARINDEX();LEN();RIGHT();SUBSTRING()
charindex(expression1 , expression2 )表示:
从expression2字符串中指定的位置处开始查找是否包含expression1字符串。如果能够从expression2字符串中查找到expression1字符串则返回expression1在
expression2出现的位置;反之,返回0
LEN(expression1 )表示:返回expression1 的长度;
RIGHT(expression,number)表示:从expression右边开始,返回长度为number的字符串。
SUBSTRING(expression, start, length)表示:返回从expression的start位置开始截取长度为length 的字符串。
附上完整的存储过程,是用的操作的,包含增加用户,修改用户,批量删除用户。
文件:sp_query_掌上微课_用户操作.sql
USE [ZSWK]
GO
/****** Object: StoredProcedure [dbo].[sp_query_掌上微课_用户操作] Script Date: 12/05/2018 11:10:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- exec sp_query_掌上微课_用户操作 'insert','','20181204测试','123456','','测试001','男','','','1','','','123',''
-- exec sp_query_掌上微课_用户操作 'update','10','test001','','','','','','null','','','','',''
-- exec sp_query_掌上微课_用户操作 'delete','11','null','null','null','null','null','null','null','null','null','null','null','null'
ALTER PROC [dbo].[sp_query_掌上微课_用户操作]
@operation varchar(255),
@userId varchar(255),
@userName varchar(255),
@password varchar(255),
@phone varchar(255),
@fullname varchar(255),
@sex varchar(255),
@area varchar(255),
@createAt varchar(255),
@isTeacher varchar(255),
@teacherInfo varchar(4000),
@icon varchar(4000),
@empNumber varchar(255),
@userAccount varchar(255)
AS
BEGIN
SET NOCOUNT ON
IF (@operation = 'insert')
BEGIN
INSERT INTO TB_user(user_name,password,fullname,phone,sex,area,create_at,isTeacher,teacher_info,icon,empNumber,userAccount)
VALUES (@userName,@password, @fullname, @phone,@sex,@area,@createAt,@isTeacher,@teacherInfo,@icon,@empNumber,@userAccount)
END
IF (@operation = 'delete')
BEGIN
DECLARE @temp nvarchar(200)
DECLARE @splitchar nvarchar(1)
SET @splitchar=','
SET @userId=@userId+@splitchar
WHILE CHARINDEX(@splitchar,@userId)>0
BEGIN
SET @temp=SUBSTRING(@userId,1,CHARINDEX(@splitchar,@userId)-1)
IF(LEN(@temp)>0)
BEGIN
PRINT(@temp)
DELETE from TB_user WHERE user_id =@temp;
END
SET @userId=RIGHT(@userId,LEN(@userId)-CHARINDEX(@splitchar,@userId))
PRINT(@userId)
END
END
IF (@operation = 'update')
BEGIN
DECLARE @sql VARCHAR(5000)
DECLARE @whereSql VARCHAR(5000)
SET @sql = 'UPDATE TB_user SET'
SET @whereSql = ''
IF ( @userName !='NULL' and @userName != '')
BEGIN
SET @whereSql =@whereSql+ ' user_name = '+''''+@userName+''''+','
END
IF ( @password !='NULL')
BEGIN
SET @whereSql =@whereSql+ ' password = '+''''+@password+''''+','
END
IF ( @phone !='NULL')
BEGIN
SET @whereSql =@whereSql+ ' phone = '+''''+@phone+''''+','
END
IF ( @fullname !='NULL')
BEGIN
SET @whereSql =@whereSql+ ' fullname = '+''''+@fullname+''''+','
END
IF ( @sex !='NULL')
BEGIN
SET @whereSql =@whereSql+ ' sex = '+''''+@sex+''''+','
END
IF ( @area !='NULL')
BEGIN
SET @whereSql =@whereSql+ ' area = '+''''+@area+''''+','
END
IF ( @createAt !='NULL')
BEGIN
SET @whereSql =@whereSql+ ' create_at = '+''''+@createAt+''''+','
END
IF ( @isTeacher !='NULL')
BEGIN
SET @whereSql =@whereSql+ ' isTeacher = '+''''+@isTeacher+''''+','
END
IF ( @teacherInfo !='NULL')
BEGIN
SET @whereSql =@whereSql+ ' teacher_info = '+''''+@teacherInfo+''''+','
END
IF ( @icon !='NULL')
BEGIN
SET @whereSql =@whereSql+ ' icon = '+''''+@icon+''''+','
END
IF ( @empNumber !='NULL')
BEGIN
SET @whereSql =@whereSql+ ' empNumber = '+''''+@empNumber+''''+','
END
IF ( @userAccount !='NULL')
BEGIN
SET @whereSql =@whereSql+ ' userAccount = '+''''+@userAccount+''''+','
END
SET @sql = @sql+@whereSql
IF ( charindex(',',@sql) > 0)
BEGIN
PRINT @sql
SET @sql = SUBSTRING(@sql,0,(LEN(@sql)) )
SET @sql = @sql + ' WHERE user_id = '+ @userId
PRINT @sql
EXEC (@sql)
END
ELSE
BEGIN
RETURN
END
END
END