Last active
December 28, 2021 10:16
-
-
Save mrkhedri/0316d4b6818142ca4b779c995131327e to your computer and use it in GitHub Desktop.
create_description_for_table_and_columns_sql_server_query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_guery(is_add, db, schema, table, columns): | |
| query = f"USE {db}" | |
| func_type = "add" if is_add == True else "update" | |
| # Table description | |
| query += f"\n\nEXEC sp_{func_type}extendedproperty" | |
| query += f"\n @name = N'MS_Description', @value = '{table[1]}'," | |
| query += f"\n @level0type = N'Schema', @level0name = '{schema}'," | |
| query += f"\n @level1type = N'Table', @level1name = '{table[0]}';" | |
| query += f"\nGO" | |
| # Table Columns description | |
| for col in columns: | |
| query += f"\n\nEXEC sp_{func_type}extendedproperty" | |
| query += f"\n @name = N'MS_Description', @value = '{col[1]}'," | |
| query += f"\n @level0type = N'Schema', @level0name = '{schema}'," | |
| query += f"\n @level1type = N'Table', @level1name = '{table[0]}'," | |
| query += f"\n @level2type = N'Column', @level2name = '{col[0]}';" | |
| query += f"\nGO" | |
| return query | |
| is_add = True | |
| db = 'db_name' | |
| schema = 'schema_name' | |
| table = ['table_name', 'table_description'] | |
| columns = [ | |
| ['column_name_1', 'column_description_1'], | |
| ['column_name_2', 'column_description_2'], | |
| ['column_name_3', 'column_description_3'], | |
| ['column_name_4', 'column_description_4'], | |
| ['column_name_5', 'column_description_5'], | |
| ['column_name_6', 'column_description_6'], | |
| ['column_name_7', 'column_description_7'] | |
| ] | |
| query = get_guery(is_add, db, schema, table, columns) | |
| with open(f"{db}_{schema}_{table[0]}.sql", "w", encoding="utf-8") as o: | |
| o.write(query) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment