473,385 Members | 1,409 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,385 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 28396
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.