Advertisement
Guest User

Untitled

a guest
Jan 24th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. declare @table_name varchar(255) = 'tc_trip_zakaz'
  2. select
  3. t_c.COLUMN_NAME as name
  4. , t_c.DATA_TYPE as type
  5. , isnull(t_c.CHARACTER_MAXIMUM_LENGTH, 0) as max_length
  6. , case when lower(t_c.IS_NULLABLE) = 'yes' then 1 else 0 end as is_nullable
  7. , isnull(s.is_unique, 0) as is_unique
  8. , isnull(s.is_foreign, 0) as is_foreign
  9. , isnull(s.is_primary, 0) as is_primary
  10. from INFORMATION_SCHEMA.COLUMNS as t_c
  11. left join INFORMATION_SCHEMA.KEY_COLUMN_USAGE as all_keys
  12. on all_keys.TABLE_NAME = t_c.TABLE_NAME
  13. and t_c.COLUMN_NAME = all_keys.COLUMN_NAME
  14. left join (
  15. select
  16. CONSTRAINT_NAME as keys
  17. , case when CONSTRAINT_TYPE = 'UNIQUE' then 1 else 0 end as is_unique
  18. , case when CONSTRAINT_TYPE = 'FOREIGN KEY' then 1 else 0 end as is_foreign
  19. , case when CONSTRAINT_TYPE = 'PRIMARY KEY' then 1 else 0 end as is_primary
  20. from
  21. INFORMATION_SCHEMA.TABLE_CONSTRAINTS as f_key where TABLE_NAME = @table_name
  22.  
  23. ) as s ON s.keys = all_keys.CONSTRAINT_NAME
  24.  
  25. where t_c.TABLE_NAME = @table_name
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement