如何创建一个SQL Server存储过程,该存储过程可以生成一个包含三个表的表

时间:2020-08-02 10:09:50

标签: sql sql-server

我有三个SQL表,如下所示。

表1:基准

WorkLoadType    MaxUsersperVCpu vCPU    RAM MaxUserCapacity
------------------------------------------------------------
Light           6                2        8     12
Medium          4                4       16     16
Heavy           2                4       16     8
Power           1                6       56     6

表#2:VMType

id  HostPoolName    Resource_Group  WorkLoadType
------------------------------------------------
1   Pool1               RG1         Light
2   Pool2               RG2         Light
3   Pool3               RG3         Light
4   Pool4               RG4         Light

表#3:VM

HostPoolName    MemoryInMB  Name        NumberOfCores   Resource_Group  VMSize
-------------------------------------------------------------------------------
Pool1              8192     VM1             2            RG1           Standard_D2s_v3
Pool1              8192     VM2             2            RG1        Standard_D2s_v3
Pool2              8192     VM3             2            RG2        Standard_D2s_v3
Pool3              8192     VM4             2            RG3        Standard_D2s_v3
Pool3              8192     VM5             2            RG3        Standard_D2s_v3
Pool3              8192     VM6             2            RG3        Standard_D2s_v3
Pool3              8192     VM7             2            RG3        Standard_D2s_v3
Pool3              8192     VM8             2            RG3        Standard_D2s_v3

我需要创建一个存储过程,该存储过程应产生一个像这样的新表。 Capacity列是MaxUserPervCpuNumberOfCores

的乘积

表:容量

Name        HostPoolName    ResourceGroup   WorkLoadType    MaxUserPerVCpu  NumberOfCores   Capacity
-----------------------------------------------------------------------------------------------------
VM1         Pool1           RG1              Light                    6     2               12       
VM2         Pool1           RG1              Light                    6     2               12
VM3         Pool2           RG2              Light                    6     2               12
VM4         Pool3           RG3              Light                    6     2               12
VM5         Pool3           RG3              Light                    6     2               12
VM6         Pool3           RG3              Light                    6     2               12

在此方面的帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

这看起来很简单join

select vm.*, b.*   -- choose the columns you want here
from vm join
     vmtype vt
     on vt.HostPoolName = vm.HostPoolName join
     Benchmark b
     on b.WorkLoadType = vt.WorkLoadType;

答案 1 :(得分:0)

您需要标识所有这些表之间的公用键,并进行内部或外部联接。 联接之后,如果您想包括一个公式,则可以,但是您可能必须使用group by语句对其余的列进行分组。 完成之后,您只需插入目标表即可。

有两个选项可以插入...插入或选择插入到

这是两者之间的区别

https://www.c-sharpcorner.com/blogs/select-into-insert-into-in-sql-server1

相关问题