SQL,具有多个参数的Case [英] SQL, Case with multiple parameters
问题描述
我对sql相对较新,我一直在尝试获得一些我认为我的编程背景没有帮助的东西。请帮我一些例子。
我需要根据是否有2个ID的可能性来构建一个带有字符串串联的return语句
到目前为止,我已经得到了这个:
Hi, I'm relatively new to sql and I've been trying to get something which I think my programming background is not helping. Please help me with some examples.
I need to build a return statement with the concatenation of strings based on the possibilities of having or not 2 IDs
So far I've got this:
SELECT
....
,'ON VESSEL "' + VESSEL.NAME + '" FROM '
+ ORIGIN.NAME + '/' + ORIGIN.COUNTRY_NAME + 'TO'
+ CASE ISNULL(SHIP.TRANSSHIP_PORT_ID_1,0)
WHEN 0 THEN
''
ELSE
PORT1.NAME + '/' + PORT1.COUNTRY_NAME
+ CASE ISNULL(SHIP.TRANSSHIP_PORT_ID_2,0) +
WHEN 0 THEN
''
ELSE
' AND TO '
+ PORT2.NAME + '/' + PORT2.COUNTRY_NAME
END
+ 'IN TRANSIT TO'
END
+ DESTINATION.NAME + '/' + DESTINATION.COUNTRY_NAME
FROM
....
事情是,到目前为止,没有什么能阻止用户删除第一个记录。这会导致逻辑错误。
有没有办法做类似的事情:
The thing is, so far there is nothing stopping the user to delete the first record. Which would cause an error with the logic.
Is there a way to do something like:
CASE condition
WHEN test AND A = B THEN
到目前为止,由于AND,我收到错误...
提前谢谢
So far I get an error due to the "AND"...
Thanks in advance
推荐答案
你可能是得到语法错误接近AND...问题出在test
你需要一个布尔表达式...像test ='Y'
SQL服务器没有布尔数据类型
看看这个链接 http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples - 和 - 解释/ [ ^ ]
或此参考资料 [ ^ ]
You are probably getting a syntax error "near to AND" ... the problem is withtest
You need a Boolean expression in there ... something like "test='Y'"
SQL server does not have a Boolean Data Type
Have a look at this link http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/[^]
or this reference material[^]
这篇关于SQL,具有多个参数的Case的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!