基于视图更新两个 SQL 表列 (SQL Server 2000)
我正在努力解决一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要的不是一个单独的表,而是一个物化视图。
在 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.
看看这个例子。
您应该能够修改它以满足您的特定需求。
Have a look at this example.
You should be able to modify it to suit your specific needs.