簡體   English   中英

將查詢值分配給變量

[英]Assign query value to variable

將查詢的返回值分配給存儲過程中的變量真的很麻煩,這就是我所擁有的:

ALTER PROCEDURE [dbo].[sp_CreateTaskFromProposalStatusUpdate]
@proposalstatusupdateid uniqueidentifier

AS
BEGIN

DECLARE @owninguser uniqueidentifier
SET @owninguser = '07da8e53-74bd-459c-af94-a037897a51e3' -- todo set to salesperson

DECLARE @name nvarchar(300)
SET @name = 'test name'

DECLARE @accountid uniqueidentifier
DECLARE @accountcontacts varchar(100)

SET @accountid = NEWID()
SET @accountcontacts = CAST(@proposalstatusupdateid as VARCHAR(36))

DECLARE @proposalid uniqueidentifier
--SET @proposalid = (SELECT new_propstatusupdateid FROM new_proposalstatusupdate WHERE New_proposalstatusupdateId = @proposalstatusupdateid)
SELECT @proposalid = new_propstatusupdateid FROM new_proposalstatusupdate WHERE New_proposalstatusupdateId = @proposalstatusupdateid

INSERT INTO ActivityPointerBase
(OwningBusinessUnit,
ActivityId,
IsBilled,
CreatedBy,
[Description],
DeletionStateCode,
ModifiedOn,
ActivityTypeCode,
StateCode,
ScheduledEnd,
ScheduledDurationMinutes,
ActualDurationMinutes,
StatusCode,
ActualStart,
CreatedOn,
PriorityCode,
RegardingObjectId,
[Subject],
IsWorkflowCreated,
ScheduledStart,
ModifiedBy,
OwningUser,
RegardingObjectTypeCode,
RegardingObjectIdName,
TimeZoneRuleVersionNumber,
RegardingAccountId,
RegardingAccountTelephone)
VALUES
('C5B71CA7-1230-4D2A-8DEA-26184EA5E262',
NEWID(),
0,
@owninguser,
'002 Proposal {decision} with {funder} B',
0,
GETDATE(),
4212,
0,
GETDATE(),
0,
1,
1,
GETDATE(),
GETDATE(),
1,
@proposalid,
'002 Proposal {decision} with {funder} B',
0,
GETDATE(),
@owninguser,
@owninguser,
10011,
@name,
0,
@accountid,
@accountcontacts)

我嘗試過兩種方法嘗試設置它但它們都導致@proposalidNULL

我檢查了@proposalstatusupdateid參數是否正常,並且沒問題,手動嘗試查詢總是返回結果。

不確定是什么問題。

謝謝

您需要返回該值或生成結果集

ALTER PROCEDURE [dbo].[sp_CreateTaskFromProposalStatusUpdate]
@proposalstatusupdateid uniqueidentifier

AS

DECLARE @proposalid uniqueidentifier
--SET @proposalid = (SELECT new_propstatusupdateid FROM new_proposalstatusupdate WHERE New_proposalstatusupdateId = @proposalstatusupdateid)
SELECT @proposalid = new_propstatusupdateid FROM new_proposalstatusupdate WHERE New_proposalstatusupdateId = @proposalstatusupdateid

SELECT @proposalid as new_propstatusupdateid

你可以使用輸出參數

    ALTER PROCEDURE [dbo].[sp_CreateTaskFromProposalStatusUpdate]
    @proposalstatusupdateid uniqueidentifier,
    @Proposalid_out out

    AS
    BEGIN

    DECLARE @owninguser uniqueidentifier
    SET @owninguser = '07da8e53-74bd-459c-af94-a037897a51e3' -- todo set to salesperson

    DECLARE @name nvarchar(300)
    SET @name = 'test name'

    DECLARE @accountid uniqueidentifier
    DECLARE @accountcontacts varchar(100)

    SET @accountid = NEWID()
    SET @accountcontacts = CAST(@proposalstatusupdateid as VARCHAR(36))

    DECLARE @proposalid uniqueidentifier
    --SET @proposalid = (SELECT new_propstatusupdateid FROM new_proposalstatusupdate WHERE New_proposalstatusupdateId = @proposalstatusupdateid)
    SELECT @proposalid = new_propstatusupdateid FROM new_proposalstatusupdate WHERE New_proposalstatusupdateId = @proposalstatusupdateid

    INSERT INTO ActivityPointerBase
    (OwningBusinessUnit,
    ActivityId,
    IsBilled,
    CreatedBy,
    [Description],
    DeletionStateCode,
    ModifiedOn,
    ActivityTypeCode,
    StateCode,
    ScheduledEnd,
    ScheduledDurationMinutes,
    ActualDurationMinutes,
    StatusCode,
    ActualStart,
    CreatedOn,
    PriorityCode,
    RegardingObjectId,
    [Subject],
    IsWorkflowCreated,
    ScheduledStart,
    ModifiedBy,
    OwningUser,
    RegardingObjectTypeCode,
    RegardingObjectIdName,
    TimeZoneRuleVersionNumber,
    RegardingAccountId,
    RegardingAccountTelephone)
    VALUES
    ('C5B71CA7-1230-4D2A-8DEA-26184EA5E262',
    NEWID(),
    0,
    @owninguser,
    '002 Proposal {decision} with {funder} B',
    0,
    GETDATE(),
    4212,
    0,
    GETDATE(),
    0,
    1,
    1,
    GETDATE(),
    GETDATE(),
    1,
    @proposalid,
    '002 Proposal {decision} with {funder} B',
    0,
    GETDATE(),
    @owninguser,
    @owninguser,
    10011,
    @name,
    0,
    @accountid,
    @accountcontacts)
SET @Proposalid_out = proposalid

/ ***********致電************************** /

DECLARE @Proposalid_out uniqueidentifier
EXEC sp_CreateTaskFromProposalStatusUpdate
        @proposalstatusupdateid uniqueidentifier,
        @Proposalid_out out
SELECT @Proposalid_out

/ ******************* *************

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM