将PL / sql转换为T-SQL

时间:2013-10-29 07:16:19

标签: sql sql-server oracle tsql plsql

我有以下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;

1 个答案:

答案 0 :(得分:0)

您可以使用游标并获取以下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