2009-08-26 85 views
2

我目前有以下存儲過程;存儲過程計算和性能改進

CREATE PROCEDURE web.insertNewCampaign 
    (
    @tmp_Id BIGINT, 
    @tmp_Title VARCHAR(100), 
    @tmp_Content VARCHAR(8000), 
    @tmp_Pledge DECIMAL(7,2), 
    [email protected]_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 
...... 

所以我有2個問題:

1)怎樣除以@recipients @tmp_Pledge給@allocation如:(@分配= @tmp_Pledge/@recipients)

2)是否有可能通過@allocation將這些語句複合爲一個更有效的語句,並將其有效地作爲值插入到列[campaign_RecipShare]中,並減少對這些已聲明變量的需求?

很多感謝您提供任何問題的任何幫助。

;-)

回答

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 

這也消除了需要爲你計算012之外@allocation成員聲明。

+0

看起來不錯,但sql管理器不會編譯它。它說addMaster_NoReceipients的第二個引用不是一個有效的列名。 – Munklefish 2009-08-26 13:49:59

1

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 

從tbl_AddressBookMaster WHERE addMaster_UserId = @tmp_Id AND addMaster_Key = @tmp_AddyBook;

SELECT @tmp_IDENTITY = SCOPE_IDENTITY() - 這將返回新添加的標識ID

1
set @allocation = @tmp_pledge/(@recepients* 1.0) 

你想這樣做,因爲othewise你會遇到的整數運算和結果將舍爲整數。