Advertisement
PavloSerg

Untitled

Apr 10th, 2023
1,119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.25 KB | None | 0 0
  1. USE [eda7k_localDB]
  2.  
  3.  
  4. --Получить все колонки таблицы
  5. SELECT *
  6. FROM eda7k_localDB.INFORMATION_SCHEMA.COLUMNS
  7. where TABLE_NAME = N'users'
  8. --Получить все колонки таблицы
  9. SELECT *
  10. FROM sys.columns
  11. WHERE object_id = OBJECT_ID('SeregaTheDed_SQLLogin_1.users');
  12.  
  13.  
  14. --Получить все автовычисляемые колонки
  15. select COLUMN_NAME, TABLE_NAME
  16. from INFORMATION_SCHEMA.COLUMNS
  17. where COLUMNPROPERTY(object_id(TABLE_SCHEMA+'.'+TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1
  18. order by TABLE_NAME
  19.  
  20.  
  21. --Получить как вычисляется вычисляемая колонка
  22. SELECT definition
  23. FROM sys.computed_columns
  24. WHERE object_id = object_id(N'SeregaTheDed_SQLLogin_1.users')
  25.  
  26.  
  27.  
  28. --Проставить всем дефолтные значения
  29. SELECT
  30.     'ALTER TABLE '+ SCHEMA_NAME(schema_id) + '.' + OBJECT_NAME(parent_object_id) +
  31.     ' ADD CONSTRAINT ' + dc.name + ' DEFAULT(' + definition
  32.     + ') FOR ' + c.name
  33. FROM sys.default_constraints dc
  34. INNER JOIN sys.columns c ON dc.parent_object_id = c.object_id AND dc.parent_column_id = c.column_id
  35.  
  36.  
  37.  
  38. --получить все внешние ключи
  39. SELECT  obj.name AS FK_NAME,
  40.     sch.name AS [schema_name],
  41.     tab1.name AS [table],
  42.     col1.name AS [column],
  43.     tab2.name AS [referenced_table],
  44.     col2.name AS [referenced_column]
  45. FROM sys.foreign_key_columns fkc
  46. INNER JOIN sys.objects obj
  47.     ON obj.object_id = fkc.constraint_object_id
  48. INNER JOIN sys.tables tab1
  49.     ON tab1.object_id = fkc.parent_object_id
  50. INNER JOIN sys.schemas sch
  51.     ON tab1.schema_id = sch.schema_id
  52. INNER JOIN sys.columns col1
  53.     ON col1.column_id = parent_column_id AND col1.object_id = tab1.object_id
  54. INNER JOIN sys.tables tab2
  55.     ON tab2.object_id = fkc.referenced_object_id
  56. INNER JOIN sys.columns col2
  57.     ON col2.column_id = referenced_column_id AND col2.object_id = tab2.object_id
  58.  
  59.  
  60. --Пример скрипата
  61. --alter table SeregaTheDed_SQLLogin_1.products
  62.     --add constraint products_products_price_fk
  63.         --foreign key (price) references SeregaTheDed_SQLLogin_1.products (price)
  64. --go
  65.  
  66.  
  67. --EXEC sp_fkeys @pktable_name = 'rel_orders_products', @pktable_owner = 'SeregaTheDed_SQLLogin_1'--Не робит
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement