sql更新问题 [英] sql Updation Problem

查看:100
本文介绍了sql更新问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,先生,

以下SQL查询是否有任何问题??


Hi Sir,

Is there any Prob with the Below SQL Query.?


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



当前的"Office_Status"为"Finished",但尚未更新.请帮帮我.


谢谢,

Dileep



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


Thanks,

Dileep

推荐答案

如果我阅读的代码不太草率,则可能可以简化和增强已编写的代码.如果尝试下面的语句,它应该执行完全相同的更新,但没有变量和if语句(因为它们嵌入在update语句中):
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):
UPDATE tbl_Office 
SET    Office_Status = 'Viewed'
WHERE  Master_ID = 1
AND    Office_Status = 'Finished';



看起来如果If语句中的测试条件之一不正确,将导致更新语句无法执行.我的猜测是@Office_Status不等于完成".
一种简单的检查方法是在if条件之前添加以下语句以查看变量的值.
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.
SELECT @Status AS Status, @Office_Status AS Office_Status


好吧,问题是当您只声明varchar时,该变量内只能容纳一个字符.

varchar等于varchar(1)

您需要增加尺寸.

我在您的查询中还注意到的另一件事是

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'')



无论如何,您要在查询块中设置@status.那为什么需要它呢?

只需将其设置为



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

just make it as

if( @Office_Status= ''Finished'')


这篇关于sql更新问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