I know i need to use this query to get the list of tables for a schema:
select table_name from all_tables where owner='schema'
I know the following query counts the record in a table:
select count(*) from schema.table
There are 2400+ tables in that schema.
My question is how to count the number of records from all the tables using one step?
You can use DBMS_XMLGEN.GETXMLTYPE function to do this in one shot:
SQL> select table_name
2 , to_number
3 ( extractvalue
4 ( dbms_xmlgen.getxmltype('select count(*) c from ' || table_name)
5 , '/ROWSET/ROW/C'
6 )
7 ) cnt
8 from user_tables
9 order by table_name
10 /
TABLE_NAME CNT
------------------------------ ----------
... [output removed] ...
71 rows selected.
But if your schema contains a lot of data, this might take a long time. Just selecting NUM_ROWS might be sufficient if estimations are ok as well.
Regards,
Rob.
The table ALL_TABLES contains the column NUM_ROWS. (You can get a description of the table with the following SQL statement: DESCRIBE ALL_TABLES;)
The following statement shows the number of records for every table:
SELECT TABLE_NAME, NUM_ROWS FROM ALL_TABLES WHERE OWNER='SCHEMA';
To get the number of records in all tables of your schema, use:
SELECT SUM(NUM_ROWS) FROM ALL_TABLES WHERE OWNER='SCHEMA';
Related
We have an internal and an external table. The internal table is actually a copy of the external table with some fields renamed and they are roughly the same. For some reason, the data in the internal table might not match the external table because of inappropriate operation. Here is the case:
SELECT COUNT(*) AS [Total Rows]
FROM [dbo].[Auct_Car_Ex];
-- (ANS.) 76716
SELECT COUNT(*) AS [Total Rows]
FROM [dbo].[Auct_Car];
-- (ANS.) 76716
They have the same number of rows.
SELECT COUNT(DISTINCT([HORSEPOWER]))
FROM [dbo].[Auct_Car_ex];
-- (ANS.) 459
SELECT COUNT(DISTINCT([Horsepower]))
FROM [dbo].[Auct_Car];
-- (ANS.) 458
However, the number of distinct Horsepower is different. I'd like to know which value of HORSEPOWER exists in Auct_Car_ex but not in Auct_Car. How can I find it?
Just use EXCEPT
SELECT acx.HORSEPOWER
FROM dbo.Auct_Car_ex acx
EXCEPT
SELECT ac.Horsepower
FROM dbo.Auct_Car ac;
yes,it is easy by sub-query.
SELECT [HORSEPOWER]
FROM [dbo].[Auct_Car_ex]
WHERE [HORSEPOWER] NOT IN (
SELECT [Horsepower]
FROM [dbo].[Auct_Car]
GROUP BY [Horsepower]
)
GROUP BY [HORSEPOWER]
This looks like a case for not exists
select horsepower
from Auct_Car_ex x
where not exists (
select * from Auct_Car a
where a.horsepower = x.horsepower
);
I am new to snowflake. I am creating a view in snowflake as below
create view TABLENAME_VIEW as select * from test.stage.TABLENAME;
while running a select statement on information_schema.tables, I am getting the row_count of view table as null whereas doing select count(*) on the view tables gives the count.
Can someone please help on this.
TABLE_NAME
ROW_COUNT
TABLE_CATALOG
TABLE_TYPE
TABLENAME
5
TEST
BASE TABLE
TABLENAME_VIEW
NULL
TEST
VIEW
select count(*) from TABLENAME_VIEW;
COUNT(*)
5
Views are not tables (unless it an materializes view). So they have no rows. When you select from the view it becomes a projection of the underlying table/s
This can be seen by creating another view that has a “WHERE false” thus will return 0 to you select count(*) from the will return 0, but it will have no row. OR if your view had “limit 1”. It will have a count of 1, but which one is the magic part.
I could not find answer to this question, despite it being very basic. How do I know whats the data type of all columns in SQL Server management System?
Col1 Col2 Col3 and so on
I wish to know the datatypes of each column in say Table1 where Table1 is the name of my table .
There are couple of options to see the data types of columns of the desired table -
Option 1
sp_help <tableName> e.g. sp_help Table1
Option 2
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Table1'
Option 3
Expand the Tables
Expand the desired table
Expand the columns
There are many several way to do this, one of them is to use schema :
select *
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = 'Table1' or
COLUMN_Name = 'col1';
I have two columns in a table with different data types, how can i compare these two columns to look for similar values.
Table
column_a(int) column_b(nvarchar)
------------- -----------------
1 Apple
2 Mango
3 5
4 2
5 Book
I tried below query but did not work. Kindly suggest.
SELECT * FROM tab
WHERE
CAST(column_a AS nvarchar)
IN
(SELECT column_b FROM tab)
Thanks in advance
Use self join for this
SELECT A.*
FROM TAB A
INNER JOIN TAB B ON CAST(A.column_a as nvarchar) = LTRIM(RTRIM(B.column_b))
I would use TRY_CONVERT and an EXISTS to do this:
CREATE TABLE dbo.YourTable (ColumnA int,
ColumnB nvarchar(5));
INSERT INTO dbo.YourTable (ColumnA,ColumnB)
VALUES (1,'Apple'),
(2,'Mango'),
(3,'5'),
(4,'2'),
(5,'Book');
GO
SELECT YT.ColumnA,
YT.ColumnB
FROM dbo.YourTable YT
WHERE EXISTS (SELECT 1
FROM dbo.YourTable sq
WHERE YT.ColumnA = TRY_CONVERT(int,sq.ColumnB));
DB<>Fiddle
This, however, will perform poorly in a large table as the query is not SARGable (due to the TRY_CONVERT). As I mentioned in the comments, if you need to store numerical data, use a numerical data type.
List item
I am looking for a way query to query a table and add a column with the table name, without explicitly writing the actual 'tablename' within the select statement. Is there a way to do this?
For example I want;
Table name: Construction
The original columns would be Modif_num, modif_desc.
I'd like a query with these results;
The original columns would be Modif_num, modif_desc.
MODIF_NUM TABLE_NAME MODIF_DESC
2 Construction Quality
2 Construction Quality
2 Construction Quality
2 Construction Quality
A regular select * would yield
MODIF_NUM MODIF_DESC
2 Quality
2 Quality
2 Quality
2 Quality
In this instance i would use excel.
column A : table name
column B : ="select cast('"&A1&"' as nvarchar(50)) as tablename ,* into TARGETTABLE from "& A1
Then fill column A with all your table names.. then copy and paste column B into SSMS
This assumes based on your comment this is a one off task. If its not a one off task use the same logic to generate a bunch of strings and execute them.
Ah Wait sorry you cannot do select into repeatedly, what am i thinking.. sorry more like this:
In your select statement you can return a column based on a string, for example:
SELECT 'Construction' As Table_Name, MODIF_NUM FROM MyTable
OR
SELECT 'Construction' As Table_Name, * FROM MyTable
To bring them together, a UNION may work:
SELECT 'Construction' As Table_Name, MODIF_NUM, MODIF_DESC FROM tblConstruction
UNION ALL
SELECT 'Demolition' As Table_Name, MODIF_NUM, MODIF_DESC FROM tblDemolition
UNION ALL
SELECT 'Reconstruction' As Table_Name, MODIF_NUM, MODIF_DESC FROM tblReconstruction
Does this help?
Try this query:
SELECT TABLE_NAME, a.*
FROM [Construction] a,
INFORMATION_SCHEMA.TABLES