如果不存在其他更新插入 [英] if exists update else insert

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

问题描述

我想知道下一块code是正确的:

I want to know if the next piece of code is correct:

 SqlCommand cmd = new SqlCommand(
   "IF NOT EXISTS(SELECT count(*) from Raspunsuri where id_intrebare=2)" +
   "Insert INTO Raspunsuri VALUES(@raspuns,@cnp,@data,2,@ip,@idsesiune)" +
   "else" +
   "UPDATE Raspunsuri SET raspuns=@raspuns,cod_numeric_personal=@cnp,data_raspuns=@data,id_intrebare=2,ip_user=@ip,id_sesiune=@idsesiune WHERE id_intrebare=2", con);

所有的参数是正确的,我想插入但似乎这片code的没有做,你有什么建议在插入或update.Do?它是一个SQL查询使用C#结合..

All the parameters are correct that I want to insert but it seems this piece of code doesn't do the insert or update.Do you have any suggestions?it's a sql query combined with c#..

推荐答案

检查该公司由命令创建的字符串:有些话需要它们之间的空格。

Inspect the string that's created by that command: some words need spaces between them.

 SqlCommand cmd = new SqlCommand("IF NOT EXISTS(SELECT 1 from Raspunsuri where id_intrebare=2)" +
                " Insert INTO Raspunsuri VALUES(@raspuns,@cnp,@data,2,@ip,@idsesiune)" +
                " else" +
                " UPDATE Raspunsuri SET raspuns=@raspuns,cod_numeric_personal=@cnp,data_raspuns=@data,id_intrebare=2,ip_user=@ip,id_sesiune=@idsesiune WHERE id_intrebare=2", con);

这篇关于如果不存在其他更新插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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