PLSQL Variable Insert - sql

I send a parameter in my PLSQL function which is the name of the Table.
In my code, I want to insert into the table that is being received in the parameter.
When I type the insert statement
insert into TABLE_VARIABLE_NAME
VALUES (1, 2, 3);
It gives the error of Table Doesn't exist.
How can I use Table's Name as the parameter of the function?

Here is the full demo code you can try it and even the above hint is absolutely correct ,i just elaborated it more with insert values as varchar2 :-
create table td (valued varchar2(10));
create or replace procedure dhar_conn(tname varchar2)
as
begin
execute immediate 'insert into '||tname||' values(''1'')';
commit;
end;/
execute dhar_conn('td')

You'll need dynamic SQL with execute immediate.
execute immediate 'insert into ' || l_var_name || 'values (1,2,3)'

Related

Redshift dollar quoting inside a procedure

I have a procedure which accepts the table name and the 3 fields that need filling. This is to be called from another procedure that loops through another table deciding which sub-table to put stuff into. The important bit is a simple insert statement, e.g.
insert into table1 values
('blah','String','50');
So that 4 parameters coming in (table1, and the 3 values). How would I do this using dollar quoting? Obviously this doesnt work but gives you an idea of what I'm trying to accomplish:
create or replace procedure
insert_dc_table(p_tblname varchar,
p_name varchar,
p_datatype varchar,
p_datalen varchar)
as $$
begin
execute
'insert into '||p_tblname||'(name,datatype,datalen) values '
||'('
||p_name||', '
||p_datatype||', '
||p_datalen
||')';
end;
$$ language plpgsql;
I'd need double-dollars around some, but am unsure of exactly where $$ and quotes go in all this !*&#!
I could declare a variable to hold the execute statement and do:
declare a _output varchar(200);
a_output := $$ insert into $$||p_tblname||$$(name,datatype,datalen) values ( '$$||p_name||$$',
well, i get lost there!
Thanks in advance for help!
Redshift Stored Procedures only require dollar quoting of the procedure body. Quotes inside the the procedure body are interpreted as normal.
You may find that your SQL client does not submit the SP create correctly due to the dollar quotes. If so I recommend using psql to create the stored procedure.
Sample stored procedures are available in our "Amazon Redshift Utils" GitHub repository.
Here's a modification of your example:
-- DROP PROCEDURE insert_dc_table(VARCHAR,VARCHAR ,VARCHAR ,VARCHAR);
CREATE OR REPLACE PROCEDURE insert_dc_table(
p_tblname VARCHAR, p_name VARCHAR, p_datatype VARCHAR, p_datalen VARCHAR )
AS $$
DECLARE
rows INTEGER;
BEGIN
sql := 'INSERT INTO '||p_tblname||' (name, datatype, datalen)'
||' VALUES ('||p_name||','||p_datatype||','||p_datalen||');';
RAISE INFO 'Running SQL: %', sql;
EXECUTE sql;
GET DIAGNOSTICS rows := ROW_COUNT;
RAISE INFO 'Rows inserted = %', rows;
END
$$ LANGUAGE plpgsql;
-- CALL insert_dc_table ('test_table', 'name', 'type', 'length');

Oracle PL/SQL: how to execute a procedure with a varray as out parameter?

I'm writing a stored procedure that will receive a code and return one or more VARCHAR2 values. The number of values will vary according to the code the procedure receives.
I don't know if this is the best approach (needless to say I'm a beginner), but I created a user-defined type and a table to store these values.
create or replace type t_desc as varray(15) of varchar2(15);
create table all_values(code int, list t_desc);
insert into all_values values (0, t_desc('a','b','c','d'));
insert into all_values values (1, t_desc('a','b'));
Then, I wrote the procedure as follows:
create or replace procedure teste(v_code int, v_list out t_list) is
begin
select list into v_list from all_values where code = v_code;
end;
This is where I'm stuck. How should this procedure be executed to return the values I've stored in the list column?
you have to declare a variable and pass it in you proc.
declare
t t_desc ;
i int;
begin
teste(i, t);
end;
to see the results you can itarate through your varray:
FOR x IN 1..t.COUNT LOOP
dbms_output.put_line(t(x));
END LOOP;

INSERT after EXECUTE IMMEDIATE in oracle [duplicate]

