PL/SQL - printing a value - oracle

I have a field Base that has the values for example 0000000000. I need to print it in txt file through stored procedure. Its data type is CHAR(10). But while printing it is just printing a single value like 0.
Please give me the suggestions to correct this.

Option1:
You should try using LPAD function.
Select LPAD(Columnname, 10, '0') from yourtable;
Option2:
try forcing TO_CHAR while printing

maybe your procedure implicitly convert your column to number.
try to change the call of your column to by explicit char by call to_char(column_name) in your procedure.

Related

Lazarus Pascal setting value from a textbox

I have to set up some sql queries.
The command I want to enter is INSERT INTO tblTest VALUES (1);
Here is my code
SQLQuery1.SQL.text:=('INSERT INTO tblTest VALUES (' (enterid.text) ')' );
enterid is my textbox that I wish to input a number or name into
How would I get the brackets before and after it to create my command?
I can see two problems with your code. The entire SQL statement is bracketed which is wrong (at least, it's wrong in Delphi, so I assume that it's also wrong in Lazarus. Secondly, the 'values' statement doesn't need the brackets and is liable to store the literal string, "enterid.text".
Better to write like this:
SQLQuery1.SQL.text:= 'INSERT INTO tblTest VALUES (:p1);
SQLQuery1.params[0].asstring:= enterid.text;
// alternatively SQLQuery1.ParamByName ('p1').asstring:= enterid.text;
SQLQuery1.execsql;

Variable column number pl/sql cursor

I have a function that returns a Select clause with a variable number of columns (from 2 to 31). Then, I need to do inserts into a table using the first + each of the other columns. For example, if my Select returns: ('A', '1', '2', '3') I need to insert ('A','1'), ('A','2') and ('A','3') into a given table. The problem is that I can't know how many columns I'll have in the original Select clause.
I tried to use the Select clause to open a cursor but, is there any way I can know how many columns the cursor has and, then, fetch them separately? Is there any other way to do this?
Thanks a lot in advance,
Ander.
You are in need of dbms_sql.describe_columns
Thanks,
I finally managed to solve it. As Sanders says, using dbms_sql, I open a cursor, parse it, get the column number with describe_columns, and in a loop, define their formats (luckily for me, all of them are varchar2(6)).
Finally, I use dbms_sql.column_value to get each column's value while I fetch all the rows.

How can I copy a BLOB from a parameter to a local variable in Oracle PL/SQL?

I have an Oracle PL/SQL routine that takes a BLOB as a parameter. The BLOB contains a .jpg file. I want to assign the BLOB parameter to a local variable. I then want to insert (or update) a BLOB column in a table the BLOB varaible.
I have tried something like this:
declare
vATTACHMENT blob;
begin
dbms_lob.createtemporary(vATTACHMENT, false, dbms_lob.session);
dbms_lob.write(vATTACHMENT, dbms_lob.lobmaxsize, 1, :pATTACHMENT));
-- do some stuff
insert into attachments (attachment, file_name)
values (vATTACHMENT, vFILE_NAME);
end;
But I get the following error:
ORA-06502: PL/SQL: numeric or value error
ORA-06512: at "SYS.DBMS_LOB", line 811
ORA-06512: at line 21
I have also tried a direct assignment like vATTACHMENT := :pATTACHMENT; but that doesn't want to work either.
I think you can use DEFAULT in a variable declaration to assign a value to it without using the assignment operator :=, e.g.:
declare
vATTACHMENT blob DEFAULT :pATTACHMENT;
begin
-- rest of your code...
I can only get the exact error when pAttachment is null or empty (tested in 10.2.0.5). If it isn't, I get ORA-21560 instead, as it doesn't like lobmaxsize. If I do this instead, it's OK:
dbms_lob.write(vATTACHMENT, dbms_lob.getlength(:pATTACHMENT), 1, :pATTACHMENT));
But from your comments you have some issue referencing the bind variable more than once, and for some reason I don't quite understand you can't do assignments in PL/SQL as := is misinterpreted - which makes using PL/SQL at all somewhat impractical, I'd have thought. I'm a bit unclear if you're running this as an anonymous block directly from your client; if so maybe you should consider making it a stored procedure to avoid both those issues? You could then just do:
vATTACHMENT := :pATTACHMENT;
... though then it would be a parameter passed to the procedure rather than a bind variable, and you wouldn't need to both with the copy in the first place (as Dave Costa suggests).
If you are stuck with running it like this you could incur a context switch and do:
select :pATTACHMENT into vATTACHMENT from dual;
But that's not ideal; if you just want to make a copy, why aren't you using the copy procedures?
dbms_lob.copy(vATTACHMENT, :pATTACHMENT, dbms_lob.getlength(:pATTACHMENT));
... which like that still breaks your re-referencing-bind-variables restriction, but this one does understand lobmaxsize:
dbms_lob.copy(vATTACHMENT, :pATTACHMENT, dbms_lob.lobmaxsize);
You'll still get an error (ORA-22994 I think) if :pATTACHMENT is empty or null, so you'll need to make sure it isn't before calling the block, or check inside.
The fourth parameter to DBMS_LOB.WRITE is supposed to be either a RAW or a VARCHAR2. If I understand you correctly, you're binding an existing BLOB to the :pATTACHMENT placeholder, so you're passing an incorrect type.
Why do you need to assign it to a temporary BLOB at all? It seems to me that this should work:
insert into attachments (attachment, file_name)
values (:pATTACHMENT, vFILE_NAME);
For instance, this runs fine:
DECLARE
PROCEDURE insert_blob( p_blob BLOB) IS
BEGIN
INSERT INTO t_dave (b) VALUES (p_blob);
END;
BEGIN
insert_blob( empty_blob() );
END;
/

