Click here to Skip to main content
15,891,675 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
In My Table I have a table Let's say Student
and this student table has columns Hobies and subject class
Now what i need to do is
SQL
if(Student.class=2 and Student.Hobies like '%basketball%')
Update Student Set Student.Subject='BasketBall'
if(Student.class=2 and Student.Hobies like '%Football%')
Update Student Set Student.Subject='Football'

But it throw an error
SQL
The multi-part identifier "dbo.Student.class" could not be bound.

SQL
The multi-part identifier "dbo.Student.Hobies" could not be bound.

Does anyone have Idea why it is happening
-Thanks
Posted
Updated 13-Oct-14 21:37pm
v3
Comments
Herman<T>.Instance 14-Oct-14 3:29am    
Hobies or Hobbies column?
Vishal Pand3y 14-Oct-14 3:31am    
@digimanus does it matter it's just a typo and yes you are right it is hobies
Herman<T>.Instance 14-Oct-14 4:16am    
I was wondering if the name in he source was spelled incorrect and that might have been a cause..
KaushalJB 14-Oct-14 3:31am    
Are you using any kind of ALIAS for your tables?
Vishal Pand3y 14-Oct-14 3:32am    
yes but all alias are same ie dbo

It will be like this:-
SQL
Update Student Set Subject='BasketBall'
where class=2 and Hobies like '%basketball%'


If you have multiple conditions the use it like this

Update Student Set Subject=(CASE WHEN class=2 and Hobies like '%basketball%' THEN 'BasketBall'
WHEN class=2 and Hobies like '%Football%' THEN 'Football'
END)
 
Share this answer
 
v2
Comments
Vishal Pand3y 14-Oct-14 3:38am    
Actually I have multiple condition so i can't use where
Arora_Ankit 14-Oct-14 3:44am    
Answer updated.
Rajesh waran 14-Oct-14 5:26am    
But u didn't mention that u have multiple condition,arora_ankit solution is right for the question that u have provided here.
Arora_Ankit 14-Oct-14 5:39am    
Yes Raj, that answer was correct and now I improved that for his need too.
SQL
Update Student Set Student.Subject=
( Case 
  When Student.class=2 and Student.Hobies like '%basketball%' Then 'BasketBall'
  When Student.class=2 and Student.Hobies like '%Football%' Then 'Football' Else       Student.Subject END)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900