基于视图更新两个 SQL 表列 (SQL Server 2000)

发布于 2024-10-24 03:39:26 字数 685 浏览 2 评论 0原文

我正在努力解决一个 SQL 问题。我们使用企业管理器与 SQL Server 2000 进行通信。

背景:

我有一个表(表 A),其中包含销售人员编号(slspsn_no)以及区域和销售信息...由于销售人员编号和区域有多个记录,我创建了一个对销售人员编号和区域进行分组的视图(视图 A)。

现在,我创建了一个单独的表(表 B),其中包含基于该视图的两个类似的列以及更多列。

列:slspsn_no、region、January_sales_goals、二月_sales_goals、March_sales_goals 等。

这些每月销售目标将由人员通过访问权限进行管理。唯一的问题是,这目前效果很好,但表 A 是 slspsn_no 和区域经常更新的表,因此是我的观点。

问题:

有人可以帮助我使用一个 SQL 命令来根据视图 A 更新表 B 中的这两列吗?面临的挑战是在不将空值放入已输入的每月销售目标中的情况下执行此操作。如果该销售人员/区域组合不再位于视图 A 中,它只会删除表 B 中的任何行,或者如果新的销售人员/已在视图 A 中创建区域,每个月度销售目标都为空或零,稍后可以通过访问进行更改。

如果有人有一个想法,可以在创建语法时将 slspsn_no 和表 A 中的区域分组,而不需要视图,我很乐意尝试。

非常感谢!, -D

I'm struggling with a SQL question.. We use enterprise manager to communicate with SQL Server 2000.

Background:

I have a table (Table A) that houses salesperson number(slspsn_no) and region and sales info... since there is more than one record for salesperson number and region, i created a view (View A) that groups salesperson number and region.

Now I created a separate table (Table B) that contains two similar columns based off that view in addition to many more.

Columns: slspsn_no, region, January_sales_goals, February_sales_goals, March_sales_goals, etc.

Those monthly sales goals will be managed by a person through access. The only problem is this works great for now, but table A is the one that has slspsn_no and region updated frequently and hence my view.

Question:

Can somebody help me with a SQL command that will update those two columns in Table B based on View A? The challenge is to do this without placing nulls in the monthly sales goals already input.. it would just delete any rows in table B if that salesperson/region combo is no longer in View A, or add an additional row if a new salesperson/region has been created in View A with nulls or zero's for each of the monthly sales goals that can be changed through access later.

If somebody has an idea that would group slspsn_no and region from table A without the need for a view while creating the syntax, I'd be happy to try that to.

Thanks so much!,
-D

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

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

发布评论

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

评论(2

迷爱 2024-10-31 03:39:26

您需要的不是一个单独的表,而是一个物化视图

在 SqlServer 中,您可以通过在视图上放置索引来创建物化视图,该视图包含您希望“物化”的列。

SqlServer 自动保持其最新状态。

What you need is not a separate table but a materialised view.

In SqlServer you create a materialised view by putting an index on a view, which contains the columns you wish to "materialise".

SqlServer keeps it up to date automatically.

不离久伴 2024-10-31 03:39:26

看看这个例子。

您应该能够修改它以满足您的特定需求。

set nocount on;

declare @A table(id int, region int, name varchar(90));
insert @A values(1,200,'bob');
insert @A values(2,300,'lee'); -- doesn't exist in @B, should be added
insert @A values(5,300,'mia');
insert @A values(19,300,'tex');
insert @A values(401,400,'gaga'); -- doesn't exist in @B, should be added

declare @B table(id int, region int, goal1 int, goal2 int);
insert @B Values(1,200,8,9);
insert @B Values(5,300,7,7);
insert @B Values(19,300,5,9);
insert @B Values(555,100,1,1);  -- doesn't exist in @A, should be deleted

select * from @B order by id, region

-- insert new @A records into @B
insert @B (id, region)
select id, region from @A where id not in (select id from @B)

-- delete records from @B where not found in @A
delete @B
where id not in (select id from @A)

select * from @B order by id, region

Have a look at this example.

You should be able to modify it to suit your specific needs.

set nocount on;

declare @A table(id int, region int, name varchar(90));
insert @A values(1,200,'bob');
insert @A values(2,300,'lee'); -- doesn't exist in @B, should be added
insert @A values(5,300,'mia');
insert @A values(19,300,'tex');
insert @A values(401,400,'gaga'); -- doesn't exist in @B, should be added

declare @B table(id int, region int, goal1 int, goal2 int);
insert @B Values(1,200,8,9);
insert @B Values(5,300,7,7);
insert @B Values(19,300,5,9);
insert @B Values(555,100,1,1);  -- doesn't exist in @A, should be deleted

select * from @B order by id, region

-- insert new @A records into @B
insert @B (id, region)
select id, region from @A where id not in (select id from @B)

-- delete records from @B where not found in @A
delete @B
where id not in (select id from @A)

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