SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to create a function that takes a table name and where clause as parameters


How to create a function that takes a table name and where clause as parameters

Author
Message
rasmi.v
rasmi.v
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 2
Hello Everybody,

I need to create a function in Oracle that takes the table name and where clause as parameters and returns the number of rows that satisfy this condition. Need this urgently. Any help most welcome.

Thanks,
Rasmi
Lowell
Lowell
SSC Guru
SSC Guru (300K reputation)SSC Guru (300K reputation)SSC Guru (300K reputation)SSC Guru (300K reputation)SSC Guru (300K reputation)SSC Guru (300K reputation)SSC Guru (300K reputation)SSC Guru (300K reputation)

Group: General Forum Members
Points: 300574 Visits: 42273
In SQL you have to use dynamic sql and the EXEC(EXECUTE) statement.

Oracle is the same, but you would use EXECUTE IMMEDIATE,
ie execute immediate 'drop table TEMPTBL';

it sounds like you are trying to create a procedure to do all your adhoc sql statements; you should not do that. it's bad practice, and leaves you open to SQL injection, as well as potentially poor performance because no plan will get cached.

if you have the table name and the WHERE statement, why not just do the ad hoc query directly from your application? why send it off to a proceudre?

Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
rasmi.v
rasmi.v
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 2
Hello ,

Thanks for the very fast reply. I am actually studying in uni and am doing this for an assignment :-D
I am trying to use the execute immediate but its still giving an error. If someone can tell me whats wrong in this function it would be really helpful.

CREATE OR REPLACE FUNCTION fun_sel_test (tab1 IN VARCHAR2,p_where IN VARCHAR2)
RETURN NUMBER
IS
l_count NUMBER(20);
BEGIN

EXECUTE IMMEDIATE
'SELECT COUNT(*) FROM' || tab1 ||
'WHERE' || p_where;
RETURN l_count;
END;
/

Thanks,
Rasmi
Lowell
Lowell
SSC Guru
SSC Guru (300K reputation)SSC Guru (300K reputation)SSC Guru (300K reputation)SSC Guru (300K reputation)SSC Guru (300K reputation)SSC Guru (300K reputation)SSC Guru (300K reputation)SSC Guru (300K reputation)

Group: General Forum Members
Points: 300574 Visits: 42273
look at the error you are getting; it should be very obvious;
if i used your function for a table named "Customers" and a WHERE statement that was "CustomerName LIKE 'c%'

what would your statment look like?

SELECT COUNT(*) FROMCustomersWHERECustomerName LIKE 'c%'



does that help you identify the problem?

Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum









































































































































































SQLServerCentral


Search