如何将两列相乘并将其输出分配给另一个表的列?

时间:2018-05-09 09:54:47

标签: sql-server

我是数据库初学者。我正在使用Microsoft SQL Server Management Studio。我将一个表的两列相乘并将这些列的输出分配给另一个表,但我不知道该怎么做。有人帮我吗?

我有两个列,一个是UnitPrice列,另一个是PurchasesTable中的Quantity,我想将这些列的输出插入另一个具有Name Dues的表的TotalAmount。提前谢谢。

1 个答案:

答案 0 :(得分:0)

/****** Object:  Table [dbo].[Sample1]    Script Date: 5/9/2018 3:59:09 PM 
******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Sample1](
[ID] [INT] IDENTITY(1,1) NOT NULL,
[UnitPrice] [DECIMAL](16, 2) NOT NULL,
[Quantity] [DECIMAL](10, 2) NOT NULL,
[TotalAmount]  AS ([UnitPrice]*[Quantity]),
CONSTRAINT [PK_Sample1] PRIMARY KEY CLUSTERED 
   (
   [ID] ASC
   )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = 
 OFF, 
 ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
 ) ON [PRIMARY]

  GO

INSERT INTO dbo.Sample1
    ( UnitPrice, Quantity )
VALUES  ( 100, -- UnitPrice - decimal
      25  -- Quantity - decimal
      )

SELECT * FROM dbo.Sample1
---OUTPUT------------
ID  UnitPrice   Quantity    TotalAmount
1   100.00  25.00   2500.0000