CREATE FUNCTION getTitle ( @tablename VARCHAR(100) ,@topicid INT )
RETURNS VARCHAR(200) AS
BEGIN
DECLARE @Estr VARCHAR(500)
SET @Estr=N'select title from '+@tablename+' where topicid='+ Convert(Varchar(10),@topicid)
sp_EXECUTEsql(@Estr)//好象不能这样用!!!!报错!
return //我想返回 title字段的内容,title 是varchar类型的
END
RETURNS VARCHAR(200) AS
BEGIN
DECLARE @Estr VARCHAR(500)
SET @Estr=N'select title from '+@tablename+' where topicid='+ Convert(Varchar(10),@topicid)
sp_EXECUTEsql(@Estr)//好象不能这样用!!!!报错!
return //我想返回 title字段的内容,title 是varchar类型的
END
解决方案 »
- 请问这样的一个"存储过程"该如何设计?
- 数据库设计的问题.
- SQL 配置选项'NESTED TRIGGERS '以从1改为0,请运行RECONFIGURE语句进行安装
- 关于递归取记录的死循环问题
- 求sql语句
- about soundex?
- 请教一个简单问题。我要把B表中的ID字段与A表ID字段相同的记录,从B表中选一个字段值插入到A表的一个字段中,应该怎么写??谢谢
- 关于vfp候选索引的问题
- 还是分布式的问题!!头疼的要命!!
- sql小白请教大神,跪求帮忙
- 在 asp 文件里调出 Compute By 的所有的记录
- 我写个自定义函数,其中有计算用户输入时间与当前系统时间之间的差值,但是自定义函数中不允许有getdate(),怎么办?
RETURNS VARCHAR(200) AS
BEGIN
DECLARE @Estr VARCHAR(500)
SET @Estr='select title from '+@tablename+' where topicid='+ Convert(Varchar(10),@topicid)
--EXECUTE(@Estr)--好象不能这样用!!!!报错!
return(@Estr) --我想返回 title字段的内容,title 是varchar类型的
END
RETURNS VARCHAR(200) AS
BEGIN
DECLARE @Estr NVARCHAR(500)
DECLARE @return VARCHAR(500)
SET @Estr='select @return=title from '+@tablename+' where topicid='+ Convert(Varchar(10),@topicid)
exec sp_executesql @Estr,N'@return varchar(500) output',@return output
return @return
END
报错
Only functions and extended stored procedures can be executed from within a function.
执行的时候应该 dbo.getTitle('tableName', 'ID')
服务器: 消息 557,级别 16,状态 2,过程 getTitle,行 11
Only functions and extended stored procedures can be executed from within a function.
declare @tb_name varchar(20)
declare @sql varchar(4000)
declare cur cursor for
select distinct atTable from bbsbak
open cur
fetch next from cur
into @tb_name
set @sql=''
while(@@fetch_status=1)
begin
@sql=@sql+' select title from '+@tb_name+' union '
fetch next from cur
into @tb_name
endset @sql=left(@sql,len(@sql)-6)
go
print (@sql)
go
exec(@sql)