Is it possible for an SQL table to have null columns?

I use the following query to collect information about table columns:

SELECT COLUMN_NAME, ORDINAL_POSITION, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, Is_NULLABLE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'TableName' ORDER BY ORDINAL_POSITION 

If this query returns null results, can I declare that the table does not exist? Or is it somehow possible that the table exists, but (perversely) has no columns?

I am already querying INFORMATION_SCHEMA.TABLES to find out if a table exists, but I would like to reduce it to one query if possible.

For future reference, I found the following questions:
Create table without columns
Can I select 0 columns in SQL Server?

+6
sql sql-server
source share
2 answers

If you try:

 create table TestTable (id int) alter table TestTable drop column id 

SQL Server complains that:

 Msg 4923, Level 16, State 1, Line 1 ALTER TABLE DROP COLUMN failed because 'id' is the only data column in table 'TestTable'. A table must have at least one data column. 

Thus, the table must have at least one column.

+5
source share

If you look at the definition of the INFORMATION_SCHEMA.COLUMNS type, you will see that it starts with the sys.objects table, looking for the types ā€œUā€ (user-defined table) or ā€œVā€, (view), so it already checks for the existence of the table for you.

 CREATE VIEW INFORMATION_SCHEMA.COLUMNS AS SELECT ... FROM sys.objects o JOIN sys.columns c ON c.object_id = o.object_id LEFT JOIN sys.types t ON c.user_type_id = t.user_type_id WHERE o.type IN ('U', 'V') 
+1
source share

All Articles