Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2004
    Posts
    162

    Unanswered: Set variable value in CASE statement

    Is it possible to set a value to a variable if a case statement is true?

    like this:
    CASE
    WHEN Utable.type = 2 THEN U.Username2 + (@Uname= 2)
    WHEN Utable.type = 3 THEN U.Username3 + (@Uname= 3)

    Ive tried above and:
    "+ (select @Uname = 2)"
    "+ (SET @uname = 2)"
    "; SET @uname = 2"
    ...

    and several other things but non works.

    variable's = INT
    "Utable.type" = INT
    "U.Username"2/3 = nvarchar(50)


    Thx in advance

  2. #2
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,800
    Provided Answers: 11
    You have to remember that CASE is not a branching of code, it simply returns a variable. It looks like you are trying to return a record set and set a variable at the same time. I would have to think of a better way to do that, but I believe it will be two separate queries.

  3. #3
    Join Date
    Mar 2004
    Posts
    162
    thanks
    thats my thought to but it would be smoth to do it in one query

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •