왜 하나에 두 문장을 결합해야합니까? 그러나이를 수행하면 결과 집합의 크기가 불필요하게 네트워크를 통해 전달됩니다.
내가 대신 저장 프로 시저에 두 문장을 결합하는 것이 좋습니다 : 클라이언트 측 코드에서
CREATE PROCEDURE GetAllData (@NumberOfRecipients int output) AS BEGIN
SELECT C.*, M.members_Email
FROM tbl_Campaigns C
JOIN tbl_Members M ON C.campaign_MemberId = M.members_Id
WHERE C.campaign_MemberId = @userID
ORDER BY C.campaign_Key DESC
SELECT @NumberOfRecipients = COUNT(*)
FROM tbl_CampaignRecipients
WHERE recip_CampaignId = C.campaign_Key
AND (recipient_Status = 3 or recipient_Status = 4)
END
을, 당신은 이런 식으로 부를 것이다 :
- 가
GetAllData
에 대한 명령 객체를 생성 저장 프로 시저.
@NumberOfRecipients
매개 변수에 대한 매개 변수 개체를 연결하십시오.
- 명령을 실행하십시오.
- 기본 결과 집합을 사용합니다.
@NumberOfRecipients
매개 변수 값을 읽습니다 (결과 집합을 사용한 후에이 은이어야 함).
C# 예제 :
using(SqlCommand command = new SqlCommand("GetAllData", connection)) {
command.CommandType = CommandType.StoredProcedure;
SqlParameter recipientsParam = new SqlParameter("@NumberOfRecipients", SqlDbType.Int) { Direction = ParameterDirection.Output };
command.Parameters.Add(recipientsParam);
using(SqlDataReader reader = command.ExecuteReader()) {
// consume the resultset
}
// read the parameter
int recipients = (int) recipientsParam.Value;
}
할 수도 다시 사용하는 서버 측 T-SQL 코드에서 저장 프로 시저, 예를 들면 : 위해 일에만 나타납니다 것을
declare @NumberOfRecipients int
insert into #Results execute GetAllData @NumberOfRecipients output
Andomar을 내부 문장의 결과를 갖는 결과. – Munklefish
@Munklefish : 캠페인이 없으면 캠페인 수신자가 없을 수 있습니다. 어쨌든, 당신은 어떤 행 집합 (들) 당신이 그 경우에 반환하고자하는 설명하기 위해 질문을 편집 할 수 있습니까? – Andomar
Andomar, 내부 문장의 결과가 없으면 다른 (외부) 문장의 모든 데이터가 여전히 필요합니다 (예 : recipient_Status is not 3 또는 4). – Munklefish