I need a TSQL that brings me some table information data like which column is IS_NULLABLE
, COLUMN_NAME
, and so on, but I came across a problem, I can not bring a column in a CASE WHEN
which says that such a column is PK, which is FK if it has, and if the column has no key the CASE WHEN should bring as NULL in the select.
I have this select working in MySQL already, but I can not play it or better mount it in SQL Server.
Attempt on SQL Server:
SELECT
UPPER(AA.COLUMN_NAME) COLUMN_NAME,
LOWER(AA.IS_NULLABLE) IS_NULLABLE,
LOWER(AA.DATA_TYPE) DATA_TYPE,
LOWER(AA.CHARACTER_MAXIMUM_LENGTH) MAXIMUM_LENGTH
-- SERIA AQUI O 'CASE WHEN' IGUAL AO MYSQL
FROM
INFORMATION_SCHEMA.COLUMNS AA
WHERE
AA.TABLE_NAME = '<TABLE_NAME>'
ORDER BY
ORDINAL_POSITION ASC
Functional version in MySQL:
SELECT
UPPER(COLUMN_NAME) COLUMN_NAME,
LOWER(IS_NULLABLE) IS_NULLABLE,
LOWER(DATA_TYPE) DATA_TYPE,
LOWER(CHARACTER_MAXIMUM_LENGTH) MAXIMUM_LENGTH,
CASE" +
WHEN LOWER(COLUMN_KEY) = 'pri' THEN 'pk'
WHEN LOWER(COLUMN_KEY) = 'mul' THEN 'fk'
WHEN LOWER(COLUMN_KEY) = NULL THEN NULL
END AS COLUMN_KEY
FROM
INFORMATION_SCHEMA.COLUMNS A
WHERE
A.TABLE_SCHEMA = '<MEU_SCHEMA>' AND A.TABLE_NAME = '<TABLE_NAME>'
ORDER BY
A.ORDINAL_POSITION ASC