oracle default value for a column

You know how we could use:
dateStamp DATE DEFAULT sysdate
to assign a default value to a column in table_x. What if I want to assign a default function? Can I do that?
The function will have some values from "table_params" to run some formula including a column named : "base" in table_x.
I could possibly write a cursor to loop through and run an update statement, but I was just curious if this is possible.
thanks in advance.
From Oracle documentation:
Restriction on Default Column Values
A DEFAULT expression cannot contain references to PL/SQL functions or to other columns, the
pseudocolumns CURRVAL, NEXTVAL, LEVEL, PRIOR, and ROWNUM, or date
constants that are not fully specified.
Either use a trigger (as was already mentioned) or run an UPDATE statement after your INSERT statement(s) (shouldn't be a problem if you keep your DML in PL/SQL).
You can write an INSERT trigger for the table that calls the function you want.

How do you convert SYS_GUID() to varchar?

In oracle 10g, how do you convert SYS_GUID() to varchar? I am trying something like:
select USER_GUID from user where email = 'user#example.com'
Which returns the RAW byte[]. Is it possible to use a function to convert the RAW to VARCHAR2 in the SQL statement?
Don't forget to use HEXTORAW(varchar2) when comparing this value to the RAW columns.
There is no implicit convesion from VARCHAR2 to RAW. That means that this clause:
WHERE raw_column = :varchar_value
will be impicitly converted into:
WHERE RAWTOHEX(raw_column) = :varchar_value
, thus making indices on raw_column unusable.
Use:
WHERE raw_column = HEXTORAW(:varchar_value)
instead.
Use RAWTOHEX(USER_GUID).
select RAWTOHEX(USER_GUID) from user where email = 'user#example.com'
Please don't mod-1 if I'm wrong. I'm going from memory so this a disclaimer to verify.
TO_CHAR is actually different between SQL and PL/SQL.
In SQL TO_CHAR does not take a raw as you have found out.
In PL/SQL To_CHAR will take a raw value.
So if you're in a procedure anyways, sometimes it easier to use a variable, but if you're just using SQL, go with the other answers here.
select CAST (USER_GUID AS VARCHAR2(100)) from user where email = 'user#example.com'

Resources