scalikejdbc and MySQL subquery WHERE IN (SELECT statement) - scalikejdbc

It looks like scalikejdbc doesn't support MySQL's WHERE IN (SELECT statement) queries, as IN requires a sequence of items as a parameter, it doesn't accept a query.
Any ideas for making it work?
Thank you!

Turns out sqls.in() supports SQLSyntax as a second parameter.

Related

SSRS: Underlying SQL uses 'union all' -- results are much smaller set than expected -- plus better way than Tablix-filtering

I am using SSRS, Visual Studio 2015, with underlying TSQL. The SSRS uses 2016/01/reportdefinition.
I need to filter on a parameter (using a tablix filter with a 'like' option) with an underlying SQL statement:
SELECT col1 as result1, col2 as result2 FROM table1
UNION all
SELECT colA as result1, colB as result2 FROM table 2
order by result1
The Tablix like statement is:
="*" + Parameters!Result1.Value + "*"
I would assume that the parameter would cause an underlying where clause to be applied to both the SQL statements in the above UNION all, but I don't know that for sure. [This assumption is not correct: see answers below]
The SQL server for the project is SQL Server 2008 R2, 2012 or 2014.
In any event, when I run the query with the where statements in SSMS [v 17.7], it is very fast and it returns 12 results.
But when I run the query in the rdl-file preview for SSRS, it is extremely slow. I am therefore using a:
Set Rowcount 1000
statement. However, it only returns 1 result, instead of 12. If I remove the "Set Rowcount" option, nothing usable happens, and the "Loading" message just has a spinning icon.
Does anyone have any insight how the queries are modified to filter for the parameter,and why fewer results are returned than expected?
Possibility: Does it run a query without the where, limit the results to 1000, and then, depending on the ordering involved, exclude several of the potential result-matches? [This possibility is actually what happens, see answers below]
NOTE: There is a much better way than what is asked about in the original question. Instead of tablix-filtering, one can do the filter in the SQL where query. See answer from OP.
The query is not modified at all. The dataset query runs and returns all results just the same as if you had run the query in SSMS. If you have a filter on your tablix then that filter only applies to that tablix. You could have another report item such as a tablix or a chart even pointing to the same dataset each with their own filters.
Your query could be using a bad plan, you should be able to test this easily by adding OPTION (RECOMPILE) to the end of your dataset query. This will force the plan to be recreated. Don't set the rowcount or you won't get a true test.
If your dataset query is still running slowly in SSRS, then try putting a trace on the connection using Profiler (from the SSMS tools menu) and check that the SQL being executed is what you expect.
if that doesn't help, please post the full query so it can be assessed (I'm assuming that your actual query is not what you stated in your question...)
While the answer by Alan Schofield is complete for the question, there is a better way than that in the original question.
It is by far a better alternative to match the parameter.
To do this, one would use the following xml syntax in the appropriate place in the rdl-file:
<DataSets>
<DataSet Name="DataSet1">
<Query>
<DataSourceName>MyDataSource</DataSourceName>
<QueryParameters>
<QueryParameter Name="#Result">
<Value>=Parameters!Result.Value</Value>
</QueryParameter>
</QueryParameters>
<CommandText>
--------------------------------------------------------------------
use MyDataSource;
--------------------------------------------------------------------
select col1 as result1, col2 as result2 from table1 a
where col1 LIKE '%' + #Result + '%'
Union all
select colA as result1, colB as result2 from table2 b
where colA LIKE '%' + #Result + '%'
order by result1,
--------------------------------------------------------------------</CommandText>
<rd:UseGenericDesigner>true</rd:UseGenericDesigner>
</Query>

Run two SQL queries with one variable

My SQL knowledge is pretty limited and this seems like something that should be easy, but I can't figure it out.
I have two queries that I run with the same where clause. Previously, I've pasted the number in the where clause twice. I thought there had to be an easier way so I found that you can do variables.
My problem is when declaring the variable in the first query, it forgets it and doesn't run it for the second query. The terminology may be off on a lot of what I've explain, but the simple query below should explain it better. The second query fails and says
must declare the scalar variable #p_repid
which I have declared in the first query.
DECLARE #p_repid int=3427115
select fldRBuddyId, count(fldRBuddyId) "Repeats"
from tblMsgsOnAir_Type8 typ8
where fldCBuddyId = #p_repid
group by fldRBuddyID
having count(fldRBuddyId) > 1
order by "Repeats" desc
go
select FLDREPID,moa.FLDTGBID,tt.FLDNAME, count(*) "Count"
from TBLMSGSONAIR_1 moa
join TBLTOWERS_1 tt on moa.FLDTGBID=tt.FLDTGBID
where fldrepid = #p_repid
group by FLDREPID,moa.FLDTGBID,tt.FLDNAME
order by "Count" desc
go
Thanks in advance for any help.
Remove the GO between the two statements. That's MS SQL Server's batch separator.
If you use "GO" command all declared variables are gone.
Try removing the "GO" which is in the middle.

How to see HQL final query

I'm trying to run the following query and getting a null pointer exception. I've verified that WHERE lockedTimestamp <= (GETDATE() - CAST('00:06' AS datetime) is valid msSQL because I can run it manually and it'll work fine. It appears HQL (Hibernate Query Language) may not like it. Any thoughts on how I can go about debugging this?
Query query = this.em.createQuery("SELECT c FROM Content c WHERE lockedTimestamp <= (GETDATE() - CAST('00:06' AS datetime))");
Why not use a much more obvious and intuitive calculation, e.g.
SELECT c FROM Content c WHERE lockedTimestamp <= DATEADD(MINUTE, -6, GETDATE());
? I gather this isn't valid in JPQL either, but more for the general query itself in case other users stumble across this question...
As an aside, why not put this query in a stored procedure? Surely hibernate can call a stored procedure, then it doesn't need to try to interpret or reverse engineer your query.
EntityManager.createQuery expects a query string in JPQL (not HQL, and not SQL). JPQL does not define a CAST function.
I'd compute the timestamp limit in Java, and pass it as parameter to the query:
Date limit = ...;
this.em.createQuery("SELECT c FROM Content c WHERE lockedTimestamp <= ?")
.setParameter(0, limit);
To debug sql, you can show the queries that launches Hibernate by adding the configuration hibernate.show_sql=true in the hibernate configuration properties.
The queries will be shown in console, and it's the exact query that hibernate make to the database.

How to check if I'm currently the database owner for SQL 2000/2005/2008

I can't remember how to do this in a TSQL query. It was a one-liner, good for testing before running DML. The query was something similar to SELECT IS_DBO() or SELECT IS(DBO).
You're probably looking for the IS_MEMBER function:
SELECT IS_MEMBER('db_owner')

Using Parameters in MS Reporting Services (SQL Server 2008) against an ODBC data source

I writing a report in Visual Studio that takes a user input parameter and runs against an ODBC datasource. I would like to write the query manually and have reporting services replace part of the where clause with the parameter value before sending it to the database. What seems to be happening is that the #parmName I am assuming will be replaced is actually being sent as part of the SQL statement. Am I missing a configuration setting somewhere or is this simply not possible?
I am not using the filter option in the tool because this appears to bring back the full dataset from the database and do the filtering on the SQL Server.
It sounds like you'll need to treat the SQL Statement as an expression. For example:
="Select col1, col2 from table 1 Where col3 = " & Parameters!Param1.Value
If the where clause is a string you would need to do the following:
="Select col1, col2 from table 1 Where col3 = '" & Parameters!Param1.Value & "'"
Important: Do not use line breaks in your SQL expression. If you do you will get an error.
Holla back if you need any more assistance.
Doesn't ODBC use the old "?" syntax for parameters? Try this:
select col1, col2 from table1 where col3 = ?
The order of your parameters becomes important then, but it's less vulnerable to SQL injection than simply appending the parameter value.
Encountered same problem trying to query an access database via ODBC.
My original query: SELECT A.1 FROM A WHERE A.1 = #parameter resulted in error. Altered to: SELECT A.1 FROM A WHERE A.1 = ?.
You then have to map the query parameter with your report parameter.
I am a bit confused about this question, if you are looking for simple parameter usage then the notation is :*paramName* , however if you want to structurally change the WHERE clause (as you might in sql+ using ?) then you should really be using custom code within the report to define a function that returns the required sql for the query.
Unfortunately, when using custom code, parameters cannot be referenced directly in the generated query but have to have there values concatenated into the resultant String, thus introducing the potential for SQL injection.

Resources