EDIT: I tried this code, part of what a user mentioned, and it returns the fields correctly.
SELECT * FROM INFORMATION_SCHEMA.TABLES
JOIN INFORMATION_SCHEMA.COLUMNS on
INFORMATION_SCHEMA.TABLES.TABLE_NAME =
INFORMATION_SCHEMA.COLUMNS.TABLE_NAME
WHERE INFORMATION_SCHEMA.TABLES.TABLE_NAME = 'myTable'
There is something in the former query that works with some databases but not with others
EDIT2:
I've tried setting up a Profiler trace to check the columns creation through the wizard, as a user suggested. It is using the same schema, apparently.
ALTER TABLE dbo.table1 ADD
field2 nchar(10) NULL
I'm working with SQL Server 2014, and created databases by using the wizard, I mean, not by code, but by using the UI.
Then, I tried to fetch metadata information as regards their tables' columns.. and no results are returned. I query INFORMATION_SCHEMA.
I've tried using the same query for databases I've created by code, and it works fine, it returns the columns and their values, etc.
I've tried looking it up over the internet, but can't seem to think anything that fits cases like these. I think it's a bit weird that it makes a difference with columns created by code and through the UI.. If anyone knows why something like this can happen, or seems familiar, I would appreciate some light on it :)
Here's the code I use to retrieve the columns' metadata:
USE 'database'
SELECT infSch.TABLE_CATALOG,
infSch.TABLE_NAME,
sysCols.name,
infSch.ORDINAL_POSITION,
sysCols.is_nullable,
infSch.DATA_TYPE,
infSch.CHARACTER_MAXIMUM_LENGTH,
sysCols.is_identity,
IIF(infSchCons.CONSTRAINT_TYPE = 'PRIMARY KEY', 1, 0),
IIF(infSchCons.CONSTRAINT_TYPE = 'FOREIGN KEY', 1, 0),
IIF(infSchCons.CONSTRAINT_TYPE = 'UNIQUE', 1, 0),
IIF(infSchCons.CONSTRAINT_TYPE = 'CHECK', 1, 0)
FROM sys.columns as sysCols
RIGHT JOIN INFORMATION_SCHEMA.COLUMNS as infSch on sysCols.name =
infSch.COLUMN_NAME
RIGHT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS as infSchCons on
infSch.TABLE_NAME = infSchCons.TABLE_NAME
Results:
If table and columns were created by code --> it returns all the columns and its metadata, for a given database
If table and columns were created by using the UI and windows --> it returns nothing, just an empty set of results