I'm trying to log the state of views in my Oracle DB. For each view I want to get it's text and the last modification date.
I successfully got view text by using the following query:
select text from ALL_VIEWS where upper(view_name) like upper(<view_name>);
In SQL Navigator I also can see the modification date for each view. Is it possible to get this date by SQL query?
User need to join to user_objects. Something like:
select v.view_name, o.last_ddl_time, v.text
from user_objects o
join user_views v
on v.view_name = o.object_name
where v.view_name = upper( '&vname' )
Related
My problem is, that I would like to get column names and types etc... not only from one table or a view, but from any select query. For example when joining 2 tables.
If the select query looked like this:
Select * from employees
Then the output it would produce would have such columns as this produces:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Employees'
Now I need something like this but it must work with far more complex selects like this one for example.
select e.EmployeeId,e.EmployeeName, et.type, et.IsPermanent
from Employee e
inner join EmployeeType et
on e.EmployeeId = et.EmployeeId
Problem is that this can be any select query and I don’t know what the user will type into my app. I just need a way to get column info from anything.
Ok, so I luckily solved my issue using SqlDataReader:
var columnNames = Enumerable.Range(0,reader.FieldCount).Select(reader.GetName).ToList();
var columnFieldTypes = Enumerable.Range(0, reader.FieldCount).Select(reader.GetFieldType).ToList();
Could someone tell me what's wrong with this code? If I run the select statement only, then its returning the results.
The code is working if I remove one of the line "location" or "stored as textfile".
Also please let me know if I can specify 'delimiter' too.
create table exploderesults
location '/user/cloudera/sometest'
stored as textfile
as
select id,ph as phone, ct as city from explodetest
lateral view explode(phone)p as ph
lateral view explode(city)c as ct;
Thanks
Swap the two rows stored as and location. They have to be in a particular order according to the manual
create table exploderesults
stored as textfile
location '/user/cloudera/sometest'
as
select id,ph as phone, ct as city from explodetest
lateral view explode(phone)p as ph
lateral view explode(city)c as ct;
If you want to specify a delimiter,
create table exploderesults
row format delimited fields terminated by ','
stored as textfile
location '/user/cloudera/sometest'
as
select id,ph as phone, ct as city from explodetest
lateral view explode(phone)p as ph
lateral view explode(city)c as ct;
I have a SQL Query that accepts parameters for filtering report
(${p_year, ${p_month}, ${p_comp}) in Pentaho Report Designer.
[In Pentaho Report Designer before using view]
SELECT INSERT_DATE,EXT,DURATION_S
FROM TABLE1
WHERE CONVERT(VARCHAR(6),INSERT_DATE,112) = CONCAT(${p_year}, ${p_month})
AND DIRECTION = 'OUT'
AND EXT IN (
SELECT DISTINCT TABLE2.EXT
FROM TABLE2
WHERE ((COMP = ${p_comp} OR 'ALL' = ${p_comp}))
)
I am trying to create the view to include that query in SQL Server. However, I am not sure this is a correct view or not.
[In SQL Server]
CREATE VIEW CALLING AS
SELECT INSERT_DATE,EXT,DURATION_S
FROM TABLE1
WHERE DIRECTION = 'OUT' AND EXT IN (
SELECT DISTINCT TABLE2.EXT
FROM TABLE2
)
After that in Report Designer, I change the code to
select * from CALLING
However, I don't know how to add parameter (${p_year}, ${p_month}, ${p_comp}) into the new code. Any idea please advice.
Your current view seems to correct for me but use JOIN inside the VIEW
CREATE VIEW Calling
AS
SELECT
t1.INSERT_DATE, t2.EXT, t1.DURATION_S, t2.COMP
FROM table1 t1
INNER JOIN table2 t2 ON t2.EXT = t1.EXT
WHERE t1.DIRECTION = 'OUT'
And, you would require to call the view with WHEREclause
SELECT * FROM Calling
WHERE INSERT_DATE = #parameter AND COMP = #parameter
Or, you would also go with stored procedure where you could pass multiple parameters
I've used a script to create a view that has the table name for every table in a database, along with the number of rows of data in the respective table. Everything works fine with SELECT *. However, I'd like to query only certain rows, but I can't seem to do that.
The view was created with the following script (credit to DatabaseZone.com for the script):
CREATE VIEW RowCount_AllTables
AS
SELECT DISTINCT
sys.schemas.name, sys.tables.name AS tableName,
sys.dm_db_partition_stats.row_count AS 'RowCount'
FROM
sys.tables
INNER JOIN
sys.dm_db_partition_stats ON sys.tables.object_id = sys.dm_db_partition_stats.object_id
INNER JOIN
sys.schemas ON sys.tables.schema_id = sys.schemas.schema_id
WHERE
(sys.tables.is_ms_shipped = 0)
When I run Select * against the resulting view, I get results such as:
name tableName RowCount
dbo Table1 2
dbo Table2 1230
dbo Table3 0
If I query just the tableName column, that works fine and returns the table names. However, if I try to query the RowCount column as well, I get the error message Incorrect syntax near the keyword 'RowCount. This happens regardless of whether I qualify the database -- it seems to not recognize RowCount as a valid column that I can call in a query. So this script fails:
SELECT RowCount
FROM RowCount_AllTables;
But this one works:
SELECT tableName
FROM RowCount_AllTables;
What's going on? I'd like to be able to alias the column names in the view in my query but I can't do that so long as I can't reference the RowCount column.
(FYI running this in SQL Server 2014)
Rowcount is a reserved word, you can select reserved words using [] as:
[Rowcount]
Thanks to #sgeddes for pointing the way. Dropped the view, changed the script for creating it to use another name for the row count column, and it now works as expected. The issue was a conflict with Rowcount being a reserved word.
Changed the create table script on this line:
SELECT distinct sys.schemas.name, sys.tables.name AS tableName,
sys.dm_db_partition_stats.row_count AS 'RowCount'
to:
SELECT distinct sys.schemas.name, sys.tables.name AS tableName,
sys.dm_db_partition_stats.row_count AS 'Row_Count'
...at which point I can now reference the Row_Count column as desired.
we are migrating over to oracle from sql server side.
on sqlserver we used to have a view like the following
create view blah
AS
Select column1,
column2
FROM blah;
but doing this on oracle produces circular view error.
is this not allowed on oracle side?
You cannot have a view reference itself. It logically does not make sense. A view is essentially a cached query whose results are displayed as a table. How can a query refer to itself?
Indeed, circular view definitions are not allowed in Oracle. If you have a circular view definition, then you likely have a bug in your database code that should be addressed. Perhaps the translation from SQL server to Oracle was flawed and accidentally introduced this circular definition?
You can actually do this in ORACLE, but it is more fragile, as you need to explicitly list the output columns of your CTE. So if you change the tables, you need to manually update the CTE.
Here is an example from our db, showing how to calculate the hierarchical depth of the a record...
CREATE OR REPLACE VIEW deploy.PHARMACYDISPENSE_EX
AS
WITH SRC (
PDID, WAREID, GCN_SEQNO, QTY, UOFM, XACTDTTM, CREATEDON, PROCESSEDON,
XACTTYPE, OPDID, CLOSEDON, BYPASSEDON, BYPASSEDBY, ITEMNO, LOTNO,
EXP_DATE, VOLUMETYPE, POTYPE, DEPTH
) AS (
SELECT D.PDID, D.WAREID, D.GCN_SEQNO, D.QTY, D.UOFM, D.XACTDTTM,
D.CREATEDON, D.PROCESSEDON, D.XACTTYPE, D.OPDID, D.CLOSEDON,
D.BYPASSEDON, D.BYPASSEDBY, D.ITEMNO, D.LOTNO, D.EXP_DATE,
D.VOLUMETYPE, D.POTYPE, 0 FROM deploy.PHARMACYDISPENSE D
WHERE OPDID IS NULL
UNION ALL
SELECT D.PDID, D.WAREID, D.GCN_SEQNO, D.QTY, D.UOFM, D.XACTDTTM,
D.CREATEDON, D.PROCESSEDON, D.XACTTYPE, D.OPDID, D.CLOSEDON,
D.BYPASSEDON, D.BYPASSEDBY, D.ITEMNO, D.LOTNO, D.EXP_DATE,
D.VOLUMETYPE, D.POTYPE, (S.DEPTH + 1)
FROM deploy.PHARMACYDISPENSE D JOIN SRC S ON S.PDID = D.OPDID
)
SELECT PD.*
FROM SRC PD;
The important part here is the WITH SRC (<output column list>) AS .... You need that output column list. So it is possible, and does work, it just takes a bit more code than in SQL Server.
Your example is incomplete - well at least doesn't show the pertinent parts.:
-- create a table
CREATE TABLE Scrap
(fieldName VARCHAR2(20));
-- create a view
CREATE VIEW ScrapVW1
AS
SELECT * FROM Scrap;
-- create a second view that uses the first view
CREATE VIEW ScrapVW2
AS
SELECT * FROM Scrap
UNION ALL
SELECT * FROM ScrapVW1;
-- recreate the first view that references the 2nd view which contains a reference to itself
CREATE OR REPLACE VIEW SCRAP_VW1
AS
SELECT * FROM ScrapVW2;
Gives a circular reference error when you try to recreate ScrapVW1. I would guess you have some unintentional name collision going on in your conversion. If it's quite complex I'd get rid of the 'CREATE OR REPLACE VIEW' syntax and just use CREATE VIEW which would then give you 'ORA-00955 Name already used' error.
Oracle deals with Hierarchical problems different than SQL apparently. Instead of self referring view, you can use connect by clause
SELECT employee_id, last_name, manager_id
FROM employees
CONNECT BY PRIOR employee_id = manager_id;