ORA-01756:引用的字符串未正确终止

时间:2009-09-02 12:04:06

标签: sql oracle plsql

please let me know the issue with following script (sql,oracle 10g)

  1  DECLARE @colname AS NVARCHAR(50)
  2  DECLARE @tablename AS NVARCHAR(500)
  3  DEClARE @query AS NVARCHAR(500)
  4  SET @colname = 'select wk_units1 from cnt_sls_dm.fct_sales_summary'
  5  SET @tablename = 'SELECT tablename from dmi_user.fct_sales_meta'
  6  set @query='select '+@colname+' FROM '+@tablename+'
  7* EXECUTE sp_executesql @query
SQL> /
ERROR:
ORA-01756: quoted string not properly terminated

5 个答案:

答案 0 :(得分:5)

这个错误非常自我描述,你有一个未终止的引用。

您正试图在SQL Server中运行Oracle代码。这不起作用。

您只能通过复制将T-SQL变为PL/SQL

我更正了语法,但很可能你需要做的工作要多得多。

DECLARE
    colname NVARCHAR2(50);
    tname   NVARCHAR2(500);
    query   NVARCHAR2(500);
BEGIN
    SELECT  wk_units1
    INTO    colname
    FROM    cnt_sls_dm.fct_sales_summary;
    SELECT  tablename
    INTO    tname
    FROM    dmi_user.fct_sales_meta;
    query := 'SELECT ' || colname || ' FROM ' || tname;
END;

答案 1 :(得分:2)

将第6行更改为

set @query='select '+@colname+' FROM '+@tablename

答案 2 :(得分:0)

第6行在您的示例中似乎不正确:

set @query='select '+@colname+' FROM '+@tablename+'

您使用'完成该行。要么删除+',要么使用where声明完成请求...

答案 3 :(得分:0)

这看起来很像tSql而不是pl SQl,你可能想要使用||连接Oracle和varchar2中的字符串而不是nvarchar

答案 4 :(得分:0)

以下是回答问题的正确方法........ 运行良好.......... 感谢所有帮助过......

-Irveen

DECLARE
     type fct_sales_summary_cur is table of dmi_user.fct_sales_summary_cmp_1%rowtype index by binary_integer;
     cur_rec fct_sales_summary_cur;
            colname NVARCHAR2(50);
            tname   NVARCHAR2(500);
            query   VARCHAR2(500);
            cnt     number:=1;
    BEGIN
            loop
            SELECT  colname
            INTO    colname
            FROM    dmi_user.FCT_SALES_META
            where   sno=cnt;
            SELECT  tablename
            INTO    tname
            FROM    dmi_user.fct_sales_meta
            WHERE sno=cnt;
            --query:='select * from dmi_user.fct_sales_summary_cmp';
            query := 'SELECT '|| colname ||' FROM '||tname;
            -- dbms_output.put_line(colname);
            -- dbms_output.put_line(tname);
            --dbms_output.put_line(query);
            execute immediate query bulk collect into cur_rec;
            --dbms_output.put_line(cur_rec);
            dbms_output.put_line('------Table-Sno -----' || cnt);
            for i in cur_rec.first..cur_rec.last loop
            dbms_output.put_line(cur_rec(i).wk_units1);
            end loop;
            cnt:=cnt+1;
    exit when cnt=4;
    end loop;
    END;
/