如何在insert sql语句中通过参数传递列名
如何在insert sql语句中通过参数传递列名 例如,
@name='name'
insert into employees (id,@name) values(1,'a')
这可能吗?
how to pass column name with parameter in insert sql statment
e.g.
@name='name'
insert into employees (id,@name) values(1,'a')
is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,您不可能需要在应用程序中或通过在 SQL Server 本身中使用动态 SQL 来构建包括该列的 SQL 语句。
编辑:回复:“什么是动态sql”。方法如下。
然而,它有两个问题。第一个 @name 必须是 SQL 注入证明。出于同样的原因,您还希望使用 @Value 的参数。但是,必须提前指定其数据类型,这意味着它并不适合所有列。因此,经过反思,您最好在应用程序层中执行此操作。 (注意:关于 SQL 注入的相同注意事项仍然适用,您的应用程序代码将需要为列添加正确类型的参数)
No it isn't possible you would need to build the SQL Statement including the column either in your application or by using dynamic SQL in SQL Server itself.
Edit: RE: "what's dynamic sql". The approach is as follows.
However there are 2 issues with it. First @name must be SQL Injection proof. For the same reason you would also want to use a parameter for @Value. However the data type for that must be specified in advance meaning that it won't be suitable for all columns. So on reflection you would be better off doing this in your application layer. (NB: The same considerations about SQL injection still apply and your application code will need to add the parameter of the correct type for the column)