英文:
How can I use with the Daynamic sql for update table in oracle
问题
I have translated the code parts as requested. Here is the translated code:
我是Oracle的新手。我创建了一个带有列的表,并向表中插入数据。
我的代码如下:
创建表格 Tbl_Job_Desc
(
id int,
name varchar2(50),
family varchar2(50),
jobname varchar2(50)
)
插入数据到 Tbl_Job_Desc (id,name,family,jobname )VALUES (1,'negar','GHANBARIAN','PROGRAMMER');
插入数据到 Tbl_Job_Desc (id,name,family,jobname )VALUES (2,'ZAHRA','GHANBARIAN','STUDENT');
插入数据到 Tbl_Job_Desc (id,name,family,jobname )VALUES (3,'SHIRIN','SAEEDI','NURSE');
插入数据到 Tbl_Job_Desc (id,name,family,jobname )VALUES (4,'FATEME','GHASEMI','TEACHER');
插入数据到 Tbl_Job_Desc (id,name,family,jobname )VALUES (5,'mohammad','AZADI','PROGRAMMER');
我想编写一个存储过程,该存储过程从用户那里获取输入并生成如下的更新语句。
update Tbl_Job_Desc set Jobname='newjob'。
我编写了这个存储过程,但出现了错误,请查看我的存储过程并帮助我。
存储过程如下:
CREATE OR REPLACE PROCEDURE Ds_sampleTest (
newjob IN VARCHAR2
)
IS
query VARCHAR2(1000);
BEGIN
query := 'update Tbl_Job_Desc set Jobname=''' || newjob || '''';
dbms_output.put_line(query);
EXECUTE IMMEDIATE query;
END;
运行存储过程的代码如下:
-- 运行存储过程
BEGIN
Ds_sampleTest('doctor');
COMMIT;
END;
你的错误是:PLS-00103。
英文:
I am new in oracle .I create a table with columns and insert data into table
my code is :
create table Tbl_Job_Desc
(
id int,
name varchar2(50),
family varchar2(50),
jobname varchar2(50)
)
INSERT INTO Tbl_Job_Desc (id,name,family,jobname )VALUES (1,'negar','GHANBARIAN','PROGRAMMER');
INSERT INTO Tbl_Job_Desc (id,name,family,jobname )VALUES (2,'ZAHRA','GHANBARIAN','STUDENT');
INSERT INTO Tbl_Job_Desc (id,name,family,jobname )VALUES (3,'SHIRIN','SAEEDI','NURSE');
INSERT INTO Tbl_Job_Desc (id,name,family,jobname )VALUES (4,'FATEME','GHASEMI','TEACHER');
INSERT INTO Tbl_Job_Desc (id,name,family,jobname )VALUES (5,'mohammad','AZADI','PROGRAMMER');
I want Write a procedure that takes an input from the user and produces an update statement as follows.
update Tbl_Job_Desc set Jobname='newjob'
I write this procedure but I get error ,please take a look my procedure and help me .
my procedure is :
CREATE OR replace PROCEDURE Ds_sampleTest (
newjob IN VARCHAR2
)
IS
newjob VARCHAR2(200);
query VARCHAR2(1000);
BEGIN
query := 'update Tbl_Job_Desc set Jobname='||newjob' ;
dbms_output.Put_line(query);
EXECUTE IMMEDIATE query;
END;
-- Run the Procedure
CL scr
SET serveroutput ON
BEGIN
Ds_sample( 'doctor');
COMMIT;
END;
I want Write a procedure that takes an input from the user and produces an update statement as follows.
update Tbl_Job_Desc set Jobname='newjob'
my error is : PLS-00103
答案1
得分: 2
不需要使用动态SQL的情况下,请不要使用:
CREATE PROCEDURE Ds_sampleTest (
newjob IN Tbl_Job_Desc.Jobname%TYPE
)
IS
BEGIN
update Tbl_Job_Desc
set Jobname = newjob;
END;
/
(并且不要创建与存储过程参数同名的本地变量。)
然后:
CL scr
SET serveroutput ON
BEGIN
Ds_sampleTest( 'doctor');
COMMIT;
END;
/
如果必须使用动态SQL(实际上不需要的话就不应该使用),则在传递值时请使用绑定变量而不是字符串连接:
CREATE OR replace PROCEDURE Ds_sampleTest (
newjob IN Tbl_Job_Desc.Jobname%TYPE
)
IS
query VARCHAR2(1000);
BEGIN
query := 'update Tbl_Job_Desc set Jobname = :1';
dbms_output.Put_line(query);
EXECUTE IMMEDIATE query USING newjob;
END;
/
英文:
Do not use dynamic SQL if you do not need to:
CREATE PROCEDURE Ds_sampleTest (
newjob IN Tbl_Job_Desc.Jobname%TYPE
)
IS
BEGIN
update Tbl_Job_Desc
set Jobname = newjob;
END;
/
(And do not create local variables that shadow the procedure's parameters.)
Then:
CL scr
SET serveroutput ON
BEGIN
Ds_sampleTest( 'doctor');
COMMIT;
END;
/
If you must use dynamic SQL (you do not need to so you should not) then use bind variables rather than string concatenation when you are passing values:
CREATE OR replace PROCEDURE Ds_sampleTest (
newjob IN Tbl_Job_Desc.Jobname%TYPE
)
IS
query VARCHAR2(1000);
BEGIN
query := 'update Tbl_Job_Desc set Jobname = :1';
dbms_output.Put_line(query);
EXECUTE IMMEDIATE query USING newjob;
END;
/
答案2
得分: 0
将您遇到的错误消息添加到您的问题会很有帮助。
如果在发布您的代码时没有出现错误,那么我认为您的错误与您的代码的这一部分有关:
query := 'update Tbl_Job_Desc set Jobname='||newjob ;
在这种情况下,如果您想在文本中添加引号,您需要在文本中将引号加倍,您示例中的正确代码如下:
query := 'update Tbl_Job_Desc set Jobname='''||newjob||''' ;
这将保存查询如下:
update Tbl_Job_Desc set Jobname='your_newjob'
因此,每当您想要在文本中添加引号时,您需要将它们加倍。如果您想在文本中间添加引号,您可以这样写:
my_text := 'My text with '' quote'
如果您想在文本开头或结尾添加引号,您可以先加一个双引号,然后是开始或结束文本的引号:
my_text := ''There's a quote at the beginning''
如果您想将引号连接到文本中,那么您需要在文本的开头和结尾以及双引号之间放置普通引号,这将导致四个连续的引号:
my_text := '''' || 'I''m concatenating a quote at the beginning'
英文:
Adding the error message you are getting to your question would be helpful.
If there are no errors posting your code, then I think your error is related to this part of your code:
query := 'update Tbl_Job_Desc set Jobname='||newjob' ;
In this case, if you want to add quotes to the text, you need to double quote your quotes in the text, the correct code in your example is:
query := 'update Tbl_Job_Desc set Jobname='''||newjob||'''' ;
This will save query as:
update Tbl_Job_Desc set Jobname='your_newjob'
So, whenever you want to add quotes to your text, you need to make them double.
If you want a quote in the middle of your text, you write:
my_text := 'My text with '' quote'
If you want a quote at the beginning or end of your text, you put a double quote, and then the quote to begin or end your text:
my_text := '''There''s a quote at the beginning'
If you want to concatenate a quote to a text, then you need to put the normal quotes at the beginning and end of the text and the double quote, which makes for four consecutive quotes:
my_text := '''' || 'I''m concatenating a quote at the beginning'
答案3
得分: 0
OK, 所以问题是当您尝试编译您的过程时,您会收到此错误消息:
错误:PROCEDURE DS_SAMPLETEST
行/列:0/0 PL/SQL:编译单元分析终止
行/列:1/1 PLS-00410:RECORD、TABLE或参数列表中不允许重复字段
错误表示您已将newjob声明为过程的IN参数,但与此同时在您的过程中声明了一个具有相同名称的变量:
newjob VARCHAR2(200);
这是不允许的,您可以在声明中删除此行,您已将newjob声明为IN参数,或者如果出于其他某种原因您需要拥有与IN参数相同值的另一个变量,您可以使用另一个名称,类似这样:
v_newjob VARCHAR2(200) := newjob;
英文:
OK, so the problem is you are getting this error message when you try to compile your procedure:
Errors: PROCEDURE DS_SAMPLETEST
Line/Col: 0/0 PL/SQL: Compilation unit analysis terminated
Line/Col: 1/1 PLS-00410: duplicate fields in RECORD,TABLE or argument list are not permitted
The error says that you have declared newjob as an IN argument of your procedure but you are at the same time declaring a variable in your procedure with the same name:
newjob VARCHAR2(200);
That it is not allowed, you can delete this line in the declaration, you have declared newjob as IN parameter, or if for some other reason you need to have another variable with the same value as the IN parameter, you use another name, something like this:
v_newjob VARCHAR2(200) := newjob;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论