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 am writing a trigger and from the trigger I need to access so called "magic table" insert but instead of data I need to retrieve a list of columns and table name. Is that possible? I did some research but could not find anything relevant. Would greatly appreciate a push in the right direction
Your question has to be solved step by step. The first step is to get the table name. Which you can get from the following solution, which uses ##PROCID.
SELECT
OBJECT_NAME(parent_id) AS [Table],
OBJECT_NAME(object_id) AS TriggerName
FROM
sys.triggers
WHERE
object_id = ##PROCID
https://stackoverflow.com/a/727109/1339826
Then you can get the column names from INFORMATION_SCHEMA, like so:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'TABLE_NAME'
I have a sql server database which contains 7000 tables and every table ends with a date. I need to find out all the tables of a particular date.
I have the table names as follows:
HouseKeeping_Stage1_12_6_14,
HouseKeeping_Stage1_13_6_14,
HouseKeeping_Stage1_14_6_14,
HouseKeeping_Stage2_12_6_14,
HouseKeeping_Stage2_13_6_14
I want to find out all the records which are associated with the date 12_6_14.
Please let me know how to write the query.
Using SSMS Object Explorer, right-click on the Tables node and select Filter-->Filter Settings. The list will be filtered accordingly.
Alternatively, you can query the catalog or INFORMATION_SCHEMA views:
SELECT
OBJECT_SCHEMA_NAME(object_id) AS schema_name
, name
FROM sys.tables
WHERE name LIKE '%[_]13[_]6[_]14%';
SELECT
TABLE_SCHEMA
, TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE '%[_]13[_]6[_]14%';
This should do the trick:
SELECT * FROM information_schema.tables
where table_name like '%12_6_14%'
Here is an example: http://sqlfiddle.com/#!6/304bd/1/0
I'd like to get the list of columns in a temporary table, similar to the INFORMATION_SCHEMA.columns view. However, this code:
select *
from tempdb.INFORMATION_SCHEMA.columns
where TABLE_CATALOG = 'tempdb'
and TABLE_NAME like '#myTemporaryTable%'
returns one row per column and per session. Is it safe to do this:
select distinct column_name,data_type
from tempdb.INFORMATION_SCHEMA.columns
where TABLE_CATALOG = 'tempdb'
and TABLE_NAME like '#myTemporaryTable%'
I have a feeling it isn't, even if you tighten up the like clause so it won't match myTemporaryTable and myTemporaryTable2.
If you really need query tempdb, I would use object_id
SELECT *
FROM tempdb.sys.columns
WHERE object_id = OBJECT_ID('tempdb..#myTemporaryTable')
I need a query in sql to get total columns in a table.Can anybody help?
SELECT COUNT(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_CATALOG = 'database' AND TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'table'
This query gets the columns name
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'YourTableName'
And this one gets the count
SELECT Count(*) FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'YourTableName'
In MS-SQL Server 7+:
SELECT count(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'mytable'
The below query will display all the tables and corresponding column count in a database schema
SELECT Table_Name, count(*) as [No.of Columns]
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'dbo' -- schema name
group by table_name
Select Table_Name, Count(*) As ColumnCount
From Information_Schema.Columns
Group By Table_Name
Order By Table_Name
This code show a list of tables with a number of columns present in that table for a database.
If you want to know the number of column for a particular table in a database
then simply use where clause e.g. where Table_Name='name_your_table'
You can try below query:
select
count(*)
from
all_tab_columns
where
table_name = 'your_table'
It can be done using:-
SELECT COUNT(COLUMN_NAME) 'NO OF COLUMN' FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Address'
Correction to top query above, to allow to run from any database
SELECT COUNT(COLUMN_NAME) FROM [*database*].INFORMATION_SCHEMA.COLUMNS WHERE
TABLE_CATALOG = 'database' AND TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'table'
In my situation, I was comparing table schema column count for 2 identical tables in 2 databases; one is the main database and the other is the archival database. I did this (SQL 2012+):
DECLARE #colCount1 INT;
DECLARE #colCount2 INT;
SELECT #colCount1 = COUNT(COLUMN_NAME) FROM MainDB.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'SomeTable';
SELECT #colCount2 = COUNT(COLUMN_NAME) FROM ArchiveDB.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'SomeTable';
IF (#colCount1 != #colCount2) THROW 5000, 'Number of columns in both tables are not equal. The archive schema may need to be updated.', 16;
The important thing to notice here is qualifying the database name before INFORMATION_SCHEMA (which is a schema, like dbo). This will allow the code to break, in case columns were added to the main database and not to the archival database, in which if the procedure were allowed to run, data loss would almost certainly occur.
To get the list of all columns of the SQL table
select column_name from information_schema.columns where table_name=[dbo].[your_table_name]
To get the list of number of columns of the SQL table
select count(column_name) from information_schema.columns where table_name=[dbo].[your_table_name]
To get the total number of columns in table.
SELECT COUNT(COLUMN_NAME) FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'database_name' AND TABLE_NAME = 'table_name';