【发布时间】:2012-10-01 07:56:39
【问题描述】:
我正在研究 NHibernate 标准,我根据输入参数逐渐构建 upp。
我在这些参数的邮政部分遇到了一些问题。 由于我们得到了一个 5 位数的邮政编码,因此输入参数是一个 int,但由于我们在数据库中也接受外国邮政编码,因此数据库将其保存为字符串。
我试图在 NHibernate Criteria/Criterion 中复制的是以下 where 子句。
WHERE
11182 <=
(case when this_.SendInformation = 0 AND dbo.IsInteger(this_.Zipcode) = 1 then
CAST(REPLACE(this_.Zipcode, ' ', '') AS int)
when this_.SendInformation = 1 AND dbo.IsInteger(this_.WorkZipcode) = 1 then
CAST(REPLACE(this_.WorkZipcode, ' ', '') AS int)
when this_.SendInformation = 2 AND dbo.IsInteger(this_.InvoiceZipcode) = 1 then
CAST(REPLACE(this_.InvoiceZipcode, ' ', '') AS int)
else
NULL
end)
我们所做的是检查成员联系人 (this_) 首选将信息发送到的位置,然后我们根据列是否可转换为 int (@987654323) 将输入的邮政编码作为整数与三个不同的列进行对比@函数)如果列不可转换,我们将边标记为NULL
在这种情况下,我们只检查邮政编码是否 >= 输入参数(在 sql 代码中反转,因为参数是第一个),目标是做一个 between(用 'AND' 语句包裹的 2 个子句),>= 或
更新
有一点成功的迹象。
Projections.SqlProjection("(CASE when SendInformation = 0 AND dbo.IsInteger(Zipcode) = 1 then CAST(REPLACE(Zipcode, ' ', '') AS int) when SendInformation = 1 AND dbo.IsInteger(WorkZipcode) = 1 then CAST(REPLACE(WorkZipcode, ' ', '') AS int) when SendInformation = 2 AND dbo.IsInteger(InvoiceZipcode) = 1 then CAST(REPLACE(InvoiceZipcode, ' ', '') AS int) else NULL END)"
, new[] { "SendInformation", "Zipcode", "WorkZipcode", "InvoiceZipcode" },
new[] { NHibernateUtil.Int32, NHibernateUtil.String, NHibernateUtil.String, NHibernateUtil.String });
将我的整个子句放在 Projections.SqlProjection 中,但是当我运行我的代码时,我的一些投影被剪切(“AS int)否则 NULL END)”被从末尾剪切)并使 sql 损坏。 这有什么限制吗?
【问题讨论】:
-
尽管我不是 T-SQL 的粉丝,但这可能是使用 SQL 函数封装逻辑然后使用公式映射属性或使用sqlprojection,但调用函数(我不知道 sql 投影中 sql 长度的限制)
标签: sql nhibernate case criteria where