views:

220

answers:

3

Hi,

I currently have the following stored procedure;

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
......

So i have 2 questions:

1) How do i divide @tmp_Pledge by @recipients to give @allocation eg:(@allocation = @tmp_Pledge / @recipients)

2) Is it possible to compound these statements into a more efficient statement(s) with @allocation effectively being inserted as a value into the column [campaign_RecipShare], and reducing the need for these declared variables?

Many Thanks for any help you can offer for either question.

;-)

+1  A: 

After the first select, you can do this to set @allocation:

set @allocation = @tmp_pledge / @recepients

As for making it more efficient, it's already fairly efficient--you won't go through any less steps, but you can condense the code a bit:

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

This also removes the need for you calculating the @allocation member outside of the insert statement.

Eric
Looks good, but the sql manager wont compile it. Its saying the 2nd reference of addMaster_NoReceipients isnt a valid column name.
Munklefish
+1  A: 

1) @tmp_pledge / @recepients - I'll assume allocation is a numeric field of some form in TBL_CAMPAIGNS holding a number in varchar is not a good idea.

2) You just need to build a select that returns all the values from the other table and the parameters matching the columns to insert into.

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 WHERE addMaster_UserId = @tmp_Id AND addMaster_Key = @tmp_AddyBook;

SELECT @tmp_IDENTITY = SCOPE_IDENTITY() --this returns the newly added IDENTITY ID

Mark
+1  A: 

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

You want to do that because othewise you will run into integer math and the result will round to an integer.

HLGEM