0

我有一列成本价格和一列零售价格,但该列是一个计算字段,成本价格 + 35%,所以我想编写一个自动更新零售价格的选择语句,但只是想不出该怎么做。你能帮我吗。

Cost Price      Retail Price
   35
   45
  125
   35

DECLARE @OrderNumber varchar (30)
DECLARE @OrderDate int
DECLARE @OrderLineNumber varchar(50)
DECLARE @CustomerSkey int
DECLARE @ProductSkey int
DECLARE @OrderMethodSkey int
DECLARE @Quantity int
DECLARE @Cost Decimal(18,3)


SET @OrderNumber = 1
SET @OrderDate = 0
SET @OrderLineNumber = 1
SET @CustomerSkey = 1
SET @ProductSkey = 1
SET @OrderMethodSkey = 1
SET @Quantity = 1
SET @Cost = 1


WHILE @OrderNumber <= 100
WHILE @OrderDate <= 100
WHILE @OrderLineNumber <= 100
WHILE @CustomerSkey <= 100
WHILE @ProductSkey <= 100
WHILE @OrderMethodSkey <= 100
WHILE @Quantity <= 100
WHILE @Cost <= 100



BEGIN
INSERT INTO Orders 
(OrderNumber
, OrderDate
, OrderLineNumber
, CustomerSkey
, ProductSkey
, OrderMethodSkey
, OrderTime
, Quantity
, Cost
, Price)

SELECT 
'ORD' + Right ('000000' + CAST (@OrderNumber AS varchar (30)), 6)
,DATEADD (day, CAST (RAND () * 1500 as int), '2008-1-1')
,(Right ('0' + CAST (@OrderLineNumber AS varchar (30)), 6))
,(99 * RAND()) + 1
,(99 * RAND()) + 1
,(2 * RAND()) + 1
,DATEADD(ms, cast(86400000 * RAND() as int), convert(time, '00:00'))
,(190 * RAND()) + 10
,(40 * RAND()) + 10
,@Cost + (@Cost * .35)



SET @OrderNumber = @OrderNumber + 1
SET @OrderDate = @OrderDate + 1
SET @OrderLineNumber = @OrderLineNumber + 1
SET @CustomerSkey = @CustomerSkey + 1
SET @ProductSkey = @ProductSkey + 1
SET @OrderMethodSkey = @OrderMethodSkey + 1
SET @Quantity = @Quantity + 1
SET @Cost = @Cost + 1


END
4

4 回答 4

2

使用以下公式将其设为计算列:

CostPrice + (CostPrice * .35)

因此,每当您将新条目插入表时,CostPrice都会自动评估。

于 2013-05-22T14:49:05.887 回答
0
UPDATE TableName SET RetailPrice = CostPrice + (CostPrice * .35)
于 2013-05-22T14:47:43.263 回答
0
UPDATE Orders SET Price = Cost * 1.35

或者,如果您想将百分比设为变量:

UPDATE Orders SET Price = Cost * (1 + (@PERCENT / 100))
于 2013-05-22T14:48:57.090 回答
0

如果零售价总是等于成本价加上某个百分比,那么您可能想看看计算列是否有意义。

这是使用计算列定义的表的示例

CREATE TABLE TableName ( 
    CostPrice DECIMAL(18,4), 
    RetailPrice AS (CostPrice * 1.35)
);

但是,如果您无法定义或更改表架构,或者只是想更新所选行的零售价,那么您可以在使用 CostPrice 列中的值时更新 RetailPrice 列。

UPDATE TableName SET RetailPrice = CostPrice * 1.35

对于每一行,这将使用 CostPrice 来设置该行的 RetailPrice。WHERE如果要限制更新的行,而不是更新表中的每一行,还可以包含一个子句。

于 2013-05-22T15:03:31.537 回答