Case statement and unique index

  • For the proper use of a application I developed, a unique index has to be created on 2 colums(wwoverzcode_2 & wwoactsubcode). However, because this table has several levels(top -> parent -> child -> ....etc -> node) this index has to be manipulated for some records.

    Because my application has to support both Oracle as SQL Sever, I've already thought out the following for my oracle databases:

    create unique index twwo_code on twwo (

       case when (wwoverzcode_2<>'') THEN wwoverzcode_2 ELSE wwocode END asc,

       wwoactsubcode asc

    )

    This works fine!

    Now I want this also to work i.c.w. SQL Server. However when using the code below I get a syntax error on the case statement. What's wrong? Isn't this allowed in SQL Server?

    create unique  index twwo_code on twwo (

       case when (wwoverzcode_2<>'') THEN wwoverzcode_2 ELSE wwocode END,

       wwoactsubcode

    )

    go 

     

  • No it is not valid syntax.

    You could try creating a view and then indexing that.

  • Or you could add a computed column and use that in the index.

  • Is it possible to replace the case statement with anyother syntax that can do the same thing?

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply