I'm trying to do the following in SQL alone. In the end it will end up on a wso2 DSS server but if it can be done in sql alone even better :)
Sudocode
Array results=Array;
result = <sql>select id, query from definitions</sql>
foreach result.query
r=<sql>query</sql>
results.push(r)
I am trying to run a select on table a that returns 2 columns.
One of the two columns is named query, and I want to then execute that query returning
id, query_title, query_text
We can assume that the query column always returns the same columns (through aliases written in the query)
The other option would be doing this in WSO2 DSS however I though at least that it can only do what sql does. Maybe joining with the ESB I could get it if that doesn't work but really my goal would be to do it ALL in sql as I am going to insert insert information and then update it into another table anyway.
You cannot do this with a single select query.
One solution is to do this in two steps. Fetch the query in the application using your SQL and then execute the second query from the application.
The second solution is to use a stored procedure and prepare/execute. How you then fetch the results depends on the nature of the results
Related
I want to get data from several tables which has the same column name in one SQL statement, for example:
SELECT name, age FROM table_a UNION SELECT name, age FROM table_b UNION...
But the table_x may not exists that I can't avoid from people who send the request to me, if one of the tables is not exits in a query it will be failed, is there any syntax to avoid that?
I know a way that I can use show tables to get all tables in the database and compare them to the request parameters first, but I hope I can do it from MySQL syntax.
The short answer is no. If you are using another language in front of it, such PHP or any other language really, you can query the tables as you suggest, but SQL expects the query to be accurate syntactically and if it's not it will error. There is one (IMO bad) way to do this, if you must. You could use a stored procedure, which would allow you to dynamically build the query as you would in PHP or another language, but that's about all you have with MySQL (or any database that I know).
I am developing high load web-application and trying to reduce the quantity of sql queries. Quite often I need to update one row and get the results. I think it would be nice to have possibility to run query and at the same time receive the values of updated fields without making 2 calls of mysql server. For example, I execute the following query:
update table set val=val+1 where id=1;
and function returns:
array("val"=>10)
Sure, I understand, that I can write my own function, which first makes update, than select and returns the result. But the problem is that in such case mysql server will have to seek data, update during first query, than comes the second query, which again requires to seek data and return it. And I am thinking about the way, where mysql seeks data, updates and returns updated data.
I am currently working on a query in Access 2010 and I am trying to get the below query to work. I have the connection string between my local DB and the server that I am passing through to working just fine.
Select column1
, column2
from serverDB.dbo.table1
where column1 in (Select column1 from tbl_Name1)
In this situation table1 is the table on the server that I am passing through to get to, but the tbl_Name1 is the table that is actually in my Access DB that I am trying to use to create constraints on the data that I am pulling from the server.
When I try to run the query, I am getting the error that it doesn't think tbl_Name1 exists.
Any help is appreciated!
I just came across a solution that may help others in a similar situation.
This approach is easy because you can just run one query on your local Access database and get everything you need all at once. However, a lot of filtering/churning-through-results may be done on your own local computer behind the scenes, as opposed to on the remote server, so it may not necessarily be quick.
Steps
Create a query, make it a "Pass Through" query, and set up its "ODBC Connect Str" property to connect to the remote database.
Write the pass through query, something like SELECT RemoteId From RemoteTable and give your pass through query a name, maybe PassThroughQuery
Create a new query, make it a regular "Select" query.
Write your new query, using the pass through query you just created as a table in this new query (seems weird to use a query as a table, but it works) and join that PassThroughQuery "table" to your local table and filter it based on values in the local table, something like SELECT R.RemoteId, L.LocalValue FROM PassThroughQuery R INNER JOIN LocalTable L ON L.LocalId = R.RemoteId where L.LocalValue = 'SomeText'
This approach allows you to mix/join the results of a pass through query and the data in a local Access database table cleanly, albeit potentially slowly if there is a lot of data involved.
I think the issue is that a pass through query is one that is run on the server. Since one of the tables is located on the local Access file, it won't find the table.
Possible workaround if you must stay with the pass-through is you can build an SQL string with the results of the nested query rather than the query string itself (depending on the number of results this may or may not be practical)
e.g. Instead of Select column1 from tbl_Name1 you use "c1result1","c1result2",....
I am executing queries from Access 2010 on an Interbase database via ODBC (Easysoft) ver.7. Everything works fine except when i come to fire a Union query such as this:
SELECT TRIP.TRIPDATE, RESERVATION.BOOKINGREF, RESERVATION.LEADNAME, TRIP.DRIVERID, RESERVATION.STATUS, RESERVATION.DATECANCELLED, TRIP.TRANSPORTTYPEID
FROM TRIP INNER JOIN RESERVATION ON TRIP.TRIPID = RESERVATION.ARRIVALTRIPID
WHERE (((TRIP.TRIPDATE) Between #2/1/2012# And #2/29/2012#) AND ((TRIP.DRIVERID)=2) AND ((RESERVATION.DATECANCELLED) Is Null) AND ((TRIP.TRANSPORTTYPEID)=12))
UNION
SELECT TRIP.TRIPDATE, RESERVATION.BOOKINGREF, RESERVATION.LEADNAME, TRIP.DRIVERID, RESERVATION.STATUS, RESERVATION.DATECANCELLED, TRIP.TRANSPORTTYPEID
FROM TRIP INNER JOIN RESERVATION ON TRIP.TRIPID = RESERVATION.DEPARTURETRIPID
WHERE (((TRIP.TRIPDATE) Between #2/1/2012# And #2/29/2012#) AND ((TRIP.DRIVERID)=2) AND ((RESERVATION.DATECANCELLED) Is Null) AND ((TRIP.TRANSPORTTYPEID)=12));
When I run this query from Access I get
"ODBC --call failed, [Easysoft][Interbase]Dynamic SQL Error, SQL error
code = -104, Token unknown -line1,char 0, ((#-104)"
When running the select queries on their own they work fine but when joined via UNION I get this error.
Any help would be appreciated.
thanks
You don't mention if your query is a passthrough query or if you are using linked ODBC tables in an Access query.
If you are using a normal Access query
When using linked ODBC tables in a normal Access query, the Access data engine will rewrite the queries as necessary to make them compatible with the other database engine.
Sometimes, it can fail though.
Make sure each SELECT query works and returns correct data independently.
Try a simpler UNION query to make sure that the issue comes from the UNION keyword itself.
Try UNION ALL
Try using a pass-through query instead.
If you are using a pass-through query
Pass-through queries are send verbatim to the ODBC engine, and Access just collects the results without rewriting the query itself.
Make sure each SELECT query works as a pass-through query and returns correct data independently.
Make sure that the literal dates are properly formatted for Interbase SQL.
The ones you use are correct for Access SQL, but different databases accept different formats.
Try a simpler UNION query using simple SELECT statements involving 1 or 3 fields only.
Try UNION ALL.
You don't show it in your question, but just in case, if you used an ORDER BY statement, you have to wrap the UNION query.
Try to cast the data types of your fields. It may be that some fields's data are incorrectly interpreted and that the union fails because it assumes that the data retrieved is of different types.
Try using a standard Access query instead.
I have a stored procedure in SQL Server 2008 that consists of multple select statements. When building a report in SSRS, I have a dataset that uses that stored procedure. However, the only fields that show up are the ones that are in the first select statement. Is there a way to show the other fields or use multiple select statements within one stored procedure?
Thanks!
Do the selects output the same schema (i.e. fields)? If so, and if you need all the results, you could try UNIONing the separate queries together. Otherwise, why not move the query you need into a new stored proc and call that from the report?
Eric, it's been my experience that when SSRS is based on a stored procdure, the results of the last Select statement are used and not the first. So you should be able to do anything you want up until the last Select statement in the stored proc and then make sure the last Select contains the correct data/columns for the report.
From the MSDN documentation
If multiple result sets are retrieved
through a single query, only the first
result set is processed, and all other
result sets are ignored. For example,
when you run the following query in
the text-based query designer, only
the result set for Production.Product
appears in the result pane:
SELECT ProductID FROM Production.Product
GO
SELECT ContactID FROM Person.Contact