Run ODBC select statement from Management Studio - sql

We have a linked server connecting to IBM iSeries server. Usually, it is no problem using the linked server. However, I came across a query that I can't run locally on Management studio because it is using IBM functions and grouping capabilities like:
Functions:
Trim
CVTDATE
Grouping in select statement (at field level), not in group by:
SELECT SUM(BOOKEDAMT('*CONACTIV', GLPDJC.GLDBK, GLPDJC.GLDPJ, GLPDJC.GLDEC1, GLPDJC.GLDRN1, GLPDJC.GLDJB, GLPDJC.GLDGL, GLPDJC.GLDCC, GLPDJC.GLDRN2, GLPDJC.GLDBT, GLPDJC.GLDBE, GLPDJC.GLDBL, 9, ((XCVTDATE(DATE (SUBSTRING({d '2017-06-30' }, 1, 10))))), GLPDJC.GLDEDT))
,field2
,field3
FROM .....
GROUP BY field2 field3
Question: How do I run this query locally? Is it possible? I am not sure what the query is exactly doing in order to re-write it and make it run-able.

You could use OPENQUERY to pass the query along to the remote server.
https://learn.microsoft.com/en-us/sql/t-sql/functions/openquery-transact-sql

Related

SQL Query to Get DB2 Version on IBM i

I would like to find out which version of DB2 we are running on our IBM i server using only SQL SELECT.
I am executing my queries via installed ODBC drivers for i Access. The places I am executing the queries are Excel-ODBC and Excel-Microsoft Query (simply because I am not a developer and therefore don't have/don't know of another place to run queries).
The following solutions do not work for me:
How to check db2 version
Get DB2 instance name using SQL
Basic reasons why I have failed to get the above solutions to work:
I do not have a SYSPROC table/have access to SYSPROC table
SYSIBMADM table does not contain a ENV_INST_INFO table.
I think these answers may be tailored to those using IBM z, but I use IBM i.
My end goal is to be able to execute a SQL SELECT and get the version of DB2 used on our server.
Try this:
SELECT RELEASE_LEVEL, TEXT_DESCRIPTION
FROM QSYS2.SOFTWARE_PRODUCT_INFO
WHERE PRODUCT_ID = '5770SS1'
AND PRODUCT_OPTION = '27'
--or this instead of the above line:
--AND LOAD_TYPE = 'CODE' AND PRODUCT_OPTION = '*BASE'

Oracle CTE failing in one computer

I have queries created in Microsoft Query to run in Excel with VBA.
They work in different computers but there's one computer where it doesn't work.
In that computer the queries still work except the ones that use CTEs.
A normal query like the following works:
SELECT
TBL.COL
FROM
DB.TBL TBL;
But when it has a subquery (CTE) like the following:
WITH
SUBQUERY AS (
SELECT
TBL.COL
FROM
DB.TBL TBL
)
SELECT
SUBQUERY.COL
FROM
SUBQUERY;
It runs but doesn't retrieve any data.
It doesn't even show the column name like it would if it worked but had 0 records returned.
The query shows the warning message:
SQL Query can't be represented graphically. Continue anyway?
Which is normal and shows in any computer, but it also shows another warning message after:
SQL statement executed successfully.
Which only appears in that computer when it doesn't work.
I need to be able to use them for the queries that I have made.
Using temporary tables would maybe work but I don't have the permissions required to try.
I tried using inline views but they duplicate the data.
I have queries created in Microsoft Query to run in Excel with VBA.
... but there's one computer where it doesn't work.
Common table expressions (i.e., the WITH clause) were not introduced until release 9 of the database. Since ODBC is involved (Microsoft Query), the most likely reason for your situation is that the computer that does not work has an out-dated (pre-release 9) version of the Oracle Client installed.
Compare the Oracle Client installations between a client computer that works and one that does not, to find whether this is the case. If it is, upgrade the Oracle Client on the problematic machine.
I think you can use...
SELECT
SUBQUERY.COL
FROM
(
SELECT
TBL.COL AS COL --or (TBL.COL COL) or ( COL ) #if not duplicate with any
FROM
DB.TBL TBL
) SUBQUERY;

