0

I get an error in the query below. Why ?

DECLARE @Vr varchar(50);
SET @Vr = N'Infos';
IF @Vr = FAR
UPDATE MyTable
SET TAR = @Vr

Error - Invalid column name 'FAR'.

I am using the right database and right table. Then, why does this happen ?

My logic is if Column Far = variable Vr, then update Column TAR.

2
  • 4
    Change the IF @Vr = FAR UPDATE MyTable SET TAR = @Vr to UPDATE MyTable SET TAR = @Vr WHERE FAR = @Vr. Commented Oct 16, 2013 at 22:54
  • If FAR is in MyTable Brad is right, if it is in another table AmirrezaKeshavarz is right Commented Oct 16, 2013 at 22:56

4 Answers 4

2

This is the true code:

DECLARE @Vr varchar(50);
SET @Vr = N'Infos';
IF @Vr = (select far from <table_name>)
Begin
    UPDATE MyTable
    SET TAR = @Vr

END
Sign up to request clarification or add additional context in comments.

3 Comments

No, FAR is supposed to be a column and not a hard-coded string !
I don't want to type a solution... get rid of the if and make it a where criteria on the update.
@Love2Learn - actually, there is a reason why i used if else. there are more conditions involved. I did not want to add those and clutter the question.
2
    DECLARE @Vr varchar(50);
    SET @Vr = N'Infos';
   -- IF @Vr = FAR
    IF ( select Count(*) from mytable where ColumnFar =@Vr) >0
    BEGIN
    UPDATE MyTable
    SET TAR = @Vr where ColumnFar =@Var
    END
 ELSE
 BEGIN
 -- Goes your condition
 END 

Or use CASE

  DECLARE @Vr varchar(50);
   SET @Vr = N'Infos';

UPDATE MyTable
       SET TAR =
                 CASE
                   WHEN ( @Vr = FAR)
                                   THEN 'FAR'
                   Else
                    NULL -- Or any other value you want.
                 END

Comments

0

In your case, you are checking the column name and not that particular column's value. I suggest you try dynamic sql. This link can be useful. As far as I've understood, if your @Vr = 'FAR' then update field TAR else do something else. In that case, see my below query

DECLARE @SQL NVARCHAR(MAX)
DECLARE @valueToBeUpdatedInYourColumn nvarchar(100)
set @valueToBeUpdatedInYourColumn = 'this value will be your updated value'
SET @Vr = 'Infos';
IF @Vr = 'FAR'
   BEGIN
     SET @SQL = '

                 UPDATE MyTable
                 SET TAR= '''+@valueToBeUpdatedInYourColumn+'''
                '
      EXEC(@SQL)
   END
ELSE
   BEGIN
         ...
   END

The number of single quotes ' is very important

Comments

0

You need BEGIN/END for your IF statement

DECLARE @Vr varchar(50);
SET @Vr = N'Infos';

UPDATE MyTable
SET TAR = @Vr
WHERE FAR = @vr

EDIT:

Use a where clause instead of an IF block.

3 Comments

same problem persists.
this is not gonna work because he wants to update all the rows by a condition , not some of the rows .
How do you figure? I don't see how updating the entire table accomplishes something

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.