SQL Server中使用表值函数
函数有很多限制,不能使用动态语句,不能使用临时表等等。。。细看一下,直接写语句就行了,不用动态语句
insert into @re select id,parid,@I from videoclasspic where charindex(','+cast(id as varchar(10))+',',','+@parentid+',')>0 and isvalid=1
SqlServer表值函数:
Sql server 的表值函数是返回一个Table类型,table类型相当与一张存储在内存中的一张虚拟表。
实现表值函数很简单:
下面是一个不带输入参数的表值函数
create function tvpoints()
returns tableasreturn(select * from tb_users);这个表值函数数查询所有用户表的数据
对于多语句表值函数,在 BEGIN...END 语句块中定义的函数体包含一系列 Transact-SQL 语句,这些语句可生成行并将其插入将返回的表中。
以下示例创建了一个表值函数.
create function tvpoints()returns @points table (x float, y float)as begininsert @points values(1,2);insert @points values(3,4);return;end |
查询表值函数跟查询普通表一样
select * from tvpoints()
返回的是一张表
带输入参数的表值函数
create function tvpoints2(@x AS int,@y as int)returns @points table (x float, y float)as begininsert @points values(@x,@y);return;end |
浙公网安备 33010602011771号