存储过程不插入新行

时间:2014-05-20 16:45:47

标签: sql sql-server vb.net stored-procedures

我有一个存储过程应该在表中为应用程序中的每个表中的每一行插入一个新行,而是连接这些值并将它们存储在SQL Server中的一行中。

奇怪的是,我有另一个存储过程,它完全按照它应该做的那样,占用表单中的每一行,并将每一行作为唯一行插入SQL Server表。

对于我的生活,我无法找到两者在哪里/如何创造不同的输出 - 两者都应该表现相同。

这是有效的存储过程。

子:

Private Sub AddHOMEFunds(ByVal HomeID As Integer)
    clearHOMEFunds(HomeID)
    Dim i As Integer
    For i = 0 To Request.Form.Count - 1
        If InStr(Request.Form.Keys(i), "HOMEprojectName") > 0 Then
            Dim connProperties As String = Globals.connstring
            Dim sql As String = "spAddHOMEFunds"
            Dim objConn As New SqlConnection(connProperties)
            objConn.Open()
            Dim cmd As New SqlCommand(sql, objConn)
            With cmd
                .CommandType = Data.CommandType.StoredProcedure
                .Parameters.AddWithValue("@HOMEID", HomeID)
                .Parameters.AddWithValue("@HOMEprojectName", Request.Form.Item(i))
                .Parameters.AddWithValue("@HOMEfiscalYear", Request.Form.Item(i + 1))
                .Parameters.AddWithValue("@HOMEfundingReceived", Request.Form.Item(i + 2))
                .Parameters.AddWithValue("@HOMEunitsDeveloped", Request.Form.Item(i + 3))
                .Parameters.AddWithValue("@HOMEremainingFunds", Request.Form.Item(i + 4))
                .Parameters.AddWithValue("@HOMEfinalDraw", Request.Form.Item(i + 5))
            End With
            cmd.ExecuteNonQuery()
            objConn.Close()
        End If
    Next
End Sub

存储过程:

USE [DB]
GO
/****** Object:  StoredProcedure [dbo].[spAddHOMEFunds]    Script Date: 05/20/2014 11:25:53 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spAddHOMEFunds]
            @HOMEID int
           ,@HOMEprojectName varchar(max)
           ,@HOMEfiscalYear varchar(max)
           ,@HOMEfundingReceived varchar(max)
           ,@HOMEunitsDeveloped varchar(max)
           ,@HOMEremainingFunds varchar(max)
           ,@HOMEfinalDraw varchar(max)
AS
BEGIN   
    SET NOCOUNT ON;
    INSERT INTO [dbo].[HOMEFunds]
           ([HOMEID]
           ,[HOMEprojectName]
           ,[HOMEfiscalYear]
           ,[HOMEfundingReceived]
           ,[HOMEunitsDeveloped]
           ,[HOMEremainingFunds]
           ,[HOMEfinalDraw])
     VALUES
            (@HOMEID
           ,@HOMEprojectName
           ,@HOMEfiscalYear
           ,@HOMEfundingReceived
           ,@HOMEunitsDeveloped
           ,@HOMEremainingFunds
           ,@HOMEfinalDraw)

END

这是存储过程不起作用。

子:

Private Sub AddHOMEFundsReceived(ByVal HomeID As Integer)
    clearHOMEFundsReceived(HomeID)
    Dim i As Integer
    For i = 0 To Request.Form.Count - 1
        If InStr(Request.Form.Keys(i), "FundsReceivedProjectName") > 0 Then
            Dim connProperties As String = Globals.connstring
            Dim sql As String = "spAddHOMEFundsReceived"
            Dim objConn As New SqlConnection(connProperties)
            objConn.Open()
            Dim cmd As New SqlCommand(sql, objConn)
            With cmd
                .CommandType = Data.CommandType.StoredProcedure
                .Parameters.AddWithValue("@HOMEID", HomeID)
                .Parameters.AddWithValue("@FundsReceivedProjectName", Request.Form.Item(i))
                .Parameters.AddWithValue("@FundsReceivedYearReceived", Request.Form.Item(i + 1))
                .Parameters.AddWithValue("@FundsReceivedSource", Request.Form.Item(i + 2))
                .Parameters.AddWithValue("@FundsReceivedAmount", Request.Form.Item(i + 3))
                .Parameters.AddWithValue("@FundsReceivedOutcome", Request.Form.Item(i + 4))
                .Parameters.AddWithValue("@FundsReceivedFundsRemaining", Request.Form.Item(i + 5))
                .Parameters.AddWithValue("@FundsReceivedPercentRemaining", Request.Form.Item(i + 6))
            End With
            cmd.ExecuteNonQuery()
            objConn.Close()
        End If
    Next
End Sub

存储过程:

USE [DB]
GO
/****** Object:  StoredProcedure [dbo].[spAddHOMEFundsReceived]    Script Date: 05/20/2014 10:44:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spAddHOMEFundsReceived]
            @HOMEID int
           ,@FundsReceivedProjectName varchar(max)
           ,@FundsReceivedYearReceived varchar(max)
           ,@FundsReceivedSource varchar(max)
           ,@FundsReceivedAmount varchar(max)
           ,@FundsReceivedOutcome varchar(max)
           ,@FundsReceivedFundsRemaining varchar(max)
           ,@FundsReceivedPercentRemaining varchar(max)
AS
BEGIN   
    SET NOCOUNT ON;
    INSERT INTO [dbo].[HOMEFundsReceived]
           ([HOMEID]
           ,[FundsReceivedProjectName]
           ,[FundsReceivedYearReceived]
           ,[FundsReceivedSource]
           ,[FundsReceivedAmount]
           ,[FundsReceivedOutcome]
           ,[FundsReceivedFundsRemaining]
           ,[FundsReceivedPercentRemaining])
    VALUES  (@HOMEID
           ,@FundsReceivedProjectName 
           ,@FundsReceivedYearReceived 
           ,@FundsReceivedSource 
           ,@FundsReceivedAmount 
           ,@FundsReceivedOutcome 
           ,@FundsReceivedFundsRemaining 
           ,@FundsReceivedPercentRemaining)
END

同样,工作存储过程为我的应用程序上的每个表单行创建一个新行。

不起作用的存储过程是连接并为SQL Server中的一个表行中的每个表单行创建逗号分隔值。

简而言之,我需要无法正常工作的存储过程与工作存储过程完全相同。

任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

事实证明问题不在于存储过程或子调用存储过程。

问题是表中输入的ID标记是重复的,导致调用该ID将该列中的所有单元格值合并在一起。很奇怪,我没有意识到这甚至是可能的,但我确实看到了这种方法的一些用处。

尽管如此,将这一个划分为胖指编码错误。