我正在CLR函数中运行一些C#代码并遇到一些奇怪的行为。
运行以下代码时:
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT * FROM Vertica_GetVerticaServer_vw WHERE verticaServer IS NOT NULL ORDER BY NEWID()";
using (cmd.Connection = GetSQLConnection())
{
cmd.Connection.Open();
SqlDataReader r = cmd.ExecuteReader();
while (r.Read())
{
return r.GetString(0);
}
}
抛出此错误(下面的完整相关堆栈跟踪):
函数中包含的Select语句无法将数据返回给客户端。
但是,当我运行相同的代码时,减去“ORDER BY NEWID()”(用于随机化结果)并在视图内运行“ORDER BY NEWID()”,我没有错误:任何人知道这里发生了什么事吗?
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT * FROM Vertica_GetVerticaServer_vw WHERE verticaServer IS NOT NULL");// ORDER BY NEWID()";
using (cmd.Connection = GetSQLConnection())
{
cmd.Connection.Open();
SqlDataReader r = cmd.ExecuteReader();
while (r.Read())
{
return r.GetString(0);
}
}
throw new Exception("Could not get Vertica Server name");
看起来像一个bug,但也许我错过了关于NEWID()的一些内容?
(注意,我用GETDATE()进行测试,看看它是否是一个确定性的问题,它也有效......)。
完整堆栈跟踪:
---> System.Data.SqlClient.SqlException:函数中包含的Select语句无法将数据返回给客户端。 System.Data.SqlClient.SqlException: 在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔breakConnection) 在System.Data.SqlClient.SqlDataReaderSmi.InternalNextResult(Boolean ignoreNonFatalMessages) 在System.Data.SqlClient.SqlDataReaderSmi.NextResult() 在System.Data.SqlClient.SqlCommand.RunExecuteReaderSmi(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream) 在System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String method,DbAsyncResult result) 在System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String method) 在System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior,String method) 在System.Data.SqlClient.SqlCommand.ExecuteReader() 在Aggregation.AggregateDataManager.GetVerticaServer()
根据要求,这里是CLR TVF的设置:
[Microsoft.SqlServer.Server.SqlFunction(FillRowMethodName = "FillRowAggregates", TableDefinition = "listId int, pricingDate datetime, value decimal", DataAccess=DataAccessKind.Read)]
public static IEnumerable CalculateListAggregatePricing(int listId, DateTime start, DateTime end, int WeightTypeId)
{
DataRequest d = new DataRequest();
d.Start = start;
d.Finish = end;
d.Metric = Metric.GetSharePricingMetric();
d.Metric.Weight = WeightType.Equal;
_listId = listId;
List<ConstituentInfo> ci = new List<ConstituentInfo>();
foreach (int i in AggregateDataManager.GetConstituents(listId))
ci.Add(new ConstituentInfo(i, end));
switch (WeightTypeId)
{
case 0:
EqualWeightInterpreterForSQLCLR e = new EqualWeightInterpreterForSQLCLR();
return e.GetIndex(d, ci, false);
case 1:
MarketCapWeightInterpreterForSQLCLR mc = new MarketCapWeightInterpreterForSQLCLR();
return mc.GetIndex(d, ci, false);
case 2:
PriceWeightInterpreterForSQLCLR p = new PriceWeightInterpreterForSQLCLR();
return p.GetIndex(d, ci, false);
}
throw new Exception("Invalid Weight Type");
}
public static void FillRowAggregates(Object o, out SqlInt32 listId, out SqlDateTime pricingDate, out SqlDecimal value)
{
DataPoint dp = (DataPoint)o;
listId = _listId;
pricingDate = dp.PricingDate;
value = (SqlDecimal)dp.Value;
}
连接由WeightedInterpreters构建。
答案 0 :(得分:0)
你有没有尝试过TOP 99.99999 PERCENT?