返回表型函数
CREATE   FUNCTION   forhelp   () 
 RETURNS   TABLE(id   int,fld   varchar(100)) 
 AS 
 BEGIN 
 	declare   @t   table(id   int,fld   varchar(100)) 
 	insert   into   @t   select   1, 'hyc ' 
 	return   @t 
 END 
 消息   156,级别   15,状态   1,过程   forhelp,第   3   行 
 关键字    'BEGIN '   附近有语法错误。 
 消息   137,级别   15,状态   2,过程   forhelp,第   6   行 
 必须声明标量变量    "@t "。
------解决方案--------------------CREATE FUNCTION forhelp () 
 RETURNS @t TABLE(id int,fld varchar(100)) 
 AS 
 BEGIN 
 insert into @t select 1, 'hyc ' 
 return 
 END 
 GO 
 Select * From dbo.forhelp()
------解决方案--------------------CREATE FUNCTION forhelp () 
 RETURNS @a TABLE(id int,fld varchar(100)) 
 AS 
 BEGIN   
 declare @t table(id int,fld varchar(100)) 
 insert into @t select 1, 'hyc ' 
 insert into @a select id,fld from @t 
 return  
 END 
------解决方案--------------------再问一下,印象中function里好像是不能执行动态sql,而且还是ddl的,对吗?   
 加入要函数forhelp(@n int) 
 根据参数@n的值返回不同结构的@t--@n为字段数目   
 @n=2 @t table(f1 int,f2 int) 
 @n=3 @t table(f1 int,f2 int,f3 int) 
 @n=4 @t table(f1 int,f2 int,f3 int,f4,int)   
 如何使用函数来实现,好像没辙了,望指教。 
 ------------------------------------ 
 函数不行,用存储过程
------解决方案--------------------如果SQL可以直接访问存储过程返回的记录集:   
 select * from (exec ProcName paraList) where ....   
 那太方便了,大家说对不? 
------解决方案--------------------而且只能用临时表,不能使用vtable变量,对吧   
 ------------------------------------------   
 你说的太对了,刚开始我也很郁闷的。可见微软那帮开发人员是闭门造车,他们也不坐坐,看看好不好坐,舒服不舒服……
------解决方案--------------------CREATE FUNCTION forhelp () 
 RETURNS @t TABLE(id int,fld varchar(100)) 
 AS 
 BEGIN 
 insert into @t select 1, 'hyc ' 
 return 
 END 
 GO 
 Select * From dbo.forhelp()