Link to home
Start Free TrialLog in
Avatar of emayuga
emayuga

asked on

Copying data from varchar to blob field

Hi,

I need to change a field's data type from varchar to blob, while retaining the data. I'm following the 6-step procedure (which I've also read from this site)

1. create temp varchar
2. copy data from original varchar to temp column
3. drop original varchar
4. create blob field
5. copy data from temp varchar to blob
6. drop temp varchar

I am having problem with step 5. Copying data from varchar to blob is not as straightforward as using an Update statement. I know I should be using blob cursor, and I've already gathered sql statements to create & insert record to blob field. But what's missing is how to iterate through all the records. And the sql statements are not executing successfully (I'm using IBExpert)

DECLARE blob_cur CURSOR FOR
INSERT BLOB description INTO pr_product;

OPEN blob_cur INTO description;

INSERT CURSOR blob_cur VALUES (temp_desc);

CLOSE blob_cur;


Thanks for your time,

estela : )
ASKER CERTIFIED SOLUTION
Avatar of BAlexandrov
BAlexandrov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BAlexandrov
BAlexandrov

If you use Firebird you can do directly
"update my_table set blob_collumn = varchar_collumn"
Next is an example of stored procedure that you can alter to your needs.
If we have this table:

CREATE TABLE NEW_TABLE (
    ID     INTEGER NOT NULL,
    PRICE  NUMERIC(15,2),
    CODE   INTEGER,
    BL     BLOB SUB_TYPE 2 SEGMENT SIZE 4096,
    VAR    VARCHAR(100)
);

ALTER TABLE NEW_TABLE ADD PRIMARY KEY (ID);

And you want to copy var collumn to bl collumn

CREATE PROCEDURE NEW_PROCEDURE
AS
  declare variable var_temp varchar(100);
  declare variable id int;
begin
  for Select id,var from new_table
  into :id,:var_temp
  do
  Begin
    update new_table set  bl = :var_temp where id = :id;
  end
end

Then execute it
Avatar of emayuga

ASKER

Unfortunately, I am just using IB6..
Thanks for the sample script : )
However, I am encountering an error in declaring a blob cursor. Maybe you can help with this one too..

I have a PR_PRODUCT table containing the ff. fields:
  ID  INTEGER NOT NULL,
  Description  BLOB,
  Temp_Desc VARCHAR(125),

Here's the procedure:

CREATE PROCEDURE Populate_Prod_Desc
AS
  declare variable var_temp varchar(125);
  declare variable id int;
  declare blob_cur cursor for
  insert blob description into pr_product;

begin
  open blob_cur into description;
  for Select id, temp_desc from pr_product
  into :id, :var_temp
  do
  Begin
    insert cursor blob_cur values (:var_temp) where id = :id;
  end
  close blob_cur;
end

I am encountering the error "Token Unknown - line 5, char 9. blob_cur" when executing this statement. There's probably something wrong with the syntax...?

Thanks,
estela : )
I totally cannot understand what you are trying to do... :(


Plase read the manuals about working with cursors
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:
       to accept BAlexandrov's answer
Please leave any comments here within the next four (4) days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

If you will get a useful answer to your newer question next time, please accept it after evaluating. By this way the experts will get sooner his expert points for supporting you. If you have question about, please look at  https://www.experts-exchange.com/help.jsp.

kacor
EE Cleanup Volunteer