用于更新记录的sql server存储过程
我尝试创建一个存储过程,从程序中获取值并将其插入表中的特定列中。 我在存储过程中不断遇到语法错误,但无法找出原因? 程序中的代码:用于更新记录的sql server存储过程,sql,sql-server-2005,Sql,Sql Server 2005,我尝试创建一个存储过程,从程序中获取值并将其插入表中的特定列中。 我在存储过程中不断遇到语法错误,但无法找出原因? 程序中的代码: commandObj.CommandText = "sp_insert_profileDetails" commandObj.Parameters.Clear() Dim m_param As SqlParameter m_param = commandObj.Pa
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
感谢您的帮助?我确实少了几行代码,但是 您是否指定commandObj.CommandType为System.Data.CommandType.StoredProcess
如果忘记设置,默认情况下,.NET会将其视为System.Data.CommandType.Text,数据库将抛出错误的语法错误 我看不出您实际上在哪里将任何记录插入数据库 存储过程声明:
update [dbo].ProfileFeed
set [age] = @age,
[sex] = @gender,
[locale] = @locale,
[pic] = @pic
where
[authorId] = @authorId
end
然而,SP的名称为[dbo].[SP\u insert\u profileDetails]
您的意思是将“
插入[dbo].ProfileFeed
”而不是“更新
”?发生了什么错误?你能把它复制粘贴到这里吗?与你的问题无关,但MS不鼓励在存储过程前加上“sp_”:这是个棘手的问题,除非你确切知道自己在做什么以及为什么,否则你真的不想在过程前加上“sp_”。SQL会对所有sp_uu过程进行一些额外的处理,随着时间的推移,这可能会使您陷入困境。(详细的解释可以在BOL中找到。)谢谢Chris和Phillip…将记住这一点并更改它。我确实忘记了做那件事,让我再试一次…谢谢..将发回我的荣幸。很高兴能为您提供帮助。我不是在尝试添加新记录,而是在尝试更新现有记录列。因此authorId匹配的地方就是那些列需要更新的地方。