gpt4 book ai didi

sql - 存储过程计算和性能改进

转载 作者:行者123 更新时间:2023-12-04 07:04:28 28 4
gpt4 key购买 nike

我目前有以下存储过程;

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],并减少对这些声明变量的需求?

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

;-)

最佳答案

第一次选择后,可以这样设置@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

这也消除了您计算 @allocation 的需要。 insert之外的成员(member)陈述。

关于sql - 存储过程计算和性能改进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1334547/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com