简体   繁体   English

有什么方法可以在SSDT数据库项目中引用CDC对象?

[英]Is there any way to reference CDC objects in a SSDT database project?

I'm trying to create a database project from an extant database. 我正在尝试从现有数据库创建数据库项目。 Some of the tables in the database have CDC enabled. 数据库中的某些表已启用CDC。 For each such table, we've created a view into the CDC data that looks something like: 对于每个这样的表,我们都创建了一个CDC数据视图,如下所示:

create view [dbo].[vw_cdc_myTable] as
select 
   sys.fn_cdc_map_lsn_to_time (__$start_lsn) ActionLocalTime
   ,    __$seqval ActionOrder
   ,    __$operation ImageType
   ,    case __$operation
            when 1 then 'Deleted'
            when 2 then 'Inserted'
            when 3 then 'Before Update'
            when 4 then 'After Update'
        end ImageType_desc
   ,    convert(char(5), '>>>>>') Sprtr
   ,    *
from 
    cdc.dbo_myTable_CT

SSDT complains about this as it didn't import any of the CDC objects (in this case, cdc.dbo_myTable_CT and sys.fn_cdc_map_lsn_to_time). SSDT抱怨这一点,因为它没有导入任何CDC对象(在本例中为cdc.dbo_myTable_CT和sys.fn_cdc_map_lsn_to_time)。 Is there any way to either have those imported or to fake SSDT out so that I can have the view in source control? 有什么方法可以导入这些文件或伪造SSDT,以便可以将视图包含在源代码管理中?

Yes, there's a way to do it. 是的,有一种方法可以做到。 It's called cheating, but it works wonders. 这被称为作弊,但行之有效。

Follow these steps: 跟着这些步骤:

  1. Create a stub SSDT project in your solution and call it CDC (or whatever placeholder name you want) 在您的解决方案中创建一个存根SSDT项目,并将其命名为CDC(或所需的任何占位符名称)
  2. Add a schema object to that fake project, named [cdc] 向该假项目添加一个名为[cdc]的架构对象
  3. Script out the CREATE of your dbo_myTable_CT and add it as a table object to your fake project 编写dbo_myTable_CT的CREATE脚本,并将其作为表对象添加到假项目中
  4. Now the fun begins: Add that fake a project as a database reference to your main project, and name the SQLCMD variable for the reference as CDC (for example) 现在开始有趣了:将伪造的项目添加为对主项目的数据库引用,并将该引用的SQLCMD变量命名为CDC(例如)
  5. Change the from clause in your view to look like this: 将视图中的from子句更改为如下所示:
    \n\n    from [$(CDC)].cdc.dbo_myTable_CT 来自[$(CDC)]。cdc.dbo_myTable_CT\n\n
  6. Now, when you are deploying your database, when asked for the actual name for the CDC database, just use the same database name in your own project. 现在,当您部署数据库时,当询问CDC数据库的实际名称时,只需在您自己的项目中使用相同的数据库名称即可。 They are the same database, technically. 从技术上讲,它们是相同的数据库。 You main code will be deployed, while the cdc table in the target won't be touched. 您的主要代码将被部署,而目标中的cdc表将不会被触摸。

That's it! 而已!

You can also use this trick to make deployment ignore publishing of certain objects altogether while still husing them in some way. 您还可以使用此技巧使部署完全忽略某些对象的发布,同时仍以某种方式对其进行合并。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SSDT-数据库参考 - SSDT - Database Reference 对系统数据库的引用在 SSDT 数据库项目中重复 - Reference to system databases becomes duplicated in SSDT database project SSDT循环参考:复杂项目 - SSDT Circular reference: Complex project 使用SSDT的数据项目,如何引用另一个数据库而不将其填充到SS Obj Explorer中的同一项目文件夹中 - Data Project using SSDT, how to reference another database without it populating in same project folder in SS Obj Explorer 迁移ssdt项目从visual studio 2012到2017 cdc source not opening - migrating ssdt project from visual studio 2012 to 2017 cdc source not opening 如何从现有数据库创建SSDT项目? - How to create SSDT project from existing database? SSDT 2013 新数据库项目缺失 - SSDT 2013 new database Project missing 从SSDT项目部署SQL数据库更改 - Deploying SQL Database changes from a SSDT project 如何在SSDT项目中包括自定义数据迁移和静态/参考数据? - How to include custom data migrations and static/reference data in an SSDT project? 在PostDeploy中unicode上的SSDT项目发布失败 - 任何解决此问题的解决方法或配置? - SSDT Project Publish fails on unicode in PostDeploy - any workarounds or configurations to fix this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM