Cannot comment on Redshift view's columns - database

Hello all you Redshift wizards,
I have this issue that's causing me head aches!
I create a view in Redshift:
CREATE OR REPLACE VIEW my_schema.my_view AS
SELECT aggr.unit,
aggr.date_key,
myorg.ultimate_org_sk,
aggr.user_role AS derived_user_role,
SUM(aggr.users) AS total_users
FROM my_schema.master_table aggr
INNER JOIN my_schema.my_organization myorg
ON aggr.my_organization_sk = myorg.my_organization_sk
WHERE aggr.unit = 'weekly'
GROUP BY 1, 2, 3, 4;
WITH NO SCHEMA BINDING;
This works fine.
Now When I try to comment on the view, it works ok:
COMMENT ON VIEW my_schema.my_view IS 'View of Weekly Aggregates';
But when I try to comment on any column:
COMMENT ON COLUMN my_schema.my_view.unit IS 'The period of aggregation (daily/weekly/monthly/quarterly/biyearly/yearly)';
I get an error message:
[42703][500310] [Amazon](500310) Invalid operation: column "unit" of relation "my_view" does not exist;
Which is very strange! When I run a select, I get data back:
SELECT * FROM my_schema.my_view LIMIT 10;
SELECT unit FROM my_schema.my_view LIMIT 10;
Now, the really weird bit:
If I check the information_schema table, I find no records of my view:
SELECT table_name,
ordinal_position AS position,
column_name
FROM information_schema.columns
WHERE table_schema = 'my_schema'
AND table_name = 'my_view'
ORDER BY table_name, ordinal_position;
No data returned....
But When I query the svv_columns table, I get back all the columns:
SELECT *
FROM svv_columns
WHERE table_schema = 'my_schema'
AND table_name = 'my_view'
ORDER BY table_name, ordinal_position ;
table_catalog
table_schema
table_name
column_name
...
my_db
my_schema
my_view
unit
...
my_db
my_schema
my_view
date_key
...
my_db
my_schema
my_view
ultimate_org_sk
...
my_db
my_schema
my_view
derived_user_role
...
my_db
my_schema
my_view
total_users
...
What is happening here? and how can I comment on Columns in this view?
And yes, I dropped the View and re-created it several times, but still facing the same issue!
Any help is much appreciated.

I also came across this issue. But I have some views that I can comment. I realised that if you have
WITH NO SCHEMA BINDING;
then you cannot add comments to the view!
I don't understand why this behaviour exists. I would think that the comments are on the view object, irrespective of whether it is bound to a schema or not.
Then, looking at the OP, it seems that if it is WITH NO SCHEMA BINDING; it probably isn't storing the info in information_schema table.

Related

Snowflake Information Schema behavior for View tables

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.

Is there a way to retrieve column name list from "magic" tables: insert and delete

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'

How to find a table in sql server if only the partial table name is known

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

How can I get the list of columns from a temporary table?

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')

To get total number of columns in a table in sql

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';

Resources