473,387 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

EXECUTE IMMEDIATE help

I'm trying to run an EXECUTE IMMEDIATE within a PL/SQL if loop but it
doesn't work for me. I'm trying to create a column conditionally but
it doesn't work. It fails because there are apostrophes within the
statement. How do I override the apostrophe?

DECLARE cCount NUMBER;
BEGIN SELECT count(*)
INTO cCount
FROM all_tab_columns
WHERE owner = 'Owner'
AND table_name = 'table_name'
AND column_name = 'column_name' ;

IF cCount = 0 THEN
EXECUTE IMMEDIATE 'ALTER TABLE owner.table_name
ADD (column_name CHAR(1) DEFAULT ' ' NOT NULL)';
ELSE
EXECUTE IMMEDIATE 'ALTER TABLE owner.table_name
MODIFY column_name DEFAULT ' '';
END IF;
END;

Thanks
Jul 19 '05 #1
4 28397
finlma wrote:
I'm trying to run an EXECUTE IMMEDIATE within a PL/SQL if loop but it
doesn't work for me. I'm trying to create a column conditionally but
it doesn't work. It fails because there are apostrophes within the
statement. How do I override the apostrophe?

DECLARE cCount NUMBER;
BEGIN SELECT count(*)
INTO cCount
FROM all_tab_columns
WHERE owner = 'Owner'
AND table_name = 'table_name'
AND column_name = 'column_name' ;

IF cCount = 0 THEN
EXECUTE IMMEDIATE 'ALTER TABLE owner.table_name
ADD (column_name CHAR(1) DEFAULT ' ' NOT NULL)';
ELSE
EXECUTE IMMEDIATE 'ALTER TABLE owner.table_name
MODIFY column_name DEFAULT ' '';
END IF;
END;

Thanks


You don't override, you escape - with an extra quote:
to insert "It's XMAS time" in a table, you would:
insert into table(column) values ('It''s XMAS time');
And there's inefficient code: you do not need to know
*how* many (into cCount), you just want to know IF any:
select 1 into cCount from dual
where exists (...)
would be more efficient.
--
Regards, Frank van Bortel

Jul 19 '05 #2
finlma wrote:
I'm trying to run an EXECUTE IMMEDIATE within a PL/SQL if loop but it
doesn't work for me. I'm trying to create a column conditionally but
it doesn't work. It fails because there are apostrophes within the
statement. How do I override the apostrophe?

DECLARE cCount NUMBER;
BEGIN SELECT count(*)
INTO cCount
FROM all_tab_columns
WHERE owner = 'Owner'
AND table_name = 'table_name'
AND column_name = 'column_name' ;

IF cCount = 0 THEN
EXECUTE IMMEDIATE 'ALTER TABLE owner.table_name
ADD (column_name CHAR(1) DEFAULT ' ' NOT NULL)';
ELSE
EXECUTE IMMEDIATE 'ALTER TABLE owner.table_name
MODIFY column_name DEFAULT ' '';
END IF;
END;

Thanks


You don't override, you escape - with an extra quote:
to insert "It's XMAS time" in a table, you would:
insert into table(column) values ('It''s XMAS time');
And there's inefficient code: you do not need to know
*how* many (into cCount), you just want to know IF any:
select 1 into cCount from dual
where exists (...)
would be more efficient.
--
Regards, Frank van Bortel

Jul 19 '05 #3
you would like to include brackets in the statement as well like this
EXECUTE IMMEDIATE ('ALTER TABLE owner.table_name
ADD (column_name CHAR(1) DEFAULT '' '' NOT NULL)');
Thanks

Faheem
fi********@hotmail.com (finlma) wrote in message news:<8b**************************@posting.google. com>...
I'm trying to run an EXECUTE IMMEDIATE within a PL/SQL if loop but it
doesn't work for me. I'm trying to create a column conditionally but
it doesn't work. It fails because there are apostrophes within the
statement. How do I override the apostrophe?

DECLARE cCount NUMBER;
BEGIN SELECT count(*)
INTO cCount
FROM all_tab_columns
WHERE owner = 'Owner'
AND table_name = 'table_name'
AND column_name = 'column_name' ;

IF cCount = 0 THEN
EXECUTE IMMEDIATE 'ALTER TABLE owner.table_name
ADD (column_name CHAR(1) DEFAULT ' ' NOT NULL)';
ELSE
EXECUTE IMMEDIATE 'ALTER TABLE owner.table_name
MODIFY column_name DEFAULT ' '';
END IF;
END;

Thanks

Jul 19 '05 #4
you would like to include brackets in the statement as well like this
EXECUTE IMMEDIATE ('ALTER TABLE owner.table_name
ADD (column_name CHAR(1) DEFAULT '' '' NOT NULL)');
Thanks

Faheem
fi********@hotmail.com (finlma) wrote in message news:<8b**************************@posting.google. com>...
I'm trying to run an EXECUTE IMMEDIATE within a PL/SQL if loop but it
doesn't work for me. I'm trying to create a column conditionally but
it doesn't work. It fails because there are apostrophes within the
statement. How do I override the apostrophe?

DECLARE cCount NUMBER;
BEGIN SELECT count(*)
INTO cCount
FROM all_tab_columns
WHERE owner = 'Owner'
AND table_name = 'table_name'
AND column_name = 'column_name' ;

IF cCount = 0 THEN
EXECUTE IMMEDIATE 'ALTER TABLE owner.table_name
ADD (column_name CHAR(1) DEFAULT ' ' NOT NULL)';
ELSE
EXECUTE IMMEDIATE 'ALTER TABLE owner.table_name
MODIFY column_name DEFAULT ' '';
END IF;
END;

Thanks

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: michi | last post by:
Hello there... Can anybody tell me what is the difference when I excecute a sql statement within pl sql with/without "execute immediate" statement Thanks Michi :)
3
by: Agoston Bejo | last post by:
I am looking for the PL/SQL equivalent of the VBScript Exec and/or Eval functions, i.e. I want to be able to dynamically create a statement, then execute it in the current PL/SQL context, e.g. ...
5
by: Gustavo Randich | last post by:
Hello, I'm writing an automatic SQL parser and translator from Informix to DB2. Now I'm faced with one of the most difficult things to translate, the "foreach execute procedure" functionality...
1
by: lakon15 | last post by:
Dear all, I'll try to convert from SQL server Store Procedure to DB2 Store Procedure. I've make SP under DB2 like this CREATE PROCEDURE GetSearchedRecords (pKeyWords VARCHAR(1000), ...
5
by: umesh049 | last post by:
Hello, I want to delete all the date of all the table in a scheman. but i got error at execute immediate statement. can any body help me. thanks
3
by: nghivo | last post by:
My environment DB2 9.1.4 on Sun OS I write a C embedded SQL to load data. I declare host vars as: EXEC SQL BEGIN DECLARE SECTION; SQL TYPE IS CLOB(599999) sqlStr; EXEC SQL END DECLARE...
3
by: Rahul Babbar | last post by:
Hi, I have the following doubt. Suppose I use the execute immediate statement and the statement to be executed is a Select statement from the sysibm.sysdummy1 table which will always return...
2
by: finlma | last post by:
I'm trying to run an EXECUTE IMMEDIATE within a PL/SQL if loop but it doesn't work for me. I'm trying to create a column conditionally but it doesn't work. It fails because there are apostrophes...
6
by: Oliver | last post by:
I'm fairly new to DB2. I have been assigned to build a delete trigger that finds the data type of each of the table's fields so that the trigger can then build a string consisting of OLD values...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.