Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2004
    Posts
    14

    Question Unanswered: set one of the fields to a certain value (was "Need Help with a SQL Problem")

    I am trying to make a query to do the following. Basically I am selecting a record and based on that information set one of the fields of that selection to a certain value. In essence this:

    Select *, (If (Select Value from TableB) = 'T' then set Status = 1) as Status from TableA

    Any help would be greatly appreciated. Thanks in advance for your help and time with this.

  2. #2
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    you want an UPDATE with a WHERE clause. although if you do not know that much, maybe you should not be it. if you do not have this installed, please download and install and read the article on UPDATE. Please also do your boss a favor (if this is not homework) and backup or make a copy of the table you are about to update.
    Last edited by Thrasymachus; 04-10-06 at 14:19.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  3. #3
    Join Date
    Mar 2004
    Posts
    14
    I guess I should have been more specific with my request. My fault. That part in parenthesis was what I need it to do in pseudoish code. I need it to bring back a record and if it matches certain criteria set the selection value to a specific value.

    So in

    Select *, (If (Select Value from TableB) = 'T' then set Status = 1) as Status from TableA

    If the value from the select matches 'T' then the column Status is to be set to 1. No updating or anything like that.

  4. #4
    Join Date
    Apr 2006
    Location
    System.Web.UI.WebControls.Home.BedRoom2
    Posts
    42

    Lightbulb

    Assuming by what you have explained,

    The IF syntax(Books on Line) goes like this:

    IF Boolean_expression
    { sql_statement | statement_block }
    [ ELSE
    { sql_statement | statement_block } ]

    Firstly there is no need for the word THAN(which programming language uses IF THAN ELSE)
    Secondly Boolean_expression cannot be a table of value, not even column something like:
    " IF(Select TOP 1 value from TableB)='T' " would make more sense to me and MSSQL.

    IF that does not work, liked to help.

  5. #5
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,799
    Provided Answers: 11
    Look up the Case (Transact SQL Reference) article in BOL. If you search on "case", it is article 22 for me, anyway.

  6. #6
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    I think all she needs is an update with an inner join.

    I think we can all agree she needs to review the sections on SELECT and UPDATE in Books Online....
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

Posting Permissions

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