select a.names from ( select names from tb where language=1) a, ( select names from tb where language=2) b, ( select names from tb where language=5) c where a.names=b.names and b.names=c.names
select a.name from tb where a.langue = '1' and exist (select * from tb where langue = '5' and name = a,name)
meiqingsong(阿飛) : 还是那个问题,条件个数不是固定的.
这样:做一个存储过程: Create proc GetName @WhereStr varchar(100) as begin if len(@WhereStr)=0 begin--如果参数长度为0,表示取所有name select name from TB return end declare @TmpTable table(ConID int identity(1,1),ConNum varchar(20)) declare @PreStr varchar(100),@Pos int set @PreStr=@WhereStr while len(@WhereStr)>0 begin--分离条件参数 set @Pos=charindex(',',@WhereStr) if @Pos!=0 set @PreStr=substring(@WhereStr,1,@Pos-1) else if @Pos=0 begin set @PreStr=@WhereStr set @Pos=len(@whereStr) end insert into @TmpTable(ConNum) values(@PreStr) set @PreStr=@WhereStr set @WhereStr=substring(@PreStr,@Pos+1,len(@PreStr)-@Pos) end declare @MaxID int select @MaxID=max(ConID) from @TmpTable if @MaxID is null begin--参数没有发现,取所有的name select name from TB return end declare @CircleNum int,@CurNum varchar(10) declare @ExecStr varchar(2000) set @CircleNum=1 set @ExecStr='select a.name from ' while @CircleNum<=@MaxID begin--拼接执行语句 select @CurNum=ConNum from @TmpTable where ConID=@CircleNum if @CircleNum=1 set @ExecStr=@ExecStr+'(select name from TB where language='+@CurNum+') a ' else set @ExecStr=@ExecStr+' inner join (select name from TB where language='+@CurNum+') '+char(ascii(a)-1+@CircleNum)+' on a.name='+char(ascii(a)-1+@CircleNum)+'.name ' set @CircleNum=@CircleNum+1 end --执行语句 exec sp_execsql @ExecStr end go没有测试,如果采用请自己调试一下。 上述存储过程应该比较灵活,给的参数是你的条件值,每个中间用半角','号隔开。 使用方法跟select语句差不多,返回也是一个记录集。 使用时,该语句:exec GetName '1,2,5' 这样参数就可以随便给,只有参数的总长度不超过参数给定的类型长度。
declare @tj varchar(20)
set @tj='1,5' --动态条件
exec('select * from tb where language in ('+@tj+')')
from ( select names from tb where language=1) a,
( select names from tb where language=2) b,
( select names from tb where language=5) c
where a.names=b.names and b.names=c.names
where a.langue = '1'
and exist (select * from tb where langue = '5' and name = a,name)
还是那个问题,条件个数不是固定的.
Create proc GetName
@WhereStr varchar(100)
as
begin
if len(@WhereStr)=0
begin--如果参数长度为0,表示取所有name
select name from TB
return
end
declare @TmpTable table(ConID int identity(1,1),ConNum varchar(20))
declare @PreStr varchar(100),@Pos int
set @PreStr=@WhereStr
while len(@WhereStr)>0
begin--分离条件参数
set @Pos=charindex(',',@WhereStr)
if @Pos!=0
set @PreStr=substring(@WhereStr,1,@Pos-1)
else if @Pos=0
begin
set @PreStr=@WhereStr
set @Pos=len(@whereStr)
end
insert into @TmpTable(ConNum) values(@PreStr)
set @PreStr=@WhereStr
set @WhereStr=substring(@PreStr,@Pos+1,len(@PreStr)-@Pos)
end
declare @MaxID int
select @MaxID=max(ConID) from @TmpTable
if @MaxID is null
begin--参数没有发现,取所有的name
select name from TB
return
end
declare @CircleNum int,@CurNum varchar(10)
declare @ExecStr varchar(2000)
set @CircleNum=1
set @ExecStr='select a.name from '
while @CircleNum<=@MaxID
begin--拼接执行语句
select @CurNum=ConNum from @TmpTable where ConID=@CircleNum
if @CircleNum=1
set @ExecStr=@ExecStr+'(select name from TB where language='+@CurNum+') a '
else
set @ExecStr=@ExecStr+' inner join (select name from TB where language='+@CurNum+') '+char(ascii(a)-1+@CircleNum)+' on a.name='+char(ascii(a)-1+@CircleNum)+'.name '
set @CircleNum=@CircleNum+1
end
--执行语句
exec sp_execsql @ExecStr
end
go没有测试,如果采用请自己调试一下。
上述存储过程应该比较灵活,给的参数是你的条件值,每个中间用半角','号隔开。
使用方法跟select语句差不多,返回也是一个记录集。
使用时,该语句:exec GetName '1,2,5' 这样参数就可以随便给,只有参数的总长度不超过参数给定的类型长度。
大家明白吗?