Sybase IQ,使用“execute”在函数内的变量中返回查询字符串的结果?

时间:2013-10-29 14:40:22

标签: execute sybase-iq

嗨,你可以帮我吗?,那样我可以在一个带有“execute”的函数内的变量中返回查询字符串的结果吗?

例如

create function my _funcion(in @ num_celular char (20), in @ str_comando char (120))
returns char (120)
- on exception resume
as
begin

set @ exec_qry = Select 1 from dummy
execute (@ exec_qry)

return (@ the return value of the query "execute")

end

go

感谢

2 个答案:

答案 0 :(得分:1)

尝试这种方式:

create function my_funcion(@num_celular char (20), @str_comando char (120))
returns char(120)
as
begin
DECLARE @Result char(120)

execute('select @Result = ''Aaa'' ' )

RETURN @Result

end

go

以下查询结果

select my_funcion('AA','BB')

Aaa

答案 1 :(得分:0)

好的,谢谢回答我的问题,但是看看这个问题要复杂得多,因为我们运行的是一个动态Sql,在while循环中形成的查询然后存储在一个字符串变量中并运行它,问题是功能及时制作如下,附于下方。

create function ms_trafico.fn_mov_cambia_tag( in @num_celular char(20), in @str_comando char(120) )
returns char(120)
--on exception resume
as
begin
  set nocount on
  set temporary option string_rtruncation='OFF'

  --aplican para isql
  --set option isql_print_result_set=ALL
  --set option isql_show_multiple_result_sets=On

  declare @comando varchar(100)
  declare @exec_qry varchar(500)
  declare @cadena varchar(60)
  declare @tag char(15)
  declare @columna char(20)
  declare @pos int
  declare @strCadFinal char(150)
  declare @strFono char(20)

  set @comando = trim(@str_comando)
  set @strFono = trim(@num_celular)
  set @exec_qry='select XXX '
  set @pos = 1
  print "comando " + @comando

  while(@pos <= locate(@comando,'<',-1))
  begin

    set @tag = substr(@comando,locate(@comando,'<',@pos),CHARINDEX('>', SUBSTR(@comando,locate(@comando,'<',@pos),length(@comando))))
    set @pos = locate(@comando,'<',@pos) + 1
    select atributo_campo into @columna from TB_MOV_MAPEO_COMANDOS where parametro = @tag
    set @cadena="replace(XXX,'"||trim(@tag)||"',"||trim(@columna)||")"
    set @exec_qry=replace(@exec_qry,'XXX',@cadena)
  end

  set @comando="'"||trim(@comando)||"'"
  set @exec_qry=replace(@exec_qry,'XXX',@comando)
  set @exec_qry=@exec_qry||' as comando INTO @strCadFinal FROM VM_ALL_SERVICES_MOVIL where num_celular=@strFono'

  execute (@exec_qry)

  return (@strCadFinal) "The result of my query with execute method"!!!!!!!!!!

end
go

感谢您的关注。