Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Sir,

Is there any Prob with the Below SQL Query.?


SQL
DECLARE @Status varchar;
DeCLARE @Office_Status varchar;


Set @Status = 'Viewed';
Set @Office_Status= (Select Office_Status From tbl_Office
Where Master_ID = 1);

if (@Status = 'Viewed' AND @Office_Status= 'Finished')

BEGIN
Update tbl_Office Set Office_Status = @Status
Where Master_ID = 1

END


The Current "Office_Status" is "Finished" , But it is not yet Updating. Please Help me.


Thanks,

Dileep
Posted
Comments
Santhosh Kumar Jayaraman 22-Aug-12 12:58pm    
It looks ok. but can you check inside if loop whether its hitting loop by putting a print statement inside if loop

If I'm not reading the code too sloppy, you could perhaps both simplify and enhance the code you have written. If you give the following statement a try, it should do the exact same update but without variables and if statements (since they are embedded in the update statement):
SQL
UPDATE tbl_Office 
SET    Office_Status = 'Viewed'
WHERE  Master_ID = 1
AND    Office_Status = 'Finished';
 
Share this answer
 
Comments
__TR__ 23-Aug-12 2:46am    
My 5.
I had come to improve my solution to include the above update statement. I guess i don't need to include it anymore :)
Wendelius 23-Aug-12 9:00am    
Thanks :)
Hi,
It looks like one of the test conditons in If statement is not true causing the update statement to not get executed. My guees is @Office_Status is not equal to 'Finished'.
An easy way to check it is by adding the below statement before the if condition to see the values of the variables.
SQL
SELECT @Status AS Status, @Office_Status AS Office_Status
 
Share this answer
 
Comments
dilzz 22-Aug-12 13:00pm    
When i add the query as u mentiond then my out put is
Status = N and Office_Status = V

Don't know why these types of datas are shown, Please advice me

Thanks
__TR__ 22-Aug-12 13:05pm    
Try declaring your variables as shown below.
DECLARE @Status varchar(20);
DECLARE @Office_Status varchar(20);
Ok the issue is when you declare just varchar it can hold only one character inside that variable.

varchar is equal to varchar(1)

You need to increase the size.

Also one more thing i noticed in your query is

if (@Status = 'Viewed' AND @Office_Status= 'Finished')


Anyhow you are setting @status inside the query block. So why is it required?

just make it as
if( @Office_Status= 'Finished')
 
Share this answer
 
v2
HI I got the Solution,

In this query i have Delcelear @Office_Status and @Status as varchar,
Then it is take it as only one charector,

When i have DECLARE the variavle as Varchar(50) then it take as the whole word, and it is working fine now.

Thanks for every one..
 
Share this answer
 

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