sql server stored proc只返回一行

时间:2016-08-03 14:23:32

标签: stored-procedures spring-batch

我遇到的问题是,我的存储过程只将csv文件中的最后一条记录写入数据库。我认为这可能与文件类型(csv或文本)有关,因为我有一个逗号分隔的文本文件,只有五条记录将写入所有记录,但是当我使用csv文件时,只写入最后一条记录。我确实读过一篇文章说这可能与在存储过程中使用标量变量有关,但我不认为这是正确的,因为当我使用文本文件时,它仍然是相同的存储过程。请指教。

谢谢!

这里是存储过程: 使用[SomeDB] GO

/****** Object:  StoredProcedure [dbo].[SaveUser]    Script Date: 8/1/2016 9:25:24 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

Alter PROCEDURE [dbo].[SaveUser]
          -- Add the parameters for the stored procedure here
          @PartnerID INT,
          @SourceID INT,
          @OrgSourcedIDs NVARCHAR(50),
          @Role NVARCHAR(50),
          @UserID NVARCHAR(50),
          @GivenName NVARCHAR(50),
          @FamilyName NVARCHAR(50),
          @Email NVARCHAR(50),
          @Grade NVARCHAR(50),
          @Identifier NVARCHAR(50)
AS
BEGIN
          -- SET NOCOUNT ON added to prevent extra result sets from
          -- interfering with SELECT statements.
          SET NOCOUNT ON;
          DECLARE @ReturnVal NVARCHAR(10)

          DECLARE @IsTransfer Bit = 0
          DECLARE @IsUpdate BIT = 0

          SELECT @IsTransfer =  CASE WHEN OrgSourcedIDs != @OrgSourcedIDs THEN 1 ELSE 0 END,
                            @IsUpdate =  CASE WHEN HASHBYTES('SHA', GivenName +FamilyName +Email +Grade ) != HASHBYTES('SHA', @GivenName + @FamilyName + @Email + @Grade ) THEN 1 ELSE 0 END
          FROM dbo.tblUsers a
          WHERE a.PartnerID = @PartnerID AND a.SourcedID = @SourceID
          IF @@RowCount = 0
          BEGIN
                         -- If not, add it to staging table with ACTION = INSERT (into tblUser)
                         INSERT INTO dbo.tblUsers
                                       ( PartnerID, SourcedID, OrgSourcedIDs, Role, UserID,
                                       GivenName, FamilyName, Email, Grade, Identifier,
                                       Action )
                         VALUES
                                       ( @PartnerID, @SourceID, @OrgSourcedIDs, @Role, @UserID,
                                       @GivenName, @FamilyName, @Email, @Grade, @Identifier,
                                       'Create' )
                         --SELECT @ReturnVal = 'INSERT'
          END
          ELSE IF (@IsTransfer = 1)
          BEGIN
                         UPDATE dbo.tblUsers
                                       SET
                                                      OrgSourcedIDs = @OrgSourcedIDs,
                                                      UserID = @UserID,
                                                      GivenName = @GivenName,
                                                      FamilyName = @FamilyName,
                                                      Email = @Email,
                                                      Grade = @Grade,
                                                      Identifier = @Identifier,
                                                      Action = 'Transfer'
                                       WHERE
                                                      PartnerID = @PartnerID
                                                      AND SourcedID = @SourceID

          END
          ELSE IF(@IsUpdate = 1)
          BEGIN
                                       UPDATE dbo.tblUsers
                                       SET
                                                      UserID = @UserID,
                                                      GivenName = @GivenName,
                                                      FamilyName = @FamilyName,
                                                      Email = @Email,
                                                      Grade = @Grade,
                                                      Identifier = @Identifier,
                                                      Action = 'Update'
                                       WHERE
                                                      PartnerID = @PartnerID
                                                      AND SourcedID = @SourceID
          END



          --SELECT @ReturnVal

END

GO

0 个答案:

没有答案