Aliases

May 2, 2012 at 7:08 PM
Edited May 2, 2012 at 7:40 PM

I can't find any documentation on how to use aliases for column names or table names, can somebody point me in the right direction?  I know I can use .As("tablealias") but I when I'm specifying Fields do I do it like this?:

DBQuery.Select()
.Field("CustomerID").As("CustID")
.Field("CustomerName").As("CustName")
.From("Profile").As("p")
May 2, 2012 at 10:29 PM

My example works, I just answered my own question.  Now I need to figure out how to concatenate to fields into one, e.g. 

.Field(DB.User.FirstName + " " + DB.User.LastName).As("SalesRep")

But with code that works.

Coordinator
May 3, 2012 at 11:06 AM

This should work for SQL server

 DBQuery concat = DBQuery.Select(DBField.Field("FirstName") + DBConst.Const(" ") + DBField.Field("LastName")).As("FullName")
                                .From("Employees");

Breaks database agnostic(ity?) as other DB engines don't support this overload.
There is also a DBFunction.Function method that will accept multiple clause parameters if you need to use this 

May 3, 2012 at 4:40 PM
Edited May 3, 2012 at 4:40 PM

The resultant SQL contains this, which obviously results in an error.  I'm not sure what to do. 

SELECT [FirstNamePerceiveit.Data.Query.DBConstRefLastName] AS [SalesRep]

Coordinator
May 4, 2012 at 8:49 AM

Influent - Use a DBField rather than a string as this is just concatenating strings in your executing code.

DBField.Field(DB.User.FirstName) + DBConst.String(" ") + DBField.Field(DB.User.LastName)

This will then be built into a concatenation on the SQL string.