如何在 ADO.NET 中将“DEFAULT"指定为 SQL 参数值? [英] How do you specify 'DEFAULT' as a SQL parameter value in ADO.NET?

查看:21
本文介绍了如何在 ADO.NET 中将“DEFAULT"指定为 SQL 参数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个针对 SQL2005 的参数化 SQL 查询,它是在代码中动态创建的,所以我使用 ADO.NET SqlParameter 类将 sql 参数添加到 SqlCommand.

I have a parameterized SQL query targetted for SQL2005 which is dynamically created in code, so I used the ADO.NET SqlParameter class to add sql parameters to SqlCommand.

在上述 SQL 中,我从具有默认值的表值函数中进行选择.我希望我的动态 sql 有时为这些默认参数指定一个值,有时我想指定应该使用 SQL DEFAULT - 如表值函数中定义的那样.

In the aforementioned SQL I select from a Table Valued Function with has defaults. I want my dynamic sql to sometimes specify a value for these default parameters, and other times I want to specify that the SQL DEFAULT - as defined in the Table Valued Function - should be used.

为了保持代码 干净 我不想动态添加 SQL DEFAULT 关键字并在使用非默认值时对其进行参数化,我只是想要将 DEFAULT 设置为我的 SQLParameter 的值.

To keep the code clean I didn't want to dynamically add the SQL DEFAULT keyword and parameterize it when a non-default is to be used, I just wanted to set DEFAULT as the value of my SQLParameter.

我可以吗?在这种情况下,最佳做法是什么?

Can I? What is best practice in such an instance?

推荐答案

SQL 查询参数代替文字值.

SQL query parameters take the place of literal values only.

您不能将 SQL 关键字作为参数的值发送,就像您不能发送表标识符、列标识符、值列表(例如,对于 IN 谓词)或表达.参数的值总是解释为文字值,就好像您在查询中包含了带引号的字符串文字或数字文字一样.

You can't send an SQL keyword as the value of a parameter, just as you cannot send a table identifier, column identifier, list of values (e.g. for an IN predicate), or an expression. The value of the parameter is always interpreted as a literal value, as if you had included a quoted string literal or a numeric literal in your query.

抱歉,您必须在准备查询之前将 SQL 关键字作为 SQL 查询的一部分.

Sorry, but you have to include an SQL keyword as part of the SQL query before you prepare that query.

这篇关于如何在 ADO.NET 中将“DEFAULT"指定为 SQL 参数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