Otis,
This is a quote from our other SQL Functions thread, which is getting very long and complex - so I broke this thread out.
4) In SQL Server, function calls are broken up into two groups - one returns Scalar values and the other returns a table/resultset. Given ones that return scalar values, is there a way to get the values from those functions without putting the result in a DataTable? For example, lets say I want this query: SELECT GETDATE()
There is no table involved in this and it returns a one row, one column result, perfect for a ExecuteScalar call. Current code to do such a query would be:
Code:
private DateTime GetSQLServerDate()
{
ResultsetFields fields = new ResultsetFields(1);
fields.DefineField(WhatTableFieldIndexHere.FieldName, 0, "TheDate"); //?????
fields[0].ExpressionToApply = new DbFunctionCall("GETDATE", null);
TypedListDAO dao = new TypedListDAO();
//dao.ExecuteScalarQuery()
DataTable results = new DataTable();
dao.GetMultiAsDataTable(fields, results, 1, null, null, null, true, null, null, 0, 0);
if (results.Rows.Count > 0)
return (DateTime)results.Rows[0][0];
return null;
}
what goes in the second line for the field we are defining
Use:
fields.DefineField(new EntityField("TheDate", new DbFunctionCall(...)));
What you gave me for using DefineField - does not work. It generates the following SQL code:
select GETDATE() as TheDate FROM []
which of course results in a SQL error...The correct syntax should be:
select GETDATE() as TheDate
Currently, I am using this code which works...
ResultsetFields fields = new ResultsetFields(1);
fields.DefineField(AnyEntityInProject.AnyFieldName, 0, "TheDate"); //?????
fields[0].ExpressionToApply = new DbFunctionCall("GETDATE", null);