开发者

Setting a variable with select inside coalesce

开发者 https://www.devze.com 2023-01-13 01:43 出处:网络
How do I fix 开发者_JAVA技巧up this part of my stored procedure? The select will either return 1, 0, or null. If the select returns 1 or 0, I want @override to be set to that value. If it returns nul

How do I fix 开发者_JAVA技巧up this part of my stored procedure?

The select will either return 1, 0, or null. If the select returns 1 or 0, I want @override to be set to that value. If it returns null, then I want @override to be set to 1.

Something is wrong with my syntax; I am told "incorrect syntax near 'select'" and "incorrect sytax near ')'".

Here is the sql:

set @override = (coalesce(select override from groupPreferences g inner join
preferences p on g.preferenceId = p.preferenceId where groupId = 13
and description = 'myDescription'), 1))


set @override = (coalesce((select override from groupPreferences g inner join
preferences p on g.preferenceId = p.preferenceId where groupId = 13
and description = 'myDescription'), 1))


I'd go for something readable like this:

select @override = override 
from groupPreferences g 
    inner join preferences p on g.preferenceId = p.preferenceId 
where groupId = 13
    and description = 'myDescription'

SET @override = ISNULL(@override, 1)

But you can do:

SELECT @override = ISNULL((select override 
from groupPreferences g 
    inner join preferences p on g.preferenceId = p.preferenceId 
where groupId = 13
    and description = 'myDescription'), 1)


Got the answer- I was missing a ( after the word coalesce.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号