Is there any way I can do
Select TableName.SomeColumn As SomeAlias FROM TableName
In Subsonic v2.2
How about:
DAL.DB.Select(DAL.MyTable.MyColumn.QualifiedName + " AS SomeAlias")
.From<DAL.MyTable>()
....
Related
I have a table that has ID varchar(255) as PK. I have managed to create an auto increment temporary column rowNumber to use with a SELECT, as follows:
SELECT
(#cnt := #cnt + 1) AS rowNumber, ID
from table
CROSS JOIN (SELECT #cnt := 0) AS n
where
(
some conditions
)
)
and date > {ts '2020-08-06 08:51:23.08'}
ORDER BY ID
LIMIT 10
;
How can the above query be expressed in JOOQ? Especially, the (#cnt := #cnt + 1) AS rowNumber part.
SQL variable support in jOOQ
jOOQ currently (version 3.13) doesn't support session variables like the ones you've shown. There's a pending feature request for this: https://github.com/jOOQ/jOOQ/issues/2558
Whenever jOOQ doesn't support a vendor specific feature, you can use it via plain SQL templating, instead. Write:
ctx
.select(
field("(#cnt := #cnt + 1)", SQLDataType.BIGINT).as("rowNumber"),
TABLE.ID)
.from(TABLE)
.crossJoin(table(select(field("#cnt := 0", SQLDataType.BIGINT))).as("n"))
.where(...)
.orderBy(TABLE.ID)
.limit(10)
.fetch();
Assuming, as always, this static import:
import static org.jooq.impl.DSL.*;
Starting from MySQL 8
Just a side-note for future visitors who might be using MySQL 8: In those cases, you'd be using the DSL.rowNumber() window function instead of the approach using variables
Is it possible to return both objects of a join as the result of a GridGain cache query?
We can get either one of the sides of the join or fields from both (and then use these to retrieve each object separately), but looking at the examples and documentation there seems to be no way to get both objects.
Thanks!
#dejan- In GridGain and Apache Ignite, you can use _key and _val functionality with SqlFieldsQuery in order to return an object. For example -
SqlFieldsQuery sql = new SqlFieldsQuery(
"select a._key, a._val, b._val from SomeTypeA a, SomeTypeB b " +
"where a.id = b.otherId");
try (QueryCursor<List<?>> cursor = cache.query(sql) {
for (List<?> row : cursor)
System.out.println("Row: " + row);
}
Note that in this case the object will be returned in a Serialized form.
First of all, GridGain Open Source edition is now Apache Ignite, so I would recommend switching.
In Ignite, you can return exactly the fields you need from a query using SqlFieldsQuery, like so:
SqlFieldsQuery sql = new SqlFieldsQuery(
"select fieldA1, fieldA2, fieldB3 from SomeTypeA a, SomeTypeB b " +
"where a.id = b.otherId");
try (QueryCursor<List<?>> cursor = cache.query(sql) {
for (List<?> row : cursor)
System.out.println("Row: " + row);
}
In GridGain open source edtion, you can use GridGain fields query APIs as well.
Below are two queries. I get correct returns in Access but NO returns in SQL. Is my syntax in the SQL version wrong? Curiously, even if I leave out the second part of the WHERE statement, the returned values don't make sense (i.e. last names = tblx.Last Name = Hull / tbly.Last Name = Morris)...Any ideas?
--SQL 2005
SELECT tblx.[Last Name], tblx.[First Name]
FROM tblx cross join tbly
WHERE (tblx.[Last Name] Like '%[tbly].[Last Name]%') AND (tblx.[First Name] Like '%Right([tbly].[First Name],3) %')
--Access 2007
SELECT tblx.[Last Name], tblx.[First Name]
FROM tblx cross join tbly
WHERE (((tblx.[Last Name]) Like "" & [tbly].[Last Name] & "") AND ((tblx.[First Name]) Like "" & Right([tbly].[First Name],3) & ""))
It should be:
SELECT tblx.[Last Name], tblx.[First Name] FROM tblx cross join tbly WHERE (((tblx.[Last Name]) Like '%' + [tbly].[Last Name]+'%') AND ((tblx.[First Name]) Like '%' + Right([tbly].[First Name],3) +'%'))
Same for Acess sql.
I am using the SubSonic SimpleRepository template for my application. I have created an ASP .NET WebForms project in VS2010 pointing to a SQL 2000 database.
I am having an issue where SubSonic is always using nvarchar in the parameterized queries instead of varchar. This causes SQL to do an Index Scan instead of an Index Seek. I have taken the SQL from the Profiler and altered it to make the parameters varchar like the table's fields and it executes very quickly (<1 second versus 8 seconds).
SubSonic Query from Profiler
exec sp_executesql N'SELECT [t0].[ADDRESS_L1], [t0].[ADDRESS_L2], [t0].[ADDRESS_L3], [t0].[CITY], [t0].[COUNTRY] FROM [aveadmin].[SAPADD] AS t0 WHERE (([t0].[SITE_ID] = #p0) AND ((([t0].[ADDRESS_TYPE] = #p1) AND 1 <> 0) OR (([t0].[ADDRESS_TYPE] = #p2) AND 0 <> 0)))', N'#p0 nvarchar(16),#p1 nvarchar(2),#p2 nvarchar(2)', #p0 = N'BCF8A0A27E543EE1', #p1 = N'00', #p2 = N'03'
Manually Modified Query
exec sp_executesql N'SELECT [t0].[ADDRESS_L1], [t0].[ADDRESS_L2], [t0].[ADDRESS_L3], [t0].[CITY], [t0].[COUNTRY] FROM [aveadmin].[SAPADD] AS t0 WHERE (([t0].[SITE_ID] = #p0) AND ((([t0].[ADDRESS_TYPE] = #p1) AND 1 <> 0) OR (([t0].[ADDRESS_TYPE] = #p2) AND 0 <> 0)))', N'#p0 varchar(16),#p1 varchar(2),#p2 varchar(2)', #p0 = N'BCF8A0A27E543EE1', #p1 = N'00', #p2 = N'03'
The SITE_ID and ADDRESS_TYPE are varchars. Is there a way to force the query to use varchar instead of nvarchar?
Is there a way to force the query to
use varchar instead of nvarchar?
You will have to modify the source code of SubSonic to change this behavior.
[Previous answer is correct -- further details follow.]
The use of nvarchar is hard-coded in a function called GetNativeType() in Sql2005Schema.cs. it's easy enough to patch and rebuild. Hey, this is OSS! This is what the source code is for!
Here is the code.
case DbType.AnsiString:
case DbType.AnsiStringFixedLength:
case DbType.String:
case DbType.StringFixedLength:
return "nvarchar";
In theory, the generated code (see SQLServer.ttinclude) actually does generate DbType.AnsiString and DbType.String as separate types. It should be possible to split the switch statement and generate "varchar" for one and "nvarchar" for the other. Maybe the author thought it wouldn't matter. I suggest you give it a try (but it may break the unit tests).
Is it possible to do a join to a nested select statement in SubSonic 2.2? I am having trouble with this because I have to do a group by...
I.E.
select conn_company.company_name, SUM(t.quote_grand_total) as 'SUM' from conn_company
inner join
(SELECT *
FROM [dbo].[QuoteDBAll]
where [dbo].[QuoteDBAll].[quote_status_id] = 1
AND [dbo].[QuoteDBAll].[quote_ca_modified_date] BETWEEN '12/1/2000' AND '12/1/2009'
) t
on conn_company.company_id = t.company_id
group by company_name
having company_name = 'JACK'
Unfortunately you're not going to be able to convert that SQL into SubSonic 2 syntax. I would suggest you create a sql view based on your query and get SubSonic to generate a model from that instead.