我已经编写了一个存储过程,如果存在记录将进行更新,否则它将进行插入.它看起来像这样:
update myTable set Col1=@col1, Col2=@col2 where ID=@ID
if @@rowcount = 0
insert into myTable (Col1, Col2) values (@col1, @col2)
Run Code Online (Sandbox Code Playgroud)
我以这种方式编写它的逻辑是更新将使用where子句执行隐式选择,如果返回0,则插入将发生.
这样做的替代方法是进行选择,然后根据返回的行数进行更新或插入.我认为这是低效的,因为如果要进行更新,将导致2次选择(第一次显式选择调用,第二次隐式更新位置).如果proc要进行插入,那么效率就没有差别.
我的逻辑声音在这里吗?这是如何将插入和更新组合到存储过程中的?
今天我正在玩我的一个项目并找到一个有趣的小片段,给定以下模式,你可以安全地清理一个线程,即使它被迫提前关闭.我的项目是一个网络服务器,它为每个客户端生成一个新线程.我发现这对于从远程端提前终止是有用的,但也可以从本地端(我可以.Abort()从我的处理代码中调用).
你有什么问题可以看到,或者你对任何看过类似方法的人提出的建议是什么?
测试用例如下:
using System;
using System.Threading;
class Program
{
static Thread t1 = new Thread(thread1);
static Thread t2 = new Thread(thread2);
public static void Main(string[] args)
{
t1.Start();
t2.Start();
t1.Join();
}
public static void thread1() {
try {
// Do our work here, for this test just look busy.
while(true) {
Thread.Sleep(100);
}
} finally {
Console.WriteLine("We're exiting thread1 cleanly.\n");
// Do any cleanup that might be needed here.
}
}
public static void thread2() {
Thread.Sleep(500); …Run Code Online (Sandbox Code Playgroud)