在SQL SERVER中获取一个表中的数据并使用它来获取另一表中的更多数据?

时间:2018-11-28 12:53:37

标签: sql-server vba

我在SQL SERVER中有两个表,分别是1)tblBuyerCodes 2)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). 

我要实现的是基于tblBUyerCodes表中获取的购买者代码在tblStatistics中获取结果并将其发布到tblResults中

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

dm6dm8是买方代码

1 个答案:

答案 0 :(得分:0)

基于有限的详细信息,为什么不呢?

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