PostgreSQL ms_description等价?

时间:2015-10-06 18:25:01

标签: sql-server postgresql

我是PostgreSQL的新手。我正在开发一个将MSSQL数据库转换为PostgreSQL的项目。现在,我们使用扩展属性(MS_Description)记录所有表和列。我希望能够保留所有这些描述,但似乎无法弄清楚是否有办法将这些转换为PostgreSQL。关于我怎么做的任何想法?我总是可以将信息转储到数据字典表中,但不希望信息与实际的表/列分开。

2 个答案:

答案 0 :(得分:2)

使用COMMENT命令,例如:

comment on table test is 'My favotite table';
comment on column test.id is 'Primary key of my favorite table';

要在SQL中检索这些注释,请使用这些函数(请参阅Table 9-60. Comment Information Functions):

select obj_description('test'::regclass, 'pg_class');
  obj_description  
-------------------
 My favotite table
(1 row)

select col_description('test'::regclass, 1);
         col_description          
----------------------------------
 Primary key of my favorite table
(1 row)

答案 1 :(得分:1)

对于其他需要这样做的人来说,这是我根据@klin的答案编写的脚本,以便从MS SQL获取描述并创建所需的PostgreSQL命令。第一个选择构建表描述,第二个构建列描述:

SELECT N'comment on table '+ Lower(o1.[name]) 
    + N' is ''' + CAST(s1.value AS NVARCHAR(4000)) + ''';'
FROM sys.objects o1 
    INNER JOIN sys.extended_properties s1 on o1.object_id = s1.major_id
WHERE o1.type = 'U'
    AND s1.name = 'MS_Description'      
UNION
SELECT N'comment on column '+ Lower(o2.[name]) + '.' + Lower(c.[name]) 
    + N' is ''' + CAST(s2.value AS NVARCHAR(4000)) + ''';' 
FROM sys.objects o2 
    INNER JOIN sys.columns c ON o2.object_id = c.object_id 
    INNER JOIN sys.extended_properties s2 on o2.object_id = s2.major_id
WHERE o2.type = 'U'
    AND c.column_id = s2.minor_id
    AND s2.name = 'MS_Description';