How to use SUM and GROUP BY(SQL) with Visual FoxPro in VB.NET?

I do not have much experience with VB. I have built a VB app that can retrieve data from a table(FoxPro). The code is given below:
Dim dBaseCommand As New System.Data.OleDb.OleDbCommand("SELECT * FROM inventory", con)
Dim dBaseDataReader As System.Data.OleDb.OleDbDataReader = dBaseCommand.ExecuteReader(CommandBehavior.SequentialAccess)
But I need to run the following SQL instead of SELECT * FROM inventory
SELECT itemnumber, mfgr, SUM(qty) AS cqty, unitcost, description
FROM inventory
GROUP BY itemnumber
I replaced the SQL and it does not work. How can I implement that SQL or GROUP BY statement into my VB code?
Update:
The SQL was a sample sql from MySQL. But I have updated the SQL that I am now actually using in my VB app. It does not show any error or does not close the app itself and it does not generate the file which is supposed to be produced by this app. If I use the sql from first sample code, it generates the file nicely from that table.
I went to database explorer(data sources) to run this query too. The following issues came up. Any ideas? not supported by Foxpro or any other workarounds?
Executing SQL: "GROUP BY clause is missing or invalid"
Verify SQL syntax: This command is not supported by this provider
Does this have something to do with this link where group by is not there?
If I use ODBC data provider, will it support the SQL?
Update 2:
It looks like ODBC driver supports GROUP BY. But Microsoft recommends to use OLEDB data provider instead of ODBC which does not support GROUP BY. In Visual basic data connections, I dont see ODBC as available data provider. It has only SQL server data provider and OLEDB data provider. Is there any way so that I can use the ODBC data provider and then use the GROUP BY statement?
*My reputation did not allow me including more references for citing the last information.
I had to change the SQL to the following code and it worked:
SELECT itemnumber, SUM(qty) AS qty, description
FROM inventory
GROUP BY itemnumber, description
From VFP 8, SELECT with GROUP BY clause can select only columns which are associated with either GROUP BY or fields with Aggregate functions. If we need to use GROUP BY clause, we need to be careful with those conditions. So if the "description" from GROUP BY clause is removed, then the SQL does not work. Similarly, if SUM function is removed from qty column, the SQL wont work.
These conditions explain why "GROUP BY clause is missing or invalid" popped up. The good thing is that we can verify the queries on Visual studio before embedding into application.
Remove the single quotes from around Inventory, to make the statement:
SELECT partnumber, SUM(quantity) FROM inventory GROUP BY partnumber
Inventory is a SQL object, not a text literal.

SELECT without a TABLE (JDBC)

In most sql databases I have seen you can do something like:
SELECT ABS(-2.4)
and I get back 2.4. Notice there is no FROM clause.
When I try to do this in OpenEdge via Squirrel and the JDBC driver I get a syntax error. Is there a way to run SELECT statements like this (sans FROM clause) via JDBC?
Single row/column Dual equivalent; SYSPROGRESS.SYSCALCTABLE

Linked server with dot in name

I have a query like this:
SELECT PID,surname,forenames,othername,date_of_birth FROM OPENQUERY(compact,'SELECT PID,surname,forenames,othername,date_of_birth FROM Order.PERSONS')
This is run from SQL Studio Manager. Order.Persons is an Oracle database. This query works as intended. Is it possible to do this:
SELECT PID,surname,forenames,othername,date_of_birth FROM OPENQUERY(compact.world,'SELECT PID,surname,forenames,othername,date_of_birth FROM Order.PERSONS')
i.e. the change the linked server name to compact.world.
Try using [compact.world] instead of compact.world.