首页 > 解决方案 > 获取一个表中的数据并使用它在 SQL SERVER 中的另一个表中获取更多数据?

问题描述

我在 SQL SERVER 中有两个表,它们是 1) tblBuyerCodes2) tblStatistics

我想在 SQL Server 中实现的是在第一个表中获取一个buyer code,在该买方代码的第二个表中获取统计信息并在第三个表中插入详细信息,我们称之为tblResults。如何在 SQL Server 存储过程中实现这一点?

眼镜:

tblBuyerCodes have columns (id, buyercode). 
tblStatistics have columns (id,buyercode,goodsbought,total). 
tblResults have columns (id, buyercode,goodsbought,total,date). 

我想要实现的只是在 tblStatistics 中获取结果,然后根据从 tblBUyerCodes 表中获取的买家代码将它们发布到 tblResults 中

For example results will be:
row 1: dm6 23 45689
row 2: dm8 45 3456

dm6dm8成为买方代码

标签: sql-servervba

解决方案


基于我们所拥有的有限细节,为什么不只是?

INSERT INTO tblResult (id, buyercode,goodsbought,total,date)
SELECT id,
       buyercode,
       goodsbought,
       total,
       {SomeDateValue} --Maybe GETDATE()?
FROM tblStatistics;

推荐阅读