Where条款中的通配符函数在哪里?(Within Where Clause criteria like wildcarded function?)

编程入门 行业动态 更新时间:2024-10-20 07:41:44
Where条款中的通配符函数在哪里?(Within Where Clause criteria like wildcarded function?)

大家下午好,

所以这是我在论坛上的第二个问题,我相信随着我对TSQL及其动态功能的了解越来越多,这个数字将会增加。 如果任何人都可以建议,如果我正在使用最佳实践方法,它也将不胜感激。

这是我写的一个函数,用于确定用户的位置:

dbo.fnGetCOTLState(CURRENT_USER))

在我创建的一个视图中,我有一个where子句,如下所示:

(dbo.OfficersT.ProcessState = dbo.fnGetCOTLState(CURRENT_USER))

由于最近的变化,我需要更新这个更加动态,所以我更新=(等于),如下所示:

(dbo.OfficersT.ProcessState LIKE dbo.fnGetCOTLState(CURRENT_USER))

但是,我意识到,这仍然不足以提取我需要的记录,我必须通配符dbo.fnGetCOTLState函数,并且来自Microsoft访问后台。我尝试了以下语法:

(dbo.OfficersT.ProcessState LIKE '%' & dbo.fnGetCOTLState(CURRENT_USER)) & '%' (dbo.OfficersT.ProcessState LIKE %dbo.fnGetCOTLState(CURRENT_USER))% (dbo.OfficersT.ProcessState LIKE '%dbo.fnGetCOTLState(CURRENT_USER))%'

但他们都没有工作> _ <这很可能是我没有使用正确的话谷歌,但我似乎无法找到任何人尝试在任何地方的功能使用通配符! 不知道这是否是可能的,但如果任何人都可以提供一些我想要达到的信息,像往常一样,我会永远感激!

SeanY

Good afternoon all,

So this is my second question on the forum and I'm sure the number will increase as I am learning more about TSQL and its dynamic capabilities. If anyone can advice if I am using the best practice approach it would also be greatly appreciated.

This is a function I wrote to determine where the user is located:

dbo.fnGetCOTLState(CURRENT_USER))

In one of the view I created, I have a where clause as follow:

(dbo.OfficersT.ProcessState = dbo.fnGetCOTLState(CURRENT_USER))

Due to recent changes I need to update this to a to be more dynamic so I updated the = (equal) to a like as follow:

(dbo.OfficersT.ProcessState LIKE dbo.fnGetCOTLState(CURRENT_USER))

However I realised that this is still not sufficient to extract the records I need, I must wild card the dbo.fnGetCOTLState function and coming from Microsoft access background I have tried the following syntax:

(dbo.OfficersT.ProcessState LIKE '%' & dbo.fnGetCOTLState(CURRENT_USER)) & '%' (dbo.OfficersT.ProcessState LIKE %dbo.fnGetCOTLState(CURRENT_USER))% (dbo.OfficersT.ProcessState LIKE '%dbo.fnGetCOTLState(CURRENT_USER))%'

but none of them worked >_< It is highly probable that I am not using the correct words to google, but I can't seem to find anyone trying to use a wild card on a function anywhere! Not sure if it is even possible, but if anyone can offer some info for what I am trying to achieve, as usual I would be eternally grateful!!

SeanY

最满意答案

你的一个尝试很接近。 改变这个:

(dbo.OfficersT.ProcessState LIKE '%' & dbo.fnGetCOTLState(CURRENT_USER)) & '%'

对此:

dbo.OfficersT.ProcessState LIKE '%' + dbo.fnGetCOTLState(CURRENT_USER) + '%'

SQL Server中的字符串连接运算符是加号( + ),而不是ampsersand。 也许混淆来自你有一个VBA背景,其中&符号实际上用于字符串连接。

One of your attempts was close. Change this:

(dbo.OfficersT.ProcessState LIKE '%' & dbo.fnGetCOTLState(CURRENT_USER)) & '%'

to this:

dbo.OfficersT.ProcessState LIKE '%' + dbo.fnGetCOTLState(CURRENT_USER) + '%'

The string concatenation operator in SQL Server is plus (+), not ampsersand. Perhaps the confusion comes from your having a VBA background, where ampersand in fact is used for string concatenation.

更多推荐

本文发布于:2023-08-01 00:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1350577.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通配符   函数   条款   Clause   wildcarded

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!