怎么把数据库中字符串类型的1,2,3,4,5转换成批量删除的1,2,3,4,5 批量删除的存储过程!要接受一个id的集合1,2,3,4
eg:
IF EXISTS(SELECT * FROM SYSOBJECTS WHERE name='D_t_Base_Employee' AND xtype='P') DROP PROC D_t_Base_Employee
@str id varchar
GO
CREATE PROC D_t_Base_Employee
BEGN
DELETE Employee WHERE ID IN (@str)
END
在存储过程中应该怎样去吧varchar类型的参数转换成in类型
DELETE Employee WHERE ID IN (1,2,3,4)
转换varcahr转换int
分享到:
------解决方案--------------------
方法一
CREATE PROC D_t_Base_Employee @str varchar(100)
as
declare @sql varchar(1000)
set @sql='DELETE Employee WHERE ID IN ('+@str+')'
exec (@sql)
go
方法二
CREATE PROC D_t_Base_Employee @str varchar(100)
as
DELETE Employee WHERE CHARINDEX(','+CAST(id as varchar)+',',','+@str+',')>0
go
------解决方案--------------------
第一种方法,就是够早了一个动态语句,最后的动态语句类似于这样:
DELETE Employee WHERE ID IN (1,2,3)
第二种方法,
DELETE Employee WHERE CHARINDEX(','+CAST(id as varchar)+',',','+@str+',')>0
最后就变为这样的语句:
DELETE Employee WHERE CHARINDEX(',2,' , ',1,2,')>0