Dynamic sql to get count from all tables by date in oracle - dynamic-sql

I am trying to get count from all the tables in particular schema for particular date. I know how to get counts but struggling with Adding where clause. Please advice.

in you plsql code:
select count(*) from dba_objects where object_type=''TABLE'' and owner=v_schema_name and created > v_date;
-- v_schema_name and v_date are passed in variables

Related

Counting Values in a repeated field in BigQuery

I want to select rows that have more thank k values in a repeated field. (consider for example selecting user that have more than 3 email addresses)
In Standard SQL I know I can use
SELECT * FROM dataset.users
WHERE array_length(email_address) > 3
But what is the way to do this in BigQuery legacy SQL?
No need for a subquery; you should be able to filter with OMIT RECORD IF directly:
SELECT *
FROM dataset.users
OMIT RECORD IF COUNT(email_address) <= 3;
Do you mind commenting on why you want to use legacy SQL, though? If you encountered a problem with standard SQL I'd like to understand what it was so that we can fix it. Thanks!
Counting Values in a repeated field in BigQuery
BigQuery Legacy SQL
SELECT COUNT(email_address) WITHIN RECORD AS address_count
FROM [dataset.users]
If you want then to count output rows - you can use below
SELECT COUNT(1) AS rows_count
FROM (
SELECT COUNT(email_address) WITHIN RECORD AS address_count
FROM [dataset.users]
)
WHERE address_count> 3

Stored PL/SQL function

I'm trying to implement a stored function in PL/SQL that finds the total number of cities visited by a given driver, return the driving license number.
I tried something like this, but i keep getting "Function created with compilation errors.". The thing is, oracle 11g doesn't show where is the error at?
Here is what i got.
create or replace function driverL return number
as licence L#%TYPE;
begin
select L#, count(T#)
from driver d
join trip t ON d.L# = t.L#
join leg l ON l.t# = l.t#;
return licence;
end;
/
How do i consider multiple visits to the same city as one visit?
If you are using SQL*Plus you can use the statement show errors after the create statement to show the compilation errors for the most recently created or altered stored procedure/function/package.
In your code I see at least two problems:
You are missing an INTO clause in your select
The column L# in your SELECT clause is ambiguously defined since two tables have a column with that name

Having trouble in getting the number of columns in a table in oracle using TOAD?

I am trying to get number of columns in a table using the following query:
select count(*)
from user_tab_columns
where table_name='MYTABLE';
but the results are shown as zero, even I had many columns in the table.
Could some one help me where I went wrong ?
You are using the wrong system table.
Try this.
select count(*) from ALL_TAB_COLUMNS where table_name='MYTABLE';

SQL Server Stored Procedure - Use Row Count in Select query

My stored procedure (SQL Server 2005) returns a dataset where one field depends, among other things, on the number of rows returned by the query. I can make a simplified first query that allows me to get ##ROWCOUNT but, in that case, the procedure returns the two sets, which is not what I want.
I tried putting the first query in a WITH statement but haven't found the syntax to extract the row count and put it in a variable that I could use in the second query. An alternative would be to get ##ROWCOUNT from the first query and tell the procedure to return only the result of the second query.
There are probably better ways to do that but my expertise in SQL is quite limited...
Thanks for any help!
Is this what you're looking for? If not, could you please describe your problem in more details (perhaps, with code snippets)
alter procedure ComplicatedStoredProcedure as
begin
declare #lastQueryRowCount int
-- Storing the number of rows returned by the first query into a variable.
select #lastQueryRowCount =
-- First resultset (not seen by caller).
(select count(*) from A where ID > 100)
-- Second resultset. This will be the actual result returned from the SP.
select * from B where SomeDependentField > #lastQueryRowCount
end

Column count in oracle10g

I am working with oracle10g. How can I determine the number of columns in a relation specified as a SQL query?
You need to take a look at DBMS_SQL package. A select statement can include n columns, thus you need to parse it manually using PL/SQL.
DBMS_SQL.REC_TAB structure will give you plenty of information about your select statement.
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/d_sql.htm
You can dba_tab_columns synonym.
SELECT COUNT(COLUMN_NAME) from DBA_TAB_COLUMNS
WHERE TABLE_NAME='NAME_HERE_RELATION';