Azure移动服务自定义API多次调用SQL SP
我有一个自定义API的Azure移动服务,它似乎在进行多个SQL调用。 当我查看日志时,我看到从SP返回的多个响应。 其中一些是空的返回(没有logging集),而其中一个是正确的与SP进行通信,并返回一个logging集。
我知道我的iOS应用程序只调用一次自定义API。
这是自定义API:
exports.post = function(request, response) { var mssql = request.service.mssql; var params = [request.query.FirstName , request.query.LastName , request.query.DOB , request.query.EmailAddress , request.query.PhoneNumber , request.query.FacilityID , request.query.DiagnosisID, request.query.GeneratedBy , request.query.UserTypeID]; console.log("processregistration params = '%j'", params); var sql = "exec MyStoredProcName ?, ?, ?, ?, ?, ?, ?, ?, ?"; mssql.query(sql, params, { success: function(results) { console.log("results = '%j'", results); // I had to put this in there to keep it from returning an empty recordset // back to my iOS app if (results.length > 0) { response.send(statusCodes.OK, results); } } }); };
这里是日志:
我知道SP只产生一个logging集,并直接testing它产生一个logging集。 我已经能够帮助它,但在response.send
之前join一个if语句。 我以前从来没有这样做过。 有任何想法吗?
编辑 – 这是我的SP(张贴为carlosfigueira)
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO /* ========================================================================================== Author: LJ Wilson Create date: 2013-12-07 Description: Processes a new (or existing) user registration ========================================================================================== Revision History Date Initials Comments 2013-12-07 LJW Created 2013-12-18 LJW Fixed issue with InvitationCodeID */ ALTER PROCEDURE [MyApp].[sp_Process_Registration] @FirstName VARCHAR(500) , @LastName VARCHAR(500) , @DOB DATETIME , @EmailAddress VARCHAR(500) , @PhoneNumber VARCHAR(25) , @FacilityID INT , @DiagnosisID INT , @GeneratedBy NVARCHAR(500) , @UserTypeID INT AS DECLARE @Timestamp AS DATETIME = CURRENT_TIMESTAMP DECLARE @ExistingUserID AS INT = ( SELECT ID FROM MyApp.Users WHERE UserTypeID = @UserTypeID AND EmailAddress = @EmailAddress AND LastName = @LastName ) DECLARE @EmailAddressInUse AS INT = ( SELECT COUNT(ID) FROM MyApp.Users WHERE EmailAddress = @EmailAddress ) IF @ExistingUserID IS NULL BEGIN BEGIN TRAN INSERT INTO MyApp.Users ( LoginName , FriendlyName , JoinDate , PhoneNumber , EmailAddress , UserTypeID , IsActive , FacilitiesListID , AllowTexting , AllowEmail , AllowCalls , AllowAPNS , ImagePath , ShowImage , LastUpdatedBy , LastUpdatedOn , FirstName , LastName , DOB ) VALUES ( @EmailAddress , -- LoginName - nvarchar(250) NULL , -- FriendlyName - nvarchar(500) @Timestamp , -- JoinDate - datetime @PhoneNumber , -- PhoneNumber - nvarchar(50) @EmailAddress , -- EmailAddress - nvarchar(500) @UserTypeID , -- UserTypeID - smallint 1 , -- IsActive - bit NULL , -- FacilitiesListID - int NULL , -- AllowTexting - bit 1 , -- AllowEmail - bit NULL , -- AllowCalls - bit 1 , -- AllowAPNS - bit NULL , -- ImagePath - nvarchar(1000) NULL , -- ShowImage - bit @GeneratedBy , -- LastUpdatedBy - nvarchar(500) @TimeStamp , -- LastUpdatedOn - datetime @FirstName , -- FirstName - varchar(500) @LastName , -- LastName - varchar(500) @DOB -- DOB - datetime ) COMMIT TRAN SET @ExistingUserID = ( SELECT @@IDENTITY ) END BEGIN DECLARE @InvitationCode AS VARCHAR(50) = ( SELECT dbo.fnc_GenInvitationCode(@EmailAddress, @FacilityID, @DiagnosisID) ) INSERT INTO MyApp.InvitationCodes ( InvitationCode , UserID , FacilityID , InvitationGeneratedOn , GeneratedBy , DiagnosisIdentifier , InvitationActive ) VALUES ( @InvitationCode , -- InvitationCode - nvarchar(50) @ExistingUserID , -- UserID - int @FacilityID , -- FacilityID - int @Timestamp , -- InvitationGeneratedOn - datetime @GeneratedBy , -- GeneratedBy - nvarchar(500) @DiagnosisID , -- DiagnosisIdentifier - int NULL -- InvitationActive - bit ) END DECLARE @InvitationCodeID AS INT = ( SELECT ID FROM MyApp.InvitationCodes WHERE InvitationCode = @InvitationCode AND InvitationGeneratedOn = @Timestamp ) INSERT INTO MyApp.FacilitiesList ( UserID , InvitationCodeID , FacilityID , AddedOn , AddedBy ) VALUES ( @ExistingUserID , -- UserID - int @InvitationCodeID , -- InvitationCodeID - int @FacilityID , -- FacilityID - int @Timestamp , -- AddedOn - datetime @GeneratedBy -- AddedBy - nvarchar(500) ) SELECT @InvitationCode AS InvitationCode , @EmailAddressInUse AS EmailAddressInUse , @ExistingUserID AS ExistingUserID GO
您遇到的问题是您的存储过程正在执行大量的中间操作(查询现有用户ID,查询使用的电子邮件地址等),默认情况下,所有这些操作都作为结果集返回SQL服务器,这就是为什么你在“真实”之前看到那些空的结果集的原因。
您可以在存储过程中使用SET NOCOUNT ON
语句,这将有效地从返回到mssql.query
函数的结果集列表中删除那些中间SQL执行。 你的SPROC看起来像这样
ALTER PROCEDURE [carlosfigueira].[sp_Process_Registration] @FirstName VARCHAR(500) , @LastName VARCHAR(500) , @DOB DATETIME , @EmailAddress VARCHAR(500) , @PhoneNumber VARCHAR(25) , @FacilityID INT , @DiagnosisID INT , @GeneratedBy NVARCHAR(500) , @UserTypeID INT AS SET NOCOUNT ON DECLARE @Timestamp AS DATETIME = CURRENT_TIMESTAMP -- rest of the stored proc code is the same
一旦添加了该语句,传递给mssql.query
的callback将只被调用一次。