关于在select查询把变量当字段使用的问题.
DECLARE pvarORDER_HISTORY_NO NUMBER;
pvarORDER_HISTORY_NO NUMBER = '222'
INSERT INTO SD035_ORDERS_HISTORY_DETAILS_T(
ORDER_HISTORY_NO,
ORDER_NO
)
SELECT
pvarORDER_HISTORY_NO ,ORDER_NO from SD035_ORDERS_HISTORY_DETAILS_T
其中pvarORDER_HISTORY_NO是一个变量,我想让 ORDER_HISTORY_NO插入都是一个值,就是pvarORDER_HISTORY_NO,请问这样写在oracle的存储过程中能执行吗,应该怎样改能实现我的要求,谢谢~
注:pvarORDER_HISTORY_NO NUMBER是不固定查询出来的,我上面赋的值只是为了说明用.
------解决方案--------------------用 execute immediate 'insert ...'
insert是拼的sql
------解决方案--------------------execute immediate 'INSERT INTO SD035_ORDERS_HISTORY_DETAILS_T(
ORDER_HISTORY_NO,
ORDER_NO
) values ('|| pvarORDER_HISTORY_NO|| ','||ORDER_NO||')';
这个试试
------解决方案--------------------pvarORDER_HISTORY_NO NUMBER = '222'去掉
INSERT INTO SD035_ORDERS_HISTORY_DETAILS_T(
ORDER_HISTORY_NO,
ORDER_NO
)
SELECT
'222',ORDER_NO from SD035_ORDERS_HISTORY_DETAILS_T
------解决方案--------------------
------解决方案--------------------
------解决方案--------------------用动态执行sql语句吧,很好用execute immediatel ‘要执行的sql语句’;
例子: execute immediate 'select * from dual' into 变量名;
但是在存储过程中要把查询的结果存到一个变量里。