0

嗨,你能帮我吗?,这样我可以用“执行”在函数内的变量中返回查询字符串的结果?

例如

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

谢谢

4

2 回答 2

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
于 2013-10-30T14:08:22.283 回答
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

谢谢你的关注。

于 2013-11-04T13:45:32.373 回答