在我的存储过程中,我有SELECT
个语句。我想将这些SELECT
语句的结果作为参数传递给函数。请告诉我如何在SQL Server中传递参数。
我的存储过程:
CREATE PROCEDURE PositionReport
AS
BEGIN
(select COUNT(ID) from Submissions where SubmissionStatus = 'Status1') Status1,
(select COUNT(ID) from Submissions where SubmissionStatus = 'Status2') Status2,
dbo.[fnGetTotal](Status1,Status1) Total
END
功能:
CREATE FUNCTION [dbo].[fnGetTotal](@Status1 int, @Status1 int)
RETURN varchar(100) AS
BEGIN
DECLARE @Total varchar(100);
SELECT @Total = @Status1 + @Status1
RETURN @Total
END
我的原始存储过程:
CREATE PROCEDURE [dbo].[Usp_GetJobsPositionReport]
@AccountID int,
@Userid int,
@JobTitle varchar(100),
@ClientID varchar(8000),
@ContactID varchar(8000),
@PageNum int,
@PageSize int,
@SortExpression varchar(1000),
@isExport bit = 0
AS
BEGIN
SELECT @sqlQuery = 'SELECT * FROM (SELECT distinct ROW_NUMBER() over (ORDER BY ' + @SortExpression + ') as SNo, Jobs.JobNo, Jobs.JobID, Jobs.JobTitle, VendorClients.CompanyName Client, VendorContacts.FirstName + '' '' + VendorContacts.LastName as ContactName, Users.FirstName + '' ''+ Users.LastName CreatedBy, (select COUNT(SubmissionID) from CandidateSubmissions where SubmissionStatus in(''Submitted'',''Screening'') and InterviewType is null and CandidateSubmissions.jobid=jobs.jobid) Submitted, (select COUNT(SubmissionID) from CandidateSubmissions where SubmissionStatus not in (''Potential'',''Rejected'',''Attached'',''CandidateWithdrawn'',''Backout'',''Matched'') and CandidateSubmissions.jobid=jobs.jobid) Active,
(select COUNT(SubmissionID) from CandidateSubmissions where SubmissionStatus = ''Shortlisted'' and CandidateSubmissions.jobid=jobs.jobid) ShortListed,
(select COUNT(SubmissionID) from CandidateSubmissions where SubmissionStatus in(''Submitted'',''Screening'') and InterviewType = ''Phone'' and CandidateSubmissions.jobid=jobs.jobid) PhoneInterview,
(select COUNT(SubmissionID) from CandidateSubmissions where SubmissionStatus in(''Submitted'',''Screening'') and InterviewType = ''Face2Face'' and CandidateSubmissions.jobid=jobs.jobid) F2FInterview,
(select COUNT(SubmissionID) from CandidateSubmissions where SubmissionStatus in(''Submitted'',''Screening'') and InterviewType = ''Web'' and CandidateSubmissions.jobid=jobs.jobid) WebInterview,
(select COUNT(SubmissionID) from CandidateSubmissions where SubmissionStatus = ''UnderReview'' and CandidateSubmissions.jobid=jobs.jobid) UnderReview,
(select COUNT(SubmissionID) from CandidateSubmissions where SubmissionStatus = ''OfferMade'' and CandidateSubmissions.jobid=jobs.jobid) OfferMade,
(select COUNT(SubmissionID) from CandidateSubmissions where SubmissionStatus = ''OfferRevoked'' and CandidateSubmissions.jobid=jobs.jobid) OfferRevoked,
(select COUNT(SubmissionID) from CandidateSubmissions where SubmissionStatus = ''OfferNegotiating'' and CandidateSubmissions.jobid=jobs.jobid) OfferNegotiating,
(select COUNT(SubmissionID) from CandidateSubmissions where SubmissionStatus = ''OfferAccepted'' and CandidateSubmissions.jobid=jobs.jobid) OfferAccepted,
(select COUNT(SubmissionID) from CandidateSubmissions where SubmissionStatus = ''OfferRejected'' and CandidateSubmissions.jobid=jobs.jobid) OfferRejected,
(select COUNT(SubmissionID) from CandidateSubmissions where SubmissionStatus = ''Screening'' and CandidateSubmissions.jobid=jobs.jobid) Screening,
(select COUNT(SubmissionID) from CandidateSubmissions where SubmissionStatus = ''Joined'' and CandidateSubmissions.jobid=jobs.jobid) Joined,
Jobs.PositionType TypeOfHire,REPLACE(CONVERT(VARCHAR(11), Jobs.PostedDate, 106), '' '', ''-'') PostedDate,
PrimaryIndustry = case Jobs.PrimaryIndustry
when ''Select'' then ''''
else Jobs.PrimaryIndustry
end,
JobCategory = case Jobs.JobCategory
when ''Select'' then ''''
else Jobs.JobCategory
end,'
select @sqlLinkQuery = 'Education = case Jobs.MinimumEducation
when ''Select'' then ''''
else Jobs.MinimumEducation
end,
FROM Jobs WITH(NOLOCK)
INNER JOIN Users WITH(NOLOCK) ON Jobs.UserID = Users.UserID and Users.IsActive=1
INNER JOIN AccountUsers WITH(NOLOCK) ON AccountUsers.UserID = Jobs.UserID
INNER JOIN CityList WITH(NOLOCK) ON Jobs.JobLocation = CityList.CityID
INNER JOIN JobAssignedUsers WITH(NOLOCK) ON Jobs.JobID = JobAssignedUsers.JobID
where Jobs.JobStatus = ''Open'' AND jobAssignedUsers.userid = '+ CONVERT(varchar(9),@Userid)+ ' AND VendorClients.AccountID = '+ CONVERT(varchar(9),@AccountID)+ @strJobTitle + @strClientID + @strContactID +'
) as Result'
SELECT @subSql = ' where Result.SNo >= '+ CONVERT(varchar(9), @boundPageNum) +' and Result.SNo <= '+ CONVERT(varchar(9), @boundPageSize)
IF(@isExport = 0)
BEGIN
EXEC (@sqlQuery + @sqlLinkQuery + @subSql)
EXEC('SELECT COUNT(*)TotalRows FROM ('+@sqlQuery + @sqlLinkQuery+')a')
END
ELSE
BEGIN
EXEC (@sqlQuery + @sqlLinkQuery)
END
END
答案 0 :(得分:0)
按以下步骤更改您的程序
CREATE PROCEDURE PositionReport
as
BEGIN
DECLARE @Status1 int
DECLARE @Status2 int
select @Status1=COUNT(ID) from Submissions where SubmissionStatus = 'Status1'
select @Status2=COUNT(ID) from Submissions where SubmissionStatus = 'Status2'
SELECT @Status1 as Status1,@Status2 as Status2
,dbo.[fnGetTotal](@Status1,@Status1) Total
END
但是我不认为除非你做一些更复杂的事情,否则需要一个单独的功能。
答案 1 :(得分:0)
只需将自身(作为字符串)的select语句(而不是结果)传递给存储的函数,然后在函数内部运行它即可。
要从字符串中运行一些SQL命令,请使用:
sys.sp_executesql
示例:
EXEC sys.sp_executesql N'select ''Hello world'' as Result'