使用SQL Server Query插入新值时,避免在表中复制值 [英] Avoid Duplicating values inTable when inserting new values using SQL Server Query

查看:89
本文介绍了使用SQL Server Query插入新值时,避免在表中复制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望仅在新的值不在costcode列中存在时才将新的costcode值插入到表中。

如果它存在,那么

TranDate和金额将根据现有的成本代码进行更新。



简单:

如果costCode exsist然后更新tranDate和amount,则为每列插入带有新值的新行。



CostCode TranDate Amount

04 31/01/2013 12:00:00 AM 262033.83

07 31/01/2013 12:00:00 AM 31579.87

01 31 / 01/2013 12:00:00 AM 980834.24

09 31/01/2013 12:00:00 AM 197237.28



我需要帮助紧急。



请有人在这里帮助我。

谢谢

I want to insert new costcode values in to table only if the new value dose not exsist in costcode column already.
if it exsists so
the TranDate and amount will be updated for the exsisting cost code.

Simply:
if costCode exsist then update tranDate and amount else insert new row with new valuse for each column.

CostCode TranDate Amount
04 31/01/2013 12:00:00 AM 262033.83
07 31/01/2013 12:00:00 AM 31579.87
01 31/01/2013 12:00:00 AM 980834.24
09 31/01/2013 12:00:00 AM 197237.28

I need help urgently.

please someone help me here.
thanks

推荐答案

if(存在(从tbl中选择1,其中costcode = yourcode))

开始



你的更新sql;

结束



其他



开始



insert sql



end
if(Exists(select 1 from tbl where costcode=yourcode))
begin

your update sql;
end

else

begin

insert sql

end


查询y我们的表,看看你的成本代码是否存在,如果它确实更新了需要更新的值,如果它没有插入你的新记录。



SQL,



http://www.w3schools.com/sql/default。 asp [ ^ ]
Query your table to see if your cost codes exists if it does then update the values that need to be updated, if it doesnt insert your new record.

SQL,

http://www.w3schools.com/sql/default.asp[^]


这篇关于使用SQL Server Query插入新值时,避免在表中复制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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