I'm trying to create a table and then insert some values in it within the same procedure in pl/sql. I tried to run the following query without success:
create or replace Procedure insertval8(id_no in number,e_name in char)
is
begin
execute immediate 'create table edu2(id number(20), name char(12))';
insert into edu2 values(&id_no,&e_name);
end;
displays
LINE/COL ERROR
-------- -----------------------------------------------------------------
5/1 PL/SQL: SQL Statement ignored
5/13 PL/SQL: ORA-00942: table or view does not exists
The error persists until I remove the insert code.
The procedure cannot be compiled because the table is not present at compile time.
Wrap the insert in execute immediate also, or use a global temporary table (generaly the preferred solution for temporary data).
create or replace procedure insertval8 (id in number,
name in char )
is
begin
execute immediate 'create table edu2(id number(20), name char(12))';
execute immediate 'insert into edu2(id, name) values (:1, :2)'
using id, name;
end;

'insert into' in 'execute immediate' clause

Can you check this and tell me why I've got an error, please? How should it look? I have no idea what's wrong here. I need to create a table in a function, and in the same function insert data into this table:
create or replace
function new_tab ( pyt IN varchar2) return number
IS
a number;
b varchar2(20);
begin
a:= ROUND(dbms_random.value(1, 3));
b:='example';
-- This works perfect
execute immediate 'CREATE TABLE create_tmp_table'|| a ||'(i VARCHAR2(50))';
-- Here`s the problem
execute immediate 'insert into create_tmp_table'||a|| 'values ('|| b ||')';
exception
when others then
dbms_output.put_line('ERROR-'||SQLERRM);
return 0;
end;
My result is:
ERROR-ORA-00926: missing VALUES keyword. Process exited.
Where is the mistake?
As you are creating a dynamic insert command you have to create it as is. So you are missing the single quotes for the value that is varchar:
execute immediate
'insert into create_tmp_table'||a|| ' values ('''|| b ||''');';
^here ^and here
And a good suggestion for this type of error is to do some debug. On your case you could create a varchar2 variable and put your insert on it then you use the dbms_output.put_line to this variable. Then you will have your sql command that you can test direct on your database. Something like:
declare
sqlCommand varchar2(1000);
--define a and b
begin
--put some values in a and b
sqlCommand := 'insert into create_tmp_table'||a|| ' values ('''|| b ||''');';
dbms_output.put_line(sqlCommand);
end;
Then you will know what is wrong with the dynamic command.
execute immediate 'insert into TABLE'||a||' (COLUMN_NAME) values (:val)' using b;
This way you don't have to bother with escaping your values (SQL-injection hack) and correctly casting the type.
http://docs.oracle.com/cd/B13789_01/appdev.101/b10807/13_elems017.htm

How to insert dynamically through a variable in PL/SQL?

Lets create a table first
create table test
(
id number,
name varchar2(20)
);
Now during insert, I want to hold the data into variable first & then dynamically pass the variable into the VALUES clause like this:
declare
v_data varchar2(50);
begin
v_data:='1,sunny';
execute immediate 'insert into test values(v_data)';
commit;
end;
But its showing some errors(Not enough values)...... plz help how to achieve this??
You need to use different variables for each value
declare
v_data1 number
v_data2 varchar2(50);
begin
v_data1 :=1
v_data2 = 'sunny';
insert into test values(v_data1,v_data2);
-- Alternatively insert into test (Name) values (v_data2);
commit;
end;
Table test has two columns. You're only inserting one and not naming which column it is hence "not enough values". So you need:
INSERT INTO test (name) VALUES (data)
or probably better is to put in an ID:
INSERT INTO test (id, name) VALUES (1, data)
or simply:
INSERT INTO test VALUES (1, data)
For this kind of thing though I would use a cursor rather than dynamic SQL (or even inline SQL).
The normal way to pass values into dynamic SQL statements is with bind variables like this:
declare
v_id integer;
v_name varchar2(50);
begin
v_id := 1;
v_name := 'sunny';
execute immediate
'insert into test (id, name) values(:b1, :b2)'
using v_id, v_name;
commit;
end;
That needs one variable per value.
Your approach works, but you need to adjust your query a little:
execute immediate 'insert into test values(' || v_data|| ')';
so that the contents of your v_data variable are actually inserted into the string, not the value "v_data" itself.