passing in table name as plsql parameter
I want to write a function to return the row count of a table whose name is passed in as a variable. Here's my code:
create or replace function get_table_count (table_name IN varchar2)
return number
is
tbl_nm varchar(100) := table_name;
table_count number;
begin
select count(*)
into table_count
from tbl_nm;
dbms_output.put_line(table_count);
return table_count;
end;
I get this error:
FUNCTION GET_TABLE_COUNT compiled
Errors: check compiler log
Error(7,5): PL/SQL: SQL Statement ignored
Error(9,8): PL/SQL: ORA-00942: table or view does not exist
I understand that tbl_nm is being interpreted as a value and not a reference and I'm not sure how to escape that.
plsql oracle11g
add a comment |
I want to write a function to return the row count of a table whose name is passed in as a variable. Here's my code:
create or replace function get_table_count (table_name IN varchar2)
return number
is
tbl_nm varchar(100) := table_name;
table_count number;
begin
select count(*)
into table_count
from tbl_nm;
dbms_output.put_line(table_count);
return table_count;
end;
I get this error:
FUNCTION GET_TABLE_COUNT compiled
Errors: check compiler log
Error(7,5): PL/SQL: SQL Statement ignored
Error(9,8): PL/SQL: ORA-00942: table or view does not exist
I understand that tbl_nm is being interpreted as a value and not a reference and I'm not sure how to escape that.
plsql oracle11g
add a comment |
I want to write a function to return the row count of a table whose name is passed in as a variable. Here's my code:
create or replace function get_table_count (table_name IN varchar2)
return number
is
tbl_nm varchar(100) := table_name;
table_count number;
begin
select count(*)
into table_count
from tbl_nm;
dbms_output.put_line(table_count);
return table_count;
end;
I get this error:
FUNCTION GET_TABLE_COUNT compiled
Errors: check compiler log
Error(7,5): PL/SQL: SQL Statement ignored
Error(9,8): PL/SQL: ORA-00942: table or view does not exist
I understand that tbl_nm is being interpreted as a value and not a reference and I'm not sure how to escape that.
plsql oracle11g
I want to write a function to return the row count of a table whose name is passed in as a variable. Here's my code:
create or replace function get_table_count (table_name IN varchar2)
return number
is
tbl_nm varchar(100) := table_name;
table_count number;
begin
select count(*)
into table_count
from tbl_nm;
dbms_output.put_line(table_count);
return table_count;
end;
I get this error:
FUNCTION GET_TABLE_COUNT compiled
Errors: check compiler log
Error(7,5): PL/SQL: SQL Statement ignored
Error(9,8): PL/SQL: ORA-00942: table or view does not exist
I understand that tbl_nm is being interpreted as a value and not a reference and I'm not sure how to escape that.
plsql oracle11g
plsql oracle11g
edited Dec 22 '14 at 17:07
Vadim K.
2,2881726
2,2881726
asked Dec 21 '14 at 18:22
Serge
328918
328918
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can use dynamic SQL:
create or replace function get_table_count (table_name IN varchar2)
return number
is
table_count number;
begin
execute immediate 'select count(*) from ' || table_name into table_count;
dbms_output.put_line(table_count);
return table_count;
end;
There is also an indirect way to get number of rows (using system views):
create or replace function get_table_count (table_name IN varchar2)
return number
is
table_count number;
begin
select num_rows
into table_count
from user_tables
where table_name = table_name;
return table_count;
end;
The second way works only if you had gathered statistics on table before invoking this function.
3
I would add, the second option as the major advantage the you don't have to validate thetable_name(remember Little Bobby Tables...)
– Sylvain Leroux
Dec 21 '14 at 19:15
@SylvainLeroux - Oracle provides the DBMS_ASSERT package so we can easily handle SQL injection attacks. The problem with the second approach is that its dependence on statistics means the returned value may be inaccurate or misleading, unless we gather stats on the table immediately before running the count which is (a) time-consuming (b) potentially destabilising to ongoing performance.
– APC
Apr 10 at 7:25
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f27592366%2fpassing-in-table-name-as-plsql-parameter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use dynamic SQL:
create or replace function get_table_count (table_name IN varchar2)
return number
is
table_count number;
begin
execute immediate 'select count(*) from ' || table_name into table_count;
dbms_output.put_line(table_count);
return table_count;
end;
There is also an indirect way to get number of rows (using system views):
create or replace function get_table_count (table_name IN varchar2)
return number
is
table_count number;
begin
select num_rows
into table_count
from user_tables
where table_name = table_name;
return table_count;
end;
The second way works only if you had gathered statistics on table before invoking this function.
3
I would add, the second option as the major advantage the you don't have to validate thetable_name(remember Little Bobby Tables...)
– Sylvain Leroux
Dec 21 '14 at 19:15
@SylvainLeroux - Oracle provides the DBMS_ASSERT package so we can easily handle SQL injection attacks. The problem with the second approach is that its dependence on statistics means the returned value may be inaccurate or misleading, unless we gather stats on the table immediately before running the count which is (a) time-consuming (b) potentially destabilising to ongoing performance.
– APC
Apr 10 at 7:25
add a comment |
You can use dynamic SQL:
create or replace function get_table_count (table_name IN varchar2)
return number
is
table_count number;
begin
execute immediate 'select count(*) from ' || table_name into table_count;
dbms_output.put_line(table_count);
return table_count;
end;
There is also an indirect way to get number of rows (using system views):
create or replace function get_table_count (table_name IN varchar2)
return number
is
table_count number;
begin
select num_rows
into table_count
from user_tables
where table_name = table_name;
return table_count;
end;
The second way works only if you had gathered statistics on table before invoking this function.
3
I would add, the second option as the major advantage the you don't have to validate thetable_name(remember Little Bobby Tables...)
– Sylvain Leroux
Dec 21 '14 at 19:15
@SylvainLeroux - Oracle provides the DBMS_ASSERT package so we can easily handle SQL injection attacks. The problem with the second approach is that its dependence on statistics means the returned value may be inaccurate or misleading, unless we gather stats on the table immediately before running the count which is (a) time-consuming (b) potentially destabilising to ongoing performance.
– APC
Apr 10 at 7:25
add a comment |
You can use dynamic SQL:
create or replace function get_table_count (table_name IN varchar2)
return number
is
table_count number;
begin
execute immediate 'select count(*) from ' || table_name into table_count;
dbms_output.put_line(table_count);
return table_count;
end;
There is also an indirect way to get number of rows (using system views):
create or replace function get_table_count (table_name IN varchar2)
return number
is
table_count number;
begin
select num_rows
into table_count
from user_tables
where table_name = table_name;
return table_count;
end;
The second way works only if you had gathered statistics on table before invoking this function.
You can use dynamic SQL:
create or replace function get_table_count (table_name IN varchar2)
return number
is
table_count number;
begin
execute immediate 'select count(*) from ' || table_name into table_count;
dbms_output.put_line(table_count);
return table_count;
end;
There is also an indirect way to get number of rows (using system views):
create or replace function get_table_count (table_name IN varchar2)
return number
is
table_count number;
begin
select num_rows
into table_count
from user_tables
where table_name = table_name;
return table_count;
end;
The second way works only if you had gathered statistics on table before invoking this function.
answered Dec 21 '14 at 19:01
Dmitriy
4,836111833
4,836111833
3
I would add, the second option as the major advantage the you don't have to validate thetable_name(remember Little Bobby Tables...)
– Sylvain Leroux
Dec 21 '14 at 19:15
@SylvainLeroux - Oracle provides the DBMS_ASSERT package so we can easily handle SQL injection attacks. The problem with the second approach is that its dependence on statistics means the returned value may be inaccurate or misleading, unless we gather stats on the table immediately before running the count which is (a) time-consuming (b) potentially destabilising to ongoing performance.
– APC
Apr 10 at 7:25
add a comment |
3
I would add, the second option as the major advantage the you don't have to validate thetable_name(remember Little Bobby Tables...)
– Sylvain Leroux
Dec 21 '14 at 19:15
@SylvainLeroux - Oracle provides the DBMS_ASSERT package so we can easily handle SQL injection attacks. The problem with the second approach is that its dependence on statistics means the returned value may be inaccurate or misleading, unless we gather stats on the table immediately before running the count which is (a) time-consuming (b) potentially destabilising to ongoing performance.
– APC
Apr 10 at 7:25
3
3
I would add, the second option as the major advantage the you don't have to validate the
table_name (remember Little Bobby Tables...)– Sylvain Leroux
Dec 21 '14 at 19:15
I would add, the second option as the major advantage the you don't have to validate the
table_name (remember Little Bobby Tables...)– Sylvain Leroux
Dec 21 '14 at 19:15
@SylvainLeroux - Oracle provides the DBMS_ASSERT package so we can easily handle SQL injection attacks. The problem with the second approach is that its dependence on statistics means the returned value may be inaccurate or misleading, unless we gather stats on the table immediately before running the count which is (a) time-consuming (b) potentially destabilising to ongoing performance.
– APC
Apr 10 at 7:25
@SylvainLeroux - Oracle provides the DBMS_ASSERT package so we can easily handle SQL injection attacks. The problem with the second approach is that its dependence on statistics means the returned value may be inaccurate or misleading, unless we gather stats on the table immediately before running the count which is (a) time-consuming (b) potentially destabilising to ongoing performance.
– APC
Apr 10 at 7:25
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f27592366%2fpassing-in-table-name-as-plsql-parameter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown