单个查询“单选按钮” UPDATE 的选择行为
我在桌面应用程序(Windows Forms C# 4)中使用 MS SQL CE 3.5,不太可能更改数据库。有时我会在精简版的更有限的 SQL 语法中遇到困难。
目前我需要两个查询,但想将它们合并为一个:
UPDATE manganames SET display=0 WHERE id=@id AND display=1
UPDATE manganames SET display=1 WHERE id=@id AND name=@name
显示数据类型 BIT
谢谢,我的问题很简短,而不是完全清楚
最终查询如下
UPDATE manganames
SET display = CASE WHEN name=@name THEN 1 ELSE 0 END< br> WHERE id=@id
PS 我的 stackoverflow 格式化技巧很糟糕。
I am using MS SQL CE 3.5 in an desktop application (Windows Forms C# 4) and is unlikely to change database. I sometimes hit a brick wall in the more limited SQL syntax of Compact Edition.
Currently I need two queries but would like to join them into one:
UPDATE manganames SET display=0 WHERE id=@id AND display=1
UPDATE manganames SET display=1 WHERE id=@id AND name=@name
display is datatype BIT
Thank you, my question was brief rather than completely clear
Final query follows
UPDATE manganames
SET display = CASE WHEN name=@name THEN 1 ELSE 0 END
WHERE id=@id
PS My stackoverflow formating skills sucks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试以下方法
有几个条件您没有解释。首先,当display = 1且name = name时会发生什么?根据您所显示的内容,0 和 1 都会被分配。在我的示例中,将应用第一个 WHEN 子句,并且将分配 0 来显示。
二、显示<>时会发生什么1、姓名>> @姓名?在我的示例中,ELSE 条件会将显示分配给显示,这不会导致任何更改。
Here's something you can try
There are a couple of conditions that you don't explain. First, what should happen when display = 1 and name = name? With what you show, both 0 and 1 would be assigned. In my example, the first WHEN clause would apply and 0 will be assigned to display.
Second, what should happen when display <> 1 and name <> @name? In my example, the ELSE condition will assign display to display, which results in no change.