2

我有以下 pl/sql,我想将其转换为 T-SQL。我用谷歌搜索但没有找到任何文章。

DECLARE
    l_retval    VARCHAR2(32767);
    l_rows      VARCHAR2(32767);
BEGIN
    FOR i IN (SELECT  emp.empno AS empno
                     ,emp.ename AS ename
                     ,mgr.empno AS mgr_empno
                     ,mgr.ename AS mgr_ename
              FROM    emp emp
                     ,emp mgr
              WHERE   emp.mgr = mgr.empno(+)
             )
    LOOP
        l_rows := l_rows||'{''emp_empno'':'''||i.empno||''',''emp_ename'':'''||i.ename||''',''mgr_empno'':'''||i.mgr_empno||''',''mgr_ename'':'''||i.mgr_ename||'''},';
    END LOOP;

    l_retval := '['||rtrim(l_rows,',')||']';

    htp.prn(l_retval);
END;
4

1 回答 1

0

您可以使用 cursor 并获取以下 T-SQL 代码:

DECLARE @l_retval varchar(max) 
   DECLARE @l_rows varchar(max) 
   DECLARE @SWV_I_empno VARCHAR(255) -- data type of column emp.empno 
   DECLARE @SWV_I_ename VARCHAR(255) -- data type of column emp.ename 
   DECLARE @SWV_I_mgr_empno VARCHAR(255) -- data type of column mgr.empno 
   DECLARE @SWV_I_mgr_ename VARCHAR(255) -- data type of column mgr.enam 
   DECLARE @SWV_cursor_var1 CURSOR 
   SET @SWV_cursor_var1 = CURSOR FOR SELECT emp.empno AS empno 
                     ,emp.ename AS ename 
                     ,mgr.empno AS mgr_empno 
                     ,mgr.ename AS mgr_ename 
   FROM emp emp LEFT OUTER JOIN emp mgr ON emp.mgr = mgr.empno 
   OPEN @SWV_cursor_var1 
   FETCH NEXT FROM @SWV_cursor_var1 INTO @SWV_I_empno,@SWV_I_ename,@SWV_I_mgr_empno,@SWV_I_mgr_ename 
   while @@FETCH_STATUS = 0 
   begin 
      SET @l_rows = @l_rows+'{''emp_empno'':'''+@SWV_I_empno+''',''emp_ename'':'''+@SWV_I_ename+''',''mgr_empno'':'''+@SWV_I_mgr_empno+''',''mgr_ename'':'''+@SWV_I_mgr_ename+'''},' 
      FETCH NEXT FROM @SWV_cursor_var1 INTO @SWV_I_empno,@SWV_I_ename,@SWV_I_mgr_empno,@SWV_I_mgr_ename 
   end 
   CLOSE @SWV_cursor_var1 

   SET @l_retval = '['+rtrim(@l_rows)+']' 

   EXECUTE HTP.prn @l_retval
于 2014-12-11T13:16:08.950 回答