在 Visual Studio 2010 数据库项目中生成种子数据

发布于 2024-10-28 22:05:47 字数 229 浏览 2 评论 0原文

我在 Visual studio 2010 中有一个数据库和一个数据库项目。我成功推断了数据库项目中的架构,但我还需要以某种方式导入一些表(国家、州、用户类型等)中的数据,这些表是引用表,而不是真正的数据表。

有办法吗?

到目前为止,我发现的唯一方法是在 SQL Server Management Studio 中生成数据脚本,并将该脚本放置在数据库项目的部署后脚本文件中。

有更简单的办法吗?

I have a database and a database project in Visual studio 2010. I inferred the schema in the database project successfully but I also need to import somehow the data in a few tables (Country, State, UserType etc.) that are reference tables and not really data tables.

Is there a way?

The only way I found so far is generating data scripts in SQL Server Management Studio and placing this script in the post-deployment script file in the database project.

Any simpler way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清泪尽 2024-11-04 22:05:47

我以前就是这么做的。

我只是确保脚本中的每个语句看起来像这样:

IF (EXISTS(SELECT * FROM Country WHERE CountryId = 1))
    UPDATE MyTable SET Name = 'UK' WHERE CountryId = 1 AND Name != 'UK'
ELSE
    INSERT INTO MyTable (CountryId, Name) VALUES (1, 'UK')

这意味着每次部署数据库时,您的核心参考数据都会被插入或更新(以最合适的为准),并且您可以修改这些脚本以创建新的参考数据对于较新版本的数据库。

您可以使用 T4 模板来生成这些脚本 - 我过去做过类似的事情。

this is pretty much how I've done it before.

I would just make sure that each statement in you script look something like:

IF (EXISTS(SELECT * FROM Country WHERE CountryId = 1))
    UPDATE MyTable SET Name = 'UK' WHERE CountryId = 1 AND Name != 'UK'
ELSE
    INSERT INTO MyTable (CountryId, Name) VALUES (1, 'UK')

This means that each time you deploy the database your core reference data will be inserted or updated, whichever is most appropriate, and you can modify these scripts to create new reference data for newer versions of the database.

You could use a T4 template to generate these scripts - I've done something similar in the past.

断念 2024-11-04 22:05:47

尝试使用 SQL Server 的静态数据脚本生成器。它以正确的格式自动为您执行这些部署后脚本。它是一个免费的 Google Code 托管项目,我们发现它对于编写静态数据脚本非常有用(也可以在现有数据之上进行更新)。

Try the Static Data Script Generator for SQL Server. It automates those Post-Deployment scripts for you in the correct format. It is a free Google Code hosted project and we have found it useful for scripting our static data (also over the top of existing data for updates as well).

向日葵 2024-11-04 22:05:47

您还可以使用 Merge INTO 语句将种子数据更新/删除/插入到部署后脚本中的表中。我已经尝试过了,它对我有用,这里是一个简单的示例:

*/ print 'Inserting seed data for seedingTable'

MERGE INTO seedingTable AS Target 
USING (VALUES   (1, N'Pakistan', N'Babar Azam', N'Asia',N'1'),
(2, N'England',  N'Nasir Hussain', N'Wales',N'2'), 
(3, N'Newzeland', N'Stepn Flemming', N'Australia',N'4'), 
(4, N'India', N'Virat Koli', N'Asia',N'3'),  
(5, N'Bangladash', N'Saeed', N'Asia',N'8'),   
(6, N'Srilanka', N'Sangakara', N'Asia',N'7') ) 
AS Source (Id, Cric_name,captain,region,[T20-Rank]) ON Target.Id = Source.Id

-- update matched rows 
   WHEN MATCHED THEN 
   UPDATE SET Cric_name = Source.Cric_name, Captain = Source.Captain, Region=source.Region, [T20-Rank]=source.[T20-Rank]

-- insert new rows 
   WHEN NOT MATCHED BY TARGET THEN 
   INSERT (Id, Cric_name,captain,region,[T20-Rank]) 
   VALUES (Id, Cric_name,captain,region,[T20-Rank])
-- delete rows that are in the target but not the source
   WHEN NOT MATCHED BY SOURCE THEN
  DELETE;

you can also use the Merge INTO statement for updating/deleting/inserting seed data into the table in your post deployment script.I have tried and its works for me here is the simple example:

*/ print 'Inserting seed data for seedingTable'

MERGE INTO seedingTable AS Target 
USING (VALUES   (1, N'Pakistan', N'Babar Azam', N'Asia',N'1'),
(2, N'England',  N'Nasir Hussain', N'Wales',N'2'), 
(3, N'Newzeland', N'Stepn Flemming', N'Australia',N'4'), 
(4, N'India', N'Virat Koli', N'Asia',N'3'),  
(5, N'Bangladash', N'Saeed', N'Asia',N'8'),   
(6, N'Srilanka', N'Sangakara', N'Asia',N'7') ) 
AS Source (Id, Cric_name,captain,region,[T20-Rank]) ON Target.Id = Source.Id

-- update matched rows 
   WHEN MATCHED THEN 
   UPDATE SET Cric_name = Source.Cric_name, Captain = Source.Captain, Region=source.Region, [T20-Rank]=source.[T20-Rank]

-- insert new rows 
   WHEN NOT MATCHED BY TARGET THEN 
   INSERT (Id, Cric_name,captain,region,[T20-Rank]) 
   VALUES (Id, Cric_name,captain,region,[T20-Rank])
-- delete rows that are in the target but not the source
   WHEN NOT MATCHED BY SOURCE THEN
  DELETE;
溺深海 2024-11-04 22:05:47

通过使用 postdeployment.script 并使用新表和种子数据完美执行,下面是脚本,之后我想添加新列并将数据插入其中,如何做到这一点

 insert into seedingTable (Id, Cric_name, captain, region,[T20-Rank])
 select 1, N'Pakistan', N'Babar Azam', N'Asia',N'1' 
 where not exists
 (select 1 from dbo.seedingTable where id=1)     
 go
 insert into seedingTable (Id, Cric_name, captain, region,[T20-Rank]) select 2,
 N'England', N'Nasir Hussain', N'Wales',N'3' 
 where not exists
 (select 1 from dbo.seedingTable where id=2)

让我知道上面的脚本每次都会运行使用azure pipeline部署数据库。如何更新数据。

By Using postdeployment.script and its executed perfectly with new table and seed data into it below is the script,after that I want to add new column and insert data into it how i i can do this

 insert into seedingTable (Id, Cric_name, captain, region,[T20-Rank])
 select 1, N'Pakistan', N'Babar Azam', N'Asia',N'1' 
 where not exists
 (select 1 from dbo.seedingTable where id=1)     
 go
 insert into seedingTable (Id, Cric_name, captain, region,[T20-Rank]) select 2,
 N'England', N'Nasir Hussain', N'Wales',N'3' 
 where not exists
 (select 1 from dbo.seedingTable where id=2)

Let me know above script will run every time when deploying database by using azure pipeline. how to update data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文