sqlserver表和字段增加或更新注释

分手后的思念是犯贱 2021-09-24 12:24 616阅读 0赞

针对 SpringBoot JPA + sqlserver 这种生成的表没有注释的情况 ,通过代码更新注释方法整理如下:

  1. // SpringBoot JPA 可以直接引入 jdbcTemplate Bean
  2. @Autowired
  3. private JdbcTemplate jdbcTemplate;
  4. private void setTableComment(String tableName, String tableComment) {
  5. // 1表名称 2字段名称
  6. String checkTableCommentExistsSql = "SELECT COUNT(*) FROM ::fn_listextendedproperty ('MS_Description','SCHEMA','dbo','TABLE',?,NULL,NULL)";
  7. // 1注释 2表名称 3字段名称
  8. String updateTableComment = " execute sp_updateextendedproperty 'MS_Description',?,'SCHEMA','dbo','TABLE',?";
  9. // 1注释 2表名称 3字段名称
  10. String createTableComment = " execute sp_addextendedproperty 'MS_Description',?,'SCHEMA','dbo','TABLE',?";
  11. Integer count = jdbcTemplate.queryForObject(checkTableCommentExistsSql, Integer.class, tableName);
  12. if (count == null || count.equals(0)) {
  13. jdbcTemplate.update(createTableComment, tableComment, tableName);
  14. } else {
  15. jdbcTemplate.update(updateTableComment, tableComment, tableName);
  16. }
  17. }
  18. private void setFildComment(String tableName, String fildName, String fildComment) {
  19. // 1表名称 2字段名称
  20. String checkFildCommentExistsSql = "SELECT COUNT(*) FROM ::fn_listextendedproperty ('MS_Description','SCHEMA','dbo','TABLE',?,'COLUMN',?)";
  21. // 1注释 2表名称 3字段名称
  22. String updateFildComment = " execute sp_updateextendedproperty 'MS_Description',?,'SCHEMA','dbo','TABLE',?,'COLUMN',? ";
  23. // 1注释 2表名称 3字段名称
  24. String createFildComment = " execute sp_addextendedproperty 'MS_Description',?,'SCHEMA','dbo','TABLE',?,'COLUMN',? ";
  25. Integer count = jdbcTemplate.queryForObject(checkFildCommentExistsSql, Integer.class, tableName, fildName);
  26. if (count == null || count.equals(0)) {
  27. jdbcTemplate.update(createFildComment, fildComment, tableName, fildName);
  28. } else {
  29. jdbcTemplate.update(updateFildComment, fildComment, tableName, fildName);
  30. }
  31. }

发表评论

表情:
评论列表 (有 0 条评论,616人围观)

还没有评论,来说两句吧...

相关阅读

    相关 fastadmin 的注释字段注释

    在安装好 fastdamin 之后,看官方的文档,说是创建表的时候,要写表注释和字段注释; 不了解数据库,只知道简单的表和字段的概念,这个注释是什么还真的不了解,于是,学习一

    相关 mysql查看字段注释

    说明 在mysql中,information_schema这个数据库中保存了mysql服务器所有数据库的信息。 包括数据库名,数据库的表,表字段的数据类型等。 也就是说,...