首頁 Sql Table Schema 產生器
文章
Cancel

Sql Table Schema 產生器

前言

因為工作上會需要打Table Schema

碰到有很多資料庫且裡面很多資料表的情況,一想到還要一個一個進去裡面看型態欄位就覺得很麻煩

因為在這之前,也已經有在blog其他篇文章中,留下C# Dapper Connect SQL的操作方式、以及使用Excel迅速建檔的方式

所以想到可以乾脆的趁這時利用這兩個素材解決我需求

關鍵Query

關鍵的Query如下,整隻程式都是基於這個Query產生的資料去做延伸的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 with main as
(
SELECT
c.name 'Column Name',
t.Name 'Column Type',
c.max_length 'Max Length',
case
when c.is_nullable = 0 then 'Not Null'
when c.is_nullable = 1 then 'Is Null'
End as 'IsNull',
ISNULL(i.is_primary_key, 0) 'IsPrimaryKey',
c.object_id 'object_id'
FROM
sys.columns c
INNER JOIN
sys.types t ON c.user_type_id = t.user_type_id
LEFT OUTER JOIN
sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id
LEFT OUTER JOIN
sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id
)
SELECT
s.name AS SchemaName,
t.name AS TableName,
main.[Column Name] as N'ColumnName',
main.[Column Type] as N'ColumnType',
main.[Max Length] as N'MaxLength',
main.[IsNull],
main.[IsPrimaryKey],
sys.extended_properties.value as N'ColumnDescription'
FROM sys.tables t
INNER JOIN sys.schemas s
ON t.schema_id = s.schema_id
left join main on main.object_id = OBJECT_ID(t.name )
left join sys.extended_properties on sys.extended_properties.major_id = main.object_id and sys.extended_properties.minor_id = columnproperty(main.object_id, main.[Column Name], 'ColumnId') and sys.extended_properties.name = 'MS_Description'

操作介紹

執行畫面

Desktop View

基本輸入

選擇「基本輸入」

Desktop View

「基本輸入」所對應的資料

Desktop View

匯出

Desktop View

最終結果

Desktop View

輸入ConnectString

Desktop View

匯出結果同上

匯入範本

先下載範本

Desktop View Desktop View Desktop View Desktop View Desktop View Desktop View

匯出結果同上

操作影片

執行檔下載點(Google Drive)

備註:執行檔自行斟酌下載,又或者從GitHub Code Review之後再自行使用

https://drive.google.com/drive/folders/1QwKbm9ftEL8F12Y-m0e5Y9uatimTam_p?usp=share_link

GitHub

https://github.com/digamana/AutoExportTableSchemaRepo.git

本文由作者按照 CC BY 4.0 進行授權