动态执行sql语句的问题,非高手不要浪费时间

hardnut 2000-07-06 10:45:00
想在一个程序过程中动态构建sql语句,然后执行,然后处理其结果集,一直没有找到一个方法。
既达到这样的功能:
create procedure DynSql @sql varchar(1024)
as
begin
decalare @max int
select @max=fn(@sql)
if @max>1000 ....
end

现在要的就是这个fn(@sql)怎么写?
想过用exec(@sql)可是收不到execute的结果信集,
后网友提示用openrowset,可是openrowset竟然不支持动态语句,

各位仁兄还有什么高招,我是江南尽了

...全文
316 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Limu 2000-07-10
  • 打赏
  • 举报
回复
Could you use this SQL statement?
create procedure DynSQL @SQL as select * from [@SQL] as TempTable
I've not try.I think you can try it.
Fxx 2000-07-09
  • 打赏
  • 举报
回复
如果在Developer 2000中可以用Forms_dll执行动态Sql语句,
如果在其它情况下用Dbms_sql公用包可以执行动态Sql语句。
qianxh 2000-07-08
  • 打赏
  • 举报
回复
使用游标方式,请看下面语句:

declare @sql varchar(1024)
select @sql='select max(price) as maxprice from sales'
--
declare @fn_val int
declare @fn_cur_cmd varchar(1024)
select @fn_cur_cmd='declare fn_cur cursor for '+@sql
exec(@fn_cur_cmd)
open fn_cur
fetch next from fn_cur into @fn_val
close fn_cur
deallocate fn_cur
--
select @fn_val as fn_val





hardnut 2000-07-07
  • 打赏
  • 举报
回复
说白了,我就是想有一种不是太复杂的在一个存储过程中处理一个动态执行语句产生的结果集的方法,例如我的@sql可能是‘select max(price) as maxprice from sales'
然后取得maxprice,(用select @max=...)因为后续的处理依赖于动态执行的结果,
当然可以通过临时表来得到,但考虑支效率问题我觉得有太好,我想最好的办法是用openrowset(...)---如果它支持动态sql语句的话,--我想不通为什么M$软不提供这个功能,
tanghuan 2000-07-07
  • 打赏
  • 举报
回复
哈哈
create procedure DynSql @sql varchar(1024)
as
begin
declare @max int
create table #maxtemp (maxint int)
insert #maxtemp (maxint) execute(@sql )
select @max=maxint from #maxtemp
drop table #maxtemp
if @max>1000 ....
end
LaoZheng 2000-07-07
  • 打赏
  • 举报
回复
@max=fn(@sql)
@max是什么?

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