这种sql应该怎么写呀?
比如:
SELECT * FROM ACCTA TA INNER JOIN ACCTB TB ON TA.TA001=TB.TB001 WHERE TA004 IN( '某个数 '):
"某个数 ": 我希望是N个数,是不确定的。如果要写在存储过程中“某个数”为参数的话,怎么写呀?
------解决方案--------------------create proc dbo.test
(
@arr nvarchar(1000)
)
as
begin
create table # (id int identity(1,1), chars nvarchar(10))
insert into #
select 'a ' union all
select 'b ' union all
select 'e '
declare @sql nvarchar(4000)
set @sql = 'select * from # where chars in ( ' + @arr + ') '
exec sp_executesql @sql
drop table #
end
go
declare @arr nvarchar(1000)
set @arr = ' ' 'a ' ', ' 'b ' ' '
exec dbo.test @arr
drop proc dbo.test
------解决方案--------------------在查询分析器里面运行
CREATE function My_split(@aString varchar(8000),@pattern varchar(10))
returns @temp table([Sid] [int] IDENTITY (1, 1) NOT NULL ,Myvalues varchar(100))
--实现split功能的函数
--说明:@aString,字符串,如“27,28,29”;@pattern,分隔标志,如“ ,”
as
begin
declare @i int
set @aString=rtrim(ltrim(@aString))
set @i=charindex(@pattern,@aString)
while @i> =1
begin
insert @temp values(left(@aString,@i-1))
set @aString=substring(@aString,@i+1,len(@aString)-@i)
set @i=charindex(@pattern,@aString)
end
if @aString <> ' '
insert @temp values(@aString)
return
end
添加进这个函数以后。
如果存储过程传递进来的字符串是1,2,3,4,5,6,7---假设为@IDs varchar 100
存储过程里面这么用。
update TopWinCMS_ArticleComment set Pass =1 where ID
in (select Myvalues FROM my_split(@IDs, ', '))