關於主鍵列執行批量刪除單錶資料的儲存過程,

2021-06-22 14:29:49 字數 1293 閱讀 8196

set ansi_nulls on

set quoted_identifier on

gocreate procedure [dbo].[up_ty_txl_xx_delete_batch]

--alter procedure [dbo].[up_ty_txl_xx_delete_batch]

@keys varchar(8000)

asdeclare @sid nvarchar(100),@ipos int

/*獲得 [@keys] 中第乙個英文逗號的位置*/

--set @keys='194da3ed-32eb-4983-9f0e-487f741eb147,c372b2ec-d199-41b1-83fc-3a1cb8b0204d'測試用的

set @ipos=charindex(',',@keys)

/*如果長度超出範圍,則返回過程*/

if (len(@keys)>0) --return

if @ipos = 0/*如果是單個記錄,後面沒有英文逗號執行操作*/

delete from ty_txl_xx where u_bh=@keys

/*如果存在英文逗號*/

else

begin

while 1=1

begin

/*得到第乙個英文逗號以左的字串(不包括英文逗號)*/

set @sid=left(@keys,@ipos-1)

/*改變源字串【去掉第乙個英文逗號以左的字串(包括英文逗號)】*/

set @keys=stuff(@keys,1,@ipos,'')

/*再次獲取新字串中第乙個英文逗號的位置*/

set @ipos=charindex(',',@keys)

/*執行刪除語句*/

delete from ty_txl_xx where u_bh=@sid

/*如果沒有找到英文逗號,則最後乙個被截字串就是 [@keys]*/

if(@ipos=0)

begin

/*執行刪除語句*/

delete from ty_txl_xx where u_bh=@keys

/*退出迴圈*/

break

endend

end-----測試位置

exec [dbo].[up_ty_txl_xx_delete_batch] '79a5bd18-24ac-430c-868f-b3fc40b152d5,9f4cc6e8-e021-435f-81cf-c86abdc7a8ad,fdd06b01-dd0b-421f-b94e-cb9147a662e7'

Mybatis執行批量插入返回資料庫主鍵列表

推薦閱讀 微服務還能火多久?我們都知道mybatis在插入單條資料的時候有兩種方式返回自增主鍵 1 對於支援生成自增主鍵的資料庫 增加 usegeneratekeys和keyproperty 標籤屬性。2 不支援生成自增主鍵的資料庫 使用。insert into author username,pa...

Mysql 批量刪除資料表資料

drop 不再需要該錶 truncate 保留該錶,但要刪除所有記錄 delete 要刪除部分記錄或者有可能會後悔的話,用 delete 1.drop table tb drop 是直接將 刪除,無法找回。例如刪除 user 表 drop table user 2.truncate table tb...

游標批 量刪除資料表

declare tablename varchar 30 sql varchar 500 declare cur delete table cursor read only forward only for select name from sysobjects where name like pu...