应该在数据库方面还是在API方面完成两个存储过程结果集的合并?

时间:2018-06-25 10:41:50

标签: sql sql-server-2012

下面是API,我需要创建一个新的存储过程以返回它。 “ MemberCard”之前的上部具有可以输出的存储过程,“ MemberCard”的子详细信息也具有其自己的存储过程。如何以下面相同的格式合并2个存储过程的结果集?还是应该使用Java API来合并2个sp的结果集?

示例响应:

{  
   "Action":"MemberLogin3",
   "CardNumber":"00000376",
   "MemberMobilePhone":"94600240",
   "SMSValidation":1,
   "MemberToken":"",
   "PWDFailRetryCount":10,
   "CheckResult":"0",
   "EmailValidation":1,
   "ResponseCode":0,
   "Type":"Manual",
   "MemberCreatedOn":"2014-12-16 09:33:04",
   "MemberID":49,
   "MemberDefLanguage":"us",
   "CountryCode":"00852",
   "ReadReguFlag":0,
   "MemberCard":[  
      {  
         "OctopusID":"12455678|0",
         "BrandPicGFile":"",
         "CardTypeName":"",
         "CardExpiryDate":"2075-04-01",
         "CardGradePicFile":"/tap/FileManagement/26/20120702_629Tvi.png",
         "MinPointPreAdd":"",
         "MinPointPreTransfer":"",
         "TotalPoints":"1000",
         "CardGrade":"1",
         "BrandID":"1",
         "MaxPointPreTransfer":"0",
         "MemberSex":"2",
         "HomeTelNum":"29504288",
         "BrandPicSFile":"",
         "MemberEngGivenName":"",
         "CountryCode":"852",
         "MaxAmountPreTransfer":"",
         "LaserID":"",
         "MaxAmountPreAdd":"1000.0000",
         "CardNumber":"000100086",
         "CardPointToAmountRate":"100.000000",
         "GracePeriodValue":"",
         "BrandPicMFile":"",
         "MaxPointPreAdd":"",
         "MinAmountPreAdd":"100.0000",
         "MemberRegisterMobile":"85294906037",
         "CardGradeNotes":"",
         "MemberYearofBirth":"1985",
         "GracePeriodUnit":"",
         "MemberIdentityRef":"",
         "MemberEngFamilyName":"",
         "MemberIdentityType":"",
         "MemberMonthofBirth":"6",
         "CardGradeMaxPoint":"",
         "CardTypeID":"",
         "MemberDayofBirth":"1985-06-04",
         "MemberChiFamilyName":"",
         "MemberChiGivenName":"",
         "MinAmountPreTransfer":"",
         "CardStatus":"2",
         "TotalAmount":"1000.00",
         "CardAmountToPointRate":"1.000000",
         "MemberMobilePhone":"94906037",
         "CardAmountExpiryDate":"2013-10-17",
         "CardPointExpiryDate":"2013-10-17",
         "UpdatedOn":"2014-04-17 14:50:10",
         "MemberQRCode":"Q7FM1110339d9b35928d4b8f09235b87a4cd1dd23cf404b"
      }
   ]
}

1 个答案:

答案 0 :(得分:0)

以下是将结果集组合到单个SP中的方法

  1. 假设您的表名是Member(主表)和MemberCard(子表),并且从Member到MemberCard具有一对多关系。
  2. 左联接主表(成员)和子表(成员卡),这将产生单个结果集。这将为每个会员卡产生一行,这意味着如果一个会员有多个会员卡,您将获得重复的会员详细信息;如果会员没有会员卡,您将获得会员卡的空值。
  3. 在您的JAVA api中,您可以对成员进行内存分组,并生成您提到的所需对象结构。

查询存储过程的示例

SELECT 
 M.Action,
 M.CardNumber,
 M.MemberMobilePhone,
 M.SMSValidation,
 M.MemberToken
 --Other fields from Member table
 MC.OctopusID,
 MC.BrandPicGFile,
 MC.CardTypeName,
 MC.CardExpiryDate
 -- Other fields from MemberCard table

FROM Member M
LEFT OUTER JOIN MemberCard MC ON MC.MemberId = M.MemberId
--Your filter conditions etc 

您没有提到合并为单个SP的目的,但是如果您希望减少数据库调用,则可以考虑上述方法。另外,如果结果集较大,则在SQL查询中应用分页。