Skip to content

Instantly share code, notes, and snippets.

@allansli
Last active August 31, 2018 13:44
Show Gist options
  • Save allansli/f53a50d4fb8a07d6da8c716455fec1b6 to your computer and use it in GitHub Desktop.
Save allansli/f53a50d4fb8a07d6da8c716455fec1b6 to your computer and use it in GitHub Desktop.
SELECT '<TABELAS>' +
(SELECT
TABELA.NAME AS 'NOME'
, COLUNA.NAME AS 'NOME'
, CASE WHEN COLUNA.PRECISION > 0 THEN COLUNA.PRECISION ELSE COLUNA.MAX_LENGTH END AS 'TAMANHO'
, COLUNA.scale AS 'ESCALA'
, CASE COLUNA.IS_NULLABLE WHEN 1 THEN 'N' ELSE 'S' END AS 'OBRIGATORIO'
, CASE WHEN (SELECT COUNT(*)
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS C
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS K
ON C.TABLE_NAME = K.TABLE_NAME
AND C.CONSTRAINT_CATALOG = K.CONSTRAINT_CATALOG
AND C.CONSTRAINT_SCHEMA = K.CONSTRAINT_SCHEMA
AND C.CONSTRAINT_NAME = K.CONSTRAINT_NAME
WHERE C.CONSTRAINT_TYPE = 'PRIMARY KEY'
AND K.COLUMN_NAME = COLUNA.name
AND C.TABLE_NAME = TABELA.name)
> 0 THEN 'S' ELSE 'N' END AS 'CHAVE_PRIMARIA'
, CASE WHEN COLUNA.IS_IDENTITY = 1 THEN 'N' ELSE 'S' END AS 'EDITAVEL'
, '' AS 'VALOR'
, TIPO.NAME AS 'TIPO'
FROM SYS.OBJECTS AS TABELA WITH (NOLOCK)
JOIN SYS.COLUMNS AS COLUNA WITH (NOLOCK)
ON TABELA.OBJECT_ID=COLUNA.OBJECT_ID
JOIN SYS.TYPES AS TIPO WITH (NOLOCK)
ON COLUNA.SYSTEM_TYPE_ID=TIPO.SYSTEM_TYPE_ID
WHERE TABELA.TYPE_DESC='USER_TABLE'
ORDER BY TABELA.NAME
FOR XML AUTO) + '</TABELAS>' AS XML;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment