如何指定“默认”作为ADO.NET一个SQL参数值? [英] How do you specify 'DEFAULT' as a SQL parameter value in ADO.NET?

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

问题描述

我有一个参数化的SQL查询针对性的是动态的code创建SQL2005,所以我用了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 默认 - 如表值函数定义 - 应该是用了。

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.

要保持code的洁净的我不想动态地添加SQL 默认关键字和参数是当一个非默认使用,我只是想设置默认作为值我的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关键字作为参数的值,就像你不能发送表标识符,列标识符,​​列表中的值(例如对于 predicate)或前pression。该参数的值的总是的PTED为文字值,就好像你已经包含带引号的字符串或一个数字字符在查询中。除$ P $

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查询的的一部分之前的你prepare该查询。

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

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

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