简体   繁体   English

SQL Server:存储过程挑战

[英]SQL Server : stored procedure challenge

Here's the scenario: I have a table for SunflowerSeeds in SQL Server 2008. 这是场景:我在SQL Server 2008中有一个SunflowerSeeds表。

CREATE TABLE [dbo].[SunflowerSeeds](
    [Color] [nvarchar](50) NULL,
    [SeedType] [nvarchar](50) NULL,
    [Price] float NULL
)

For simplicity, I haven't inserted long list of data. 为简单起见,我没有插入长长的数据列表。

Data insertion into the table looks like this: 插入表中的数据如下所示:

Insert into [dbo].[SunflowerSeeds] (Color, SeedType, Price)
SELECT 'Yellow', 'Dwarf', 20
UNION ALL
SELECT 'Brown', 'Garden', 30
UNION ALL
SELECT 'Red', 'Garden', 35

Now I have been provided various matrices that could raise the price of the seed. 现在我已经提供了各种可以提高种子价格的矩阵。

  1. Polenless (YES/NO): If YES, raise the price by 10% Polenless(是/否):如果是,将价格提高10%
  2. Height (> 13"): If YES, raise the price by 8% 高度(> 13“):如果是,则将价格提高8%
  3. Organic (YES/NO): If Yes, raise the price by 15% 有机(是/否):如果是,将价格提高15%
  4. Lifecycle (Annual- YES/NO): If Yes, raise the price by 12% 生命周期(年度 - 是/否):如果是,则将价格提高12%

Each data insert row could have YES or NO value to each of the matrix listed above. 每个数据插入行对于上面列出的每个矩阵可以具有YES或NO值。 I need to come up with a process to return the new price based on the Price of each seed in the SunFlowerseeds table and taking into a/c the combination of matrices. 我需要提出一个流程,根据SunFlowerseeds表中每个种子的Price返回新价格,然后考虑矩阵的组合。

For eg, if Yellow Dwarf is polenless, with height > 13”, Organic YES and having an annual life cycle then the new price has to be computed as 例如,如果黄矮星是无气的,高度> 13“,有机是,并且具有年度生命周期,则新价格必须计算为

10% + 8%+15%+12% = 45% = 1+ 0.45= 1.45 

Then new price is: 1.45 * Original Price= 1.45*20 = 29 然后新价格是: 1.45 * Original Price= 1.45*20 = 29

Likewise for rest of the combination. 同样,其余的组合。

I'm thinking of writing a SQL Server stored procedure, so that it could be called in my C# code later. 我正在考虑编写一个SQL Server存储过程,以便以后可以在我的C#代码中调用它。 The i/p parameters for the stored procedure will be Color and SeedType . 存储过程的i / p参数将是ColorSeedType With those two parameters, I could get the Price . 有了这两个参数,我就能得到Price

The challenge is how do I compute the new Price for each of the seed with a combination of applicable matrices - Polenless, height, Organic, LifeCycle. 面临的挑战是如何使用适用的矩阵组合计算每种种子的新Price - Polenless,height,Organic,LifeCycle。 Can anyone point me a proper way of doing it. 任何人都可以指出我这样做的正确方法。 Please kindly help me out. 请帮帮我。 TIA. TIA。

Suppose that combination of Color and SeedType in SunflowerSeeds is unique: 假设SunflowerSeeds中Color和SeedType的组合是唯一的:

CREATE PROCEDURE UpdatePrice
    @Polenless BIT,
    @Height BIT,
    @Organic BIT,
    @Lifecycle BIT,
    @color NVARCHAR(50),
    @seedType NVARCHAR(50)

AS
BEGIN
   UPDATE SunflowerSeeds
   SET Price = Price +  
                        ((Price * 0.1) * @Polenless)+
                        ((Price * 0.08) * @Height)+
                        ((Price * 0.15) * @Organic)+
                        ((Price * 0.12) * @Lifecycle)
    WHERE Color = @color  AND SeedType = @seedType

END
GO

Also you may use a bitwise flag of type int instead of @Polenless, @Height, @Organic, @Lifecycle. 您也可以使用int类型的按位标志,而不是@Polenless,@ Height,@ Organic,@ Lifecycle。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM