简体   繁体   English

我无法插入SQL Server中的表

[英]I can't insert into table in SQL Server

I created a stored procedure which has two statements insert and update . 我创建了一个存储过程,该存储过程具有两个语句insertupdate

It gets two parameters: 它有两个参数:

@content as nvarchar(10),
@fieldName as int

In Update line everything works correctly, but it doesn't insert to table. 在“ Update行中,所有内容均可正常运行,但不会插入到表中。 Syntax and everything is true but when I execute 语法和一切都是正确的,但是当我执行时

Execute sp_update 432,4

SQL Server shows this error: SQL Server显示此错误:

Msg 207, Level 16, State 1, Line 1 消息207,第16级,状态1,第1行
Invalid column name 'up'. 无效的列名“ up”。
Msg 207, Level 16, State 1, Line 1 消息207,第16级,状态1,第1行
Invalid column name 'Field3'. 无效的列名“ Field3”。

This is my script: 这是我的脚本:

ALTER procedure [dbo].[sp_update]
    @content as nvarchar(10),
    @fieldName as int
as
   declare @ff as varchar
   set @ff = Convert(varchar,@fieldName)

   declare @f as char(7) 
   set @f = 'Field'+ @ff

   declare @sqlupdate varchar(500)
   declare @sqlinserttoChangelog varchar(500)

   set @sqlinserttoChangelog = 'Insert Into dbo.changelog (changeType, fieldname) Values ('+'up'+','+@f+')'
   Exec (@sqlinserttoChangelog)

   set @sqlupdate = 'update TableTest set ' + @f + ' = '+@Content
   Exec (@sqlupdate)

Since you are creating dynamic sql , the values should be wrap with single quote. 由于您正在创建动态sql ,因此值应该用单引号引起来。 To escape single quote, it must be doubled, eg. 要转义单引号,必须将其加倍,例如。

set @sqlinserttoChangelog = 'Insert Into dbo.changelog (changeType, fieldname) Values ('''+'up'+''', '+@f+')'

same with the content content相同

set @sqlupdate = 'update TableTest set ' + @f + ' = '''+ @Content + ''''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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