SQL 存储过程中将用户输入附加到 table 并防止 sql 注入的正确方法

Proper way in SQL stored procedure to append user input to a table and preventing sql injection

我在 SQL 中有一个 table,其中有一个 varchar(max) 字段,它基本上是一个日志,由最终用户使用自由格式条目(从网页)更新。

如何防止 SQL 注入?或者也许已经是?

create proc stored_procedure
@input varchar(max)
as

update mytable
set logfield = logfield + '  ' + @input;

似乎没问题 (SQL Server 2012)。但我能读到的所有内容都坚持认为,即使在参数化存储过程中,你也不应该连接用户输入。

是否有不同的方法来附加用户输入?

SQL 注入只会发生在动态 SQL 上(在 SQL 服务器端或应用程序端)。您的代码不易受到 SQL 注入的攻击。但是,您正在更新 table 中的所有行。您应该使用 WHERE 子句限制更新。