Retrieving output parameter value along with other return values from select statement having more than one return value

血红的双手。 提交于 2019-12-11 08:32:09

问题


Hi i have an output parameter to a stored procedure and its value has to be returned from the two internal select statements. I have @nTotalRecords as my output parameter that value will come from the below select statement how should i retrieve the output parameter from the alias table TBL in this case i tried like this

create procedure [usp_GetMessagesbyReferenceID1]
(        
@nRowsPerPage               int,
@nPage                      int,
@nTotalRecords              int output 
) as
select  
    TBL.createdate,
    TBL.templateid,
    @nTotalRecords=TBL.TotalRecords
    from
     (
            select
                  message.createdate,
                  message.templateid,
                  count(1) over() as TotalRecords
            from
                  nts.Messages as [message]    
     )  as TBL

i am trying it this way to set the output parameter but it is not working it is throwing the following error A SELECT statement that assigns a value to a variable must not be combined with data retrieval operations.is there any possibility to achieve this. ? Or am i making any mistake syntactically , Please help me out.

NOTE: The retrieving of output parameter from select statement works fine if it returns only one value(output parameter value), but my requirement is it should work properly when my select statement returns more than one value(including output parameter).


回答1:


I have tried a lot to conquer this, but atlast i have achieved this through this way, I have written one more select statement to get the totalrecords count. I'm not sure that whether the answer was perfect and efficient, but it just worked fine for me. Please let me know if there are any other ways.

create procedure [usp_GetMessagesbyReferenceID1]
(        
@nRowsPerPage               int,
@nPage                      int,
@nTotalRecords              int output 
) as
select  
   TBL.createdate,
   TBL.templateid
from
   (
      select
         message.createdate,
         message.templateid
      from
         nts.Messages as [message]    
   )  as TBL

 select 
    @nTotalRecords = count(1)
 from 
    nts.Messages as [message]



回答2:


select  
    @CreatedDate=TBL.createdate,
    @nTotalRecords=TBL.TotalRecords
    from
     (
            select
                  message.createdate,
                  count(1) over() as TotalRecords
            from
                  nts.Messages as [message]

     )  as TBL

Your code should be like this. Removed the 1st column in the Select. This will work considering the inner query returns only one value for TotalRecords. If it returns more than one then the last value will get assigned to @nTotalRecords




回答3:


As per discussion, below code will help you to get the TotalRecords (int with single value) and CreatedDate (string with comma separated date values).

create procedure [usp_GetMessagesbyReferenceID1]
(        
@nRowsPerPage               int,
@nPage                      int,
@nTotalRecords              int output,
@CreatedDate                 varchar(500) output 
) as

select @CreatedDate=
  STUFF (( SELECT ','+ CAST(T.dates as varchar(15))
           FROM nts.Messages T
           FOR XML PATH('')),1,1,'')
 select @nTotalRecords=TBL.TotalRecords    
 from
     ( select dates, count(1) over() as TotalRecords
       from nts.Messages
     )  as TBL


来源:https://stackoverflow.com/questions/43039801/retrieving-output-parameter-value-along-with-other-return-values-from-select-sta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!