c# – 从数据库中选择不在大型集合中的id,提供的参数太多
作者:互联网
我正在开发一个有两个数据库的应用程序.
我有一个对象,让我们称之为permit,它有一个id引用表的id列表,让我们在另一个数据库中调用那些任务.
我正在尝试运行以下查询:
var listOfUsedIds = select taskid from Permit_Task;
Select * from task where id not in (listOfUsedIds)
当我运行此代码时,我收到错误:
The incoming tabular data stream (TDS) remote procedure call (RPC)
protocol stream is incorrect.
Too many parameters were provided in this RPC request. The maximum
is 2100.
我无法运行子选择或任何事情因为NHibernate不允许我在两个数据库上执行此操作.
任何人都可以帮我解决这个问题吗?
解决方法:
using (var tx = session.BeginTransaction())
{
session.CreateSQLQuery("CREATE TEMP TABLE usedIds (id INT)").ExecuteUpdate();
for (int index = 0; index < ids.Length; index++)
{
// TODO: batch this
session.CreateSQLQuery("INSERT INTO usedIds VALUES (:p" + index + ")")
.SetParameter("p" + index, id)
.ExecuteUpdate();
}
session.CreateSQLQuery("CREATE INDEX usedIds_idx ON usedIds (id)").ExecuteUpdate();
Batch batch;
while((batch.List = session.CreateSQLQuery("SELECT id FROM tasks t WHERE 1 = (SELECT COUNT(*) FROM usedIds u WHERE u.id = t.id) LIMIT 10 OFFSET " + batch.Number).List<int>()).Count > 0)
{
var tasks = session.QueryOver<Task>()
.Where(t => t.Id.IsIn(batch))
.List();
// Do something with the tasks
}
tx.Commit();
}
要么
public TaskMap()
{
Map(x => x.IsUsedCount).Formula("SELECT (SELECT COUNT(*) FROM usedIds u WHERE u.Id = Id)").LazyLoad();
}
var tasks = session.QueryOver<Task>()
.Where(t => t.IsUsedCount == 0)
.List();
标签:c,sql-server,nhibernate,multiple-databases 来源: https://codeday.me/bug/20190709/1418363.html