我是想这样,比如
a_ids='1,2,3'
这样那条语句就变成
delete from article_list where article_id in (1,2,3)
我以前在asp当中就是这样的,但写成存储过程了以后就错了
如果是a_ids='1'就可以成功
但如果是a_ids='1,2,3'
就出错了,提示类型转换失败
恳请各位帮帮我啊,
use cms
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'delete_a' AND type = 'P')
DROP PROCEDURE delete_a
go
create procedure delete_a
@a_ids nvarchar(100)
as
begin
delete from article_list where article_id in (@a_ids)
end
go
4 个解决方案
#1
declare @a_ids varchar(100)
set @a='1,2,3'
exec('delete from article_list where article_id in ('+@a+')')
set @a='1,2,3'
exec('delete from article_list where article_id in ('+@a+')')
#2
要改为exec执行才行:
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'delete_a' AND type = 'P')
DROP PROCEDURE delete_a
go
create procedure delete_a
@a_ids nvarchar(100)
as
begin
exec('delete from article_list where article_id in ('+@a_ids+')')
end
go
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'delete_a' AND type = 'P')
DROP PROCEDURE delete_a
go
create procedure delete_a
@a_ids nvarchar(100)
as
begin
exec('delete from article_list where article_id in ('+@a_ids+')')
end
go
#3
如果你不想用exec来执行,改为下面的也可以:
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'delete_a' AND type = 'P')
DROP PROCEDURE delete_a
go
create procedure delete_a
@a_ids nvarchar(100)
as
begin
delete from article_list where charindex(','+cast(article_id as varchar)+',',','+@a_ids+',')>0
end
go
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'delete_a' AND type = 'P')
DROP PROCEDURE delete_a
go
create procedure delete_a
@a_ids nvarchar(100)
as
begin
delete from article_list where charindex(','+cast(article_id as varchar)+',',','+@a_ids+',')>0
end
go
#4
多谢各位大哥!
#1
declare @a_ids varchar(100)
set @a='1,2,3'
exec('delete from article_list where article_id in ('+@a+')')
set @a='1,2,3'
exec('delete from article_list where article_id in ('+@a+')')
#2
要改为exec执行才行:
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'delete_a' AND type = 'P')
DROP PROCEDURE delete_a
go
create procedure delete_a
@a_ids nvarchar(100)
as
begin
exec('delete from article_list where article_id in ('+@a_ids+')')
end
go
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'delete_a' AND type = 'P')
DROP PROCEDURE delete_a
go
create procedure delete_a
@a_ids nvarchar(100)
as
begin
exec('delete from article_list where article_id in ('+@a_ids+')')
end
go
#3
如果你不想用exec来执行,改为下面的也可以:
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'delete_a' AND type = 'P')
DROP PROCEDURE delete_a
go
create procedure delete_a
@a_ids nvarchar(100)
as
begin
delete from article_list where charindex(','+cast(article_id as varchar)+',',','+@a_ids+',')>0
end
go
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'delete_a' AND type = 'P')
DROP PROCEDURE delete_a
go
create procedure delete_a
@a_ids nvarchar(100)
as
begin
delete from article_list where charindex(','+cast(article_id as varchar)+',',','+@a_ids+',')>0
end
go
#4
多谢各位大哥!