0
I need a TSQL bring me some table information data as which column is IS_NULLABLE
, COLUMN_NAME
, and so on, but I came across a problem, I can’t bring a column in a CASE WHEN
which says that such column is PK, which is FK, if it has, and if the column has no key CASE WHEN should bring as NULL in select.
I have this select working on Mysql already, but can’t play or better mount it on 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