34,594
社区成员
发帖
与我相关
我的任务
分享
select * from a where 名称 in (select 名称 from (select dbo.show()) t )
create table 表A
(Id int,名称 nvarchar(10), 备注 nvarchar(10))
insert 表A
select 1 ,'苹果', '123' union all
select 2 ,'香蕉', '23' union all
select 3 ,'西瓜', '34' union all
select 4 ,'番茄', '2'
create function dbo.show() returns nvarchar(100)
as
begin
--你的实现小弟就不管了
return '''香蕉'',''番茄'''
end
declare @sql as nvarchar(1000)
set @sql='select * from 表A as a where 名称 in ('+dbo.show()+')'
exec(@sql)
select *
from #temp a
inner join (select * from dbo.show()) b
on a.名称 = b.名称
create table #temp
(
Id int identity,
名称 nvarchar(10),
备注 nvarchar(10)
)
insert #temp
select N'苹果', N'123' union all
select N'香蕉', N'23' union all
select N'西瓜', N'34' union all
select N'番茄', N'2'
go
create function show()
returns nvarchar(max)
as
begin
return N'''香蕉'',''番茄'''
end
go
--SQL:
--#1.用动态SQL
declare @sql nvarchar(max)
set @sql = 'select * from #temp where 名称 in ('+ dbo.show() +')'
exec(@sql)
/*
Id 名称 备注
2 香蕉 23
4 番茄 2
*/
declare @table table (id int,col varchar(1))
insert into @table
select 101,'a' union all
select 201,'b' union all
select 301,'c' union all
select 409,'d'
declare @userid varchar(2000),
@sqlstr varchar(4000)
set @userid='201,102,301,104,199'
SELECT * FROM @table
WHERE CHARINDEX(','+CAST(id AS VARCHAR(4))+',',','+@userid+',')>0
/*
id col
----------- ----
201 b
301 c
*/
create table 表A(Id int,名称 varchar(4),备注 int)
insert into 表A
select 1,'苹果',123 union all
select 2,'香蕉',23 union all
select 3,'西瓜',34 union all
select 4,'番茄',2
declare @函数返回值 varchar(20)
set @函数返回值='香蕉'',''番茄'
declare @s varchar(800)
set @s='select * from 表A where 名称 in ('''+@函数返回值+''')'
exec (@s)
/*
Id 名称 备注
----------- ---- -----------
2 香蕉 23
4 番茄 2
*/