将查询值分配给变量

时间:2016-10-04 08:47:38

标签: sql sql-server

将查询的返回值分配给存储过程中的变量真的很麻烦,这就是我所拥有的:

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参数是否正常,这很好,手动尝试查询总是会返回结果。

不确定是什么问题。

由于

2 个答案:

答案 0 :(得分:0)

您需要返回该值或制作结果集

php artisan make:notification post

答案 1 :(得分:0)

您可以使用输出参数

    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

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