sql server儲存過程實現批量刪除

2022-04-06 09:01:29 字數 1161 閱讀 6387

在專案中用到了儲存過程來進行批量刪除的操作,給大家分享一下

原理就是把id組成的字串在資料庫分割成陣列放一張臨時表,刪除的時候與id進行對照

--

刪除會員資訊

ifobject_id('

pro_deluserinfo

','p

')is

notnull

--判斷儲存過程是否存在

drop

proc pro_deluserinfo --

存在則刪除

gocreate

proc pro_deluserinfo( --

建立儲存過程

@strid

varchar(max) --

引數 格式 "1,2,3,5,6")as

declare

@temp

table (a varchar(100)) --

建立臨時表

begin

--把引數@strid分割成int陣列並插入臨時表@temp

declare

@iint

set@strid

=rtrim(ltrim(@strid

))set@i=

charindex('

,',@strid

)while

@i>=

1begin

insert

@temp

values(left(@strid,@i-1

))set

@strid

=substring(@strid,@i

+1,len(@strid)-@i)

set@i

=charindex('

,',@strid

)end

if@strid

<>

''insert

@temp

values (@strid) --

插入臨時表

delete tbl_user where id in (select

*from

@temp) --

執行為刪除操作 通過id與臨時表中的int陣列對照

end

當然如果有多個表使用的話,也可以吧表名和唯一標示id寫成引數操作

SQL SERVER儲存過程實現分頁

create procedure dbo sp pagelist 建立儲存過程 stationid nvarchar 32 工位id pagesize int,每頁顯示的項數 pageindex int 當前頁數 as begin 獲取總行數,用count declare sqlselect nva...

sql server儲存過程

建立表的語句 create table student sno int primary key,sname nvarchar 30 sgentle nvarchar 2 sage int,sbirth smalldatetime,sdept nvarchar 30 drop table studen...

SQLSERVER儲存過程

sqlserver儲存過程使用說明書 引言首先介紹一下什麼是儲存過程 儲存過程就是將常用的或很複雜的工作,預先用 sql語句寫好並用乙個指定的名稱儲存起來,並且這樣的語句是放在資料庫中的,還可以根據條件執行不同 sql語句,那麼以後要叫資料庫提供與已定義好的儲存過程的功能相同的服務時,只需呼叫 ex...