简体   繁体   中英

sql server stored procedure to update records

I have tried to create a stored procedure that takes values from my program and inserts it into specific columns in a table. I keep getting an error of incorrect syntax in the stored procedure but cannot figure out why? The code from program:

            commandObj.CommandText = "sp_insert_profileDetails"

            commandObj.Parameters.Clear()

            Dim m_param As SqlParameter

            m_param = commandObj.Parameters.Add("@authorId", SqlDbType.NVarChar, 100)
            m_param.Direction = ParameterDirection.Input
            m_param.Value = foundProfiles.Item(i)


            m_param = commandObj.Parameters.Add("@age", SqlDbType.NVarChar, 50)
            m_param.Direction = ParameterDirection.Input
            m_param.Value = faceProfileAge.Item(i)

            m_param = commandObj.Parameters.Add("@gender", SqlDbType.NVarChar, 50)
            m_param.Direction = ParameterDirection.Input
            m_param.Value = faceProfileSex.Item(i)

            m_param = commandObj.Parameters.Add("@locale", SqlDbType.NVarChar, 50)
            m_param.Direction = ParameterDirection.Input
            m_param.Value = faceProfileLocation.Item(i)

            m_param = commandObj.Parameters.Add("@pic", SqlDbType.NVarChar, 100)
            m_param.Direction = ParameterDirection.Input
            m_param.Value = faceProfilePic.Item(i)

            Dim recordsAffected As Integer = commandObj.ExecuteNonQuery
            Return recordsAffected



ALTER procedure [dbo].[sp_insert_profileDetails]
@authorId nvarchar(100),
@age nvarchar(50),
@gender nvarchar(50),
@locale nvarchar(50),
@pic nvarchar(100)

AS 
  Begin
  set nocount on
  update [dbo].ProfileFeed
  set [age] = @age,
      [sex] = @gender,
      [locale] = @locale,
      [pic] = @pic
  where 
      [authorId] = @authorId
  end

Any assistance appreciated?

I'm missing a few lines of code to be sure, but....

Did you specify that the commandObj.CommandType is System.Data.CommandType.StoredProcedure?

If you forget to set this, .NET will treat it as System.Data.CommandType.Text by default, and the database will throw an Incorrect Syntax error..

I don't see where you are actually inserting any records into the database.

The stored procedure states:

  update [dbo].ProfileFeed
  set [age] = @age,
      [sex] = @gender,
      [locale] = @locale,
      [pic] = @pic
  where 
      [authorId] = @authorId
  end

Yet the SP is named [dbo].[sp_insert_profileDetails]

Did you mean to write " insert into [dbo].ProfileFeed " instead of " update "?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM