問題描述
使用Power Designer(PD)產生的SQL指令碼建立SQL Server database後,表和欄位的Description都變成了亂碼,如????,Google了一些相關文章後,沒有很好的解決方案,要麼是對MySql的,要麼是對Oracle的,沒有發現SQL Server 的。
解決方案
開始以為是PD產生的指令檔編碼不對,修改編碼為簡體中文GB2312也不行;修改資料庫的Collation為Chinese-PRC-CI-AS也不行。後來直接在SQL Server建測試表,添加註釋,匯出指令碼,分別如下所示:
PD產生的注釋:
execute sp_addextendedproperty 'MS_Description',
'附件',
'user', 'dbo', 'table', 'WF_Attchment'
go
execute sp_addextendedproperty 'MS_Description',
'附件ID',
'user', 'dbo', 'table', 'WF_Attchment', 'column', 'AttchmentID'
Go
SQL Server產生的注釋:
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'附件ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'WF_Attchment', @level2type=N'COLUMN',@level2name=N'AttchmentID'
GO
把PD產生的指令碼同SQL Server匯出的指令碼比較後,發現在賦值給注釋的Value上了一個N。找到了問題的根源,這下就好辦了,我們需要在PD裡進行處理。
1. 修改資料庫模型的語言屬性,位置在PD|Database|Gernate Datebase|Format,修改語言為UTF-8,如所示:
2. 修改產生的指令碼的注釋,修改的地方在:
tools-->resources-->dbms-->sql server 2008開啟DBMS屬性視窗,在general選項卡中選擇Microsoft SQL Server 2008-->script-->objects,可以修改Objects下的Table注釋-表注釋和Column注釋-欄位注釋,
/表注釋/
/預設表注釋/
[%OWNER%?[.O:[execute ][exec ]]sp_addextendedproperty [%R%?[N]]'MS_Description',
[%R%?[N]]%.q:COMMENT%,
[%R%?[N]]'user', [%R%?[N]]%.q:OWNER%, [%R%?[N]]'table', [%R%?[N]]%.q:TABLE%
:declare @CurrentUser sysname
select @CurrentUser = user_name()
[.O:[execute ][exec ]]sp_addextendedproperty [%R%?[N]]'MS_Description',
[%R%?[N]]%.q:COMMENT%,
[%R%?[N]]'user', [%R%?[N]]@CurrentUser, [%R%?[N]]'table', [%R%?[N]]%.q:TABLE%
]
/修改後的表注釋/
[%OWNER%?[.O:[execute ][exec ]]sp_addextendedproperty [%R%?[N]]'MS_Description',
[[N]]%.q:COMMENT%,
[%R%?[N]]'user', [%R%?[N]]%.q:OWNER%, [%R%?[N]]'table', [%R%?[N]]%.q:TABLE%
:declare @CurrentUser sysname
select @CurrentUser = user_name()
[.O:[execute ][exec ]]sp_addextendedproperty [%R%?[N]]'MS_Description',
[[N]]%.q:COMMENT%,
[%R%?[N]]'user', [%R%?[N]]@CurrentUser, [%R%?[N]]'table', [%R%?[N]]%.q:TABLE%
]
////////////////////////
/欄位注釋/
/預設欄位注釋/
[%OWNER%?[.O:[execute ][exec ]]sp_addextendedproperty [%R%?[N]]'MS_Description',
[%R%?[N]]%.q:COMMENT%,
[%R%?[N]]'user', [%R%?[N]]%.q:OWNER%, [%R%?[N]]'table', [%R%?[N]]%.q:TABLE%, [%R%?[N]]'column', [%R%?[N]]%.q:COLUMN%
:declare @CurrentUser sysname
select @CurrentUser = user_name()
[.O:[execute ][exec ]]sp_addextendedproperty [%R%?[N]]'MS_Description',
[%R%?[N]]%.q:COMMENT%,
[%R%?[N]]'user', [%R%?[N]]@CurrentUser, [%R%?[N]]'table', [%R%?[N]]%.q:TABLE%, [%R%?[N]]'column', [%R%?[N]]%.q:COLUMN%
]
/修改後的欄位注釋/
[%OWNER%?[.O:[execute ][exec ]]sp_addextendedproperty [%R%?[N]]'MS_Description',
[[N]]%.q:COMMENT%,
[%R%?[N]]'user', [%R%?[N]]%.q:OWNER%, [%R%?[N]]'table', [%R%?[N]]%.q:TABLE%, [%R%?[N]]'column', [%R%?[N]]%.q:COLUMN%
:declare @CurrentUser sysname
select @CurrentUser = user_name()
[.O:[execute ][exec ]]sp_addextendedproperty [%R%?[N]]'MS_Description',
[[N]]%.q:COMMENT%,
[%R%?[N]]'user', [%R%?[N]]@CurrentUser, [%R%?[N]]'table', [%R%?[N]]%.q:TABLE%, [%R%?[N]]'column', [%R%?[N]]%.q:COLUMN%
]
修改欄位注釋還有一種方法,如下所示:
/修改後的欄位注釋/
[%OWNER%?[.O:[execute ][exec ]]sp_addextendedproperty [%R%?[N]]'MS_Description',
[[N]]%.q:COMMENT%,
[%R%?[N]]'schema', [%R%?[N]]%.q:OWNER%, [%R%?[N]]'table', [%R%?[N]]%.q:TABLE%, [%R%?[N]]'column', [%R%?[N]]%.q:COLUMN%
:declare @CurrentUser sysname
select @CurrentUser = user_name()
[.O:[execute ][exec ]]sp_addextendedproperty [%R%?[N]]'MS_Description',
[[N]]%.q:COMMENT%,
[%R%?[N]]'schema', [%R%?[N]]@CurrentUser, [%R%?[N]]'table', [%R%?[N]]%.q:TABLE%, [%R%?[N]]'column', [%R%?[N]]%.q:COLUMN%
]