【问题标题】:STORED PROCEDURE Calculations & performance improvements存储过程计算和性能改进
【发布时间】:2009-08-26 13:06:57
【问题描述】:

我目前有以下存储过程;

CREATE PROCEDURE web.insertNewCampaign
   (
   @tmp_Id BIGINT,
   @tmp_Title VARCHAR(100),
   @tmp_Content VARCHAR(8000),
   @tmp_Pledge DECIMAL(7,2),
   --@tmp_Recipients BIGINT,
   @tmp_Date DATETIME,
   @tmp_Private BIT,
   @tmp_Template BIGINT,
   @tmp_AddyBook BIGINT
   )
AS
   declare @recipients BIGINT
   declare @tmp_IDENTITY BIGINT
   declare @fave BIGINT
   declare @allocation VARCHAR(50)

   --insert campaign data
   BEGIN TRAN
   SELECT @recipients = addMaster_NoRecipients FROM tbl_AddressBookMaster 
   WHERE addMaster_UserId = @tmp_Id AND addMaster_Key = @tmp_AddyBook;
   INSERT INTO TBL_CAMPAIGNS ([campaign_MemberId], [campaign_Title], [campaign_Content], [campaign_Pledge], [campaign_Date], [campaign_Private], [campaign_Template], [campaign_AddressBook], [campaign_Recipients]) 
   VALUES (@tmp_Id, @tmp_Title, @tmp_Content, @tmp_Pledge, @tmp_Date, @tmp_Private, @tmp_Template, @tmp_AddyBook, @recipients)
   SELECT @tmp_IDENTITY = SCOPE_IDENTITY() --this returns the newly added IDENTITY ID
   COMMIT
......

所以我有两个问题:

1) 我如何将@tmp_Pledge 除以@recipients 以给出@allocation 例如:(@allocation = @tmp_Pledge / @recipients)

2) 是否可以将这些语句组合成更有效的语句,将@allocation 作为值有效地插入到 [campaign_RecipShare] 列中,并减少对这些声明变量的需求?

非常感谢您为任一问题提供的任何帮助。

;-)

【问题讨论】:

    标签: sql sql-server sql-server-2005 tsql stored-procedures


    【解决方案1】:

    第一次选择后,可以这样设置@allocation

    set @allocation = @tmp_pledge / @recepients
    

    至于让它更高效,已经相当高效了——你不会少走任何步骤,但你可以稍微压缩一下代码:

    INSERT INTO TBL_CAMPAIGNS (
        [campaign_MemberId], [campaign_Title], [campaign_Content], 
        [campaign_Pledge], [campaign_Date], [campaign_Private], 
        [campaign_Template], [campaign_AddressBook], [campaign_Recipients],
        [capmain_RecipShare]) 
    SELECT 
        @tmp_Id, @tmp_Title, @tmp_Content, 
        @tmp_Pledge, @tmp_Date, @tmp_Private, 
        @tmp_Template, @tmp_AddyBook, addMaster_NoRecipients,
        @tmp_Pledge / addMaster_NoReceipients as Allocation
    FROM
        tbl_AddressBookMaster
    WHERE
        addMaster_UserId = @tmp_Id
        AND addMaster_Key = @tmp_AddyBook
    
    SELECT @tmp_IDENTITY = SCOPE_IDENTITY() --this returns the newly added IDENTITY ID
    

    这也消除了您在 insert 语句之外计算 @allocation 成员的需要。

    【讨论】:

    • 看起来不错,但是 sql manager 不会编译它。它说 addMaster_NoReceipients 的第二个引用不是有效的列名。
    【解决方案2】:

    1) @tmp_pledge / @recepients - 我假设分配是 TBL_CAMPAIGNS 中某种形式的数字字段,在 varchar 中保存数字不是一个好主意。

    2) 您只需要构建一个 select 来返回其他表中的所有值以及与要插入的列匹配的参数。

    insert into TBL_CAMPAIGNS ([campaign_MemberId], [campaign_Title], [campaign_Content],    [campaign_Pledge], [campaign_Date], [campaign_Private], [campaign_Template], [campaign_AddressBook], [campaign_Recipients], [campaign_allocation) 
    
    select @tmp_Id, @tmp_Title, @tmp_Content, @tmp_Pledge, @tmp_Date, @tmp_Private, @tmp_Template, @tmp_AddyBook, addMaster_NoRecipients, @tmp_pledge / addMaster_NoRecipients
    

    FROM FROM tbl_AddressBookMaster 其中 addMaster_UserId = @tmp_Id AND addMaster_Key = @tmp_AddyBook;

    SELECT @tmp_IDENTITY = SCOPE_IDENTITY() --返回新添加的IDENTITY ID

    【讨论】:

      【解决方案3】:
      set @allocation = @tmp_pledge / (@recepients* 1.0)
      

      你想这样做是因为否则你会遇到整数数学,结果将四舍五入为整数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-02
        • 1970-01-01
        • 2015-03-19
        • 2013-07-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多