当我创建临时表时,我通常会确保如果它们存在,我就会删除它们。
IF OBJECT_ID(N'tempdb..#tempTable') IS NOT NULL
DROP TABLE #tempTable
Run Code Online (Sandbox Code Playgroud)
我最近意识到以下方法具有相同的作用:
DROP TABLE IF EXISTS #tempTable
Run Code Online (Sandbox Code Playgroud)
有一种方法比另一种更好吗?
假设我有下表
+---+-------+-------+
| | A | B |
+---+-------+-------+
| 1 | 7,75 | 9,50 |
+---+-------+-------+
| 2 | 9,50 | 10,50 |
+---+-------+-------+
| 3 | 10,50 | 11,50 |
+---+-------+-------+
| 4 | 11,75 | 13,00 |
+---+-------+-------+
| 5 | 13,00 | 14,00 |
+---+-------+-------+
| 6 | 14,00 | 15,25 |
+---+-------+-------+
| 7 | 15,25 | |
+---+-------+-------+
Run Code Online (Sandbox Code Playgroud)
我想在 A 列中找到第一次出现,它与 B 列不匹配,行中的偏移量为 -1(在这种情况下,它应该给我11.75, A4)
我想避免使用VBA.
我尝试使用MATCH,但我不确定如何使用 …
我有以下代码:
using (var connection = new SqlConnection(constr))
{
connection.Open();
var createTempTables = new SqlCommand("[dbo].[p_CreateTempTable]", connection)
{
CommandType = CommandType.StoredProcedure
};
createTempTables.Parameters.Add(new SqlParameter("@Id", 2));
createTempTables.ExecuteNonQuery();
var actualCommand = new SqlCommand("[dbo].[p_Test]", connection)
{
CommandType = CommandType.StoredProcedure
};
var dt = new DataTable();
dt.Load(actualCommand.ExecuteReader());
}
Run Code Online (Sandbox Code Playgroud)
基本上,[dbo].[p_CreateTempTable]从表中选择与 ID inut 匹配的行作为参数。然后,[dbo].[p_Test]只需选择临时表。据我所知,只要连接没有关闭,临时表就应该持续存在,但我总是收到一个异常,说我的临时表不存在。为什么这不起作用?
注意:如果我改为使用在 C# 中创建临时表的文本命令,则程序运行没有问题。但是我想避免这样做,因为在创建临时表时会有更多的验证,并且由于应用程序是 WinForms,我想避免每次逻辑更改时重新编译。