Select from IBM DB2 using Openquery() and member - sql-server

I want to fetch some data from an IBM DB2 server using SQL Server 2008.
For this I have tried to use:
SELECT * From Openquery(LINKED_SRV,'Select * from dta.Filename')
which gives me data, albeit rather old data.
I have since gathered that the updated data is in another Member on the server, and that the default member is the outdated one I tried to import data from.
I am told the current Member is PROD2017. I have read some IBM DB2 documentation where a Library.Filename(Member) convention is used, but using:
SELECT * From Openquery(LINKED_SRV,'Select * from dta.Filename(PROD2017)')
but this gives me an error that ( is not supported.
Of several attempts which has failed I have also tried to use:
SELECT * From Openquery(LINKED_SRV,'Select * from dta.Filename"(PROD2017)"')
which actually gives a result set, but it is the same result set as the original one omitting the membership, so I'd reckon there is some issue with the Query either way.
How can I find data using openquery() from an IBM DB2 server using Sql server, when the DB2 server uses membership?

Cenderze,
From my SQL server I can query my DB2 machine through a linked server without OPENQUERY. For example:
select t1.ordno from LinkedServer.Database.Library.Table t1
Note the use of aliasing and the 4-part identifier. Replace these with your specifics and let me know what happens.
M

I received a Notable Question award for this question today and I saw that it doesn't answer how I got this issue solved.
Mind you, this is a 3 year old question so my specifics may be a little bit off but I remember it as the following:
I queried a member for all production of 2017, called PROD2017
I understood this as a book analogy, where PROD2017 was the name of the book and that my query returned a chapter. For me, the query returned the wrong chapter (I believe January's sales or something where I wanted August's).
I told the company responsible for the data in the IBM DB2 connection and they had to redo their "sorting of the book", so that the query would return the correct month's data.
I believe they had to do this each month, but once again I'm iffy on the details seeing as how long ago this was.
Hopefully this can help someone, though :)

Related

Azure Data Factory- Referencing Lookup activities in Queries

I'm following a tutorial on Azure Data Factory migration from Azure SQL to Blob through pipelines. While most of the concepts make sense, the 'Copy Data' query is a bit confusing. I have a background in writing Oracle SQL, but Azure SQL on ADF is pretty different and I'm struggling to find specific technical documentation, probably because it's not widely adopted yet.
Pipeline configuration shown below:
Query is posted below:
SELECT data_source_table.PersonID,data_source_table.Name,data_source_table.Age,
CT.SYS_CHANGE_VERSION, SYS_CHANGE_OPERATION
FROM data_source_table
RIGHT OUTER JOIN CHANGETABLE(CHANGES data_source_table,
#{activity('LookupLastChangeTrackingVersionActivity').output.firstRow.SYS_CHANGE_VERSION})
AS CT ON data_source_table.PersonID = CT.PersonID
WHERE CT.SYS_CHANGE_VERSION <=
#{activity('LookupCurrentChangeTrackingVersionActivity').output.firstRow.CurrentChangeTrackingVersion}
Output to the sink Blob as a result of the 'Copy Data' query:
2,name2,14,4,U
7,name7,51,3,I
8,name8,31,5,I
9,name9,38,6,I
Couple questions I had:
There's a lot of external referencing from other activities in the 'Copy Data' query like #{activity('...').output.firstRow.CurrentChangeTrackingVersion. Is there a way to know the appropriate syntax to referencing external activities? Can't find any good documentation the syntax, like what .firstRow is or what the changetable output looks like. I can't replicate this query in SSMS, which makes it a bit of a black box for me.
SYS_CHANGE_OPERATION appears in the SELECT with no table name prefix. Is this directly querying from the table in SourceDataset? (It points to data_source_table, which has table tracking enabled) My main confusion stems from how table tracking information is stored in the enabled tables. Is there a way to show all the table's tracked changes in SSMS? I see some documentation on what the return values, but it's hard for me to visualize it without seeing it on the table, so an output query of some return values would be nice.
LookupLastChangeTracking activity queries in all rows from a table (which when I checked, is just one row), but LookupCurrentChangeTracking activity uses a CHANGE_TRACKING function to pull the version of the data sink in table_store_ChangeTracking_version. Why does it use a function when the data sink's version is already recorded in table_store_ChangeTracking_version?
Sorry for the many questions, but I can't find any way to make this learning curve a bit less steep. Any guides or resources would be awesome!
There is an article to get the same thing done from the UI and it will help you understand it better .
https://learn.microsoft.com/en-us/azure/data-factory/tutorial-incremental-copy-change-tracking-feature-portal .
1 . These are the Lookup activity ,. very straight forward , please read about them here .
https://learn.microsoft.com/en-us/azure/data-factory/control-flow-lookup-activity
2.SYS_CHANGE_OPERATION is a column on data_source_table and so that should be fine . Regarding the details on the how the change tracking (CT) is stored , I am not sure if all the system table are exposed on Azure SQL , but we did had few table on the on-prem version of the SQL which could be queried if needed . But for this exercise I think that will be an over kill .

Unexplained 'Invalid Operation' error in Access query with SQL backend

I am trying to migrate the entire backend of an Access application onto SQL Server. The first part of my project involves moving all of the tables whilst making minimum changes after the migration (no SQL Views, Pass-through queries etc. yet).
I have two queries in particular that I am using here:
ProductionSystemUnAllocatedPurchases - Which executes and returns a resultset successfully.
This is the full formula (sorry its extremely complex) for QtyAvailableOnPurchase:
QtyAvailableOnPurchase: I believe this field could be the problem here?
IIf((IIf([Outstanding Qty]>([P-ORDER-T with Qty Balance]![QTY]-[SumOfQty]),
([P-ORDER-T with Qty Balance]![QTY]-[SumOfQty]),[Outstanding Qty]))>0,
(IIf([Outstanding Qty]>([P-ORDER-T with Qty Balance]![QTY]-[SumOfQty]),([P-
ORDER-T with Qty Balance]![QTY]-[SumOfQty]),[Outstanding Qty])),0)
ProductionSystemUnAllocatedPurchasesTotal - Which gives an 'Invalid Operation' error message
Now the strange thing for me is that the first query works perfectly fine, but the second which uses the first as a source table, gives me this error message when executing. This query works perfectly fine with an access backend, but fails with SQL Server tables.
Any Ideas?
Can QtyAvailableOnPurchase be NULL? That would explain why Sum fails. Use Nz(QtyAvailableOnPurchase,0) instead.
My approach is to decompose queries. Create two queries :
First query selects needed data
Second query applies group operations (e.g. Sum)
You'll get easy way to check every step.
I have managed to find a solution to this error.
It seems that the problem is not so much with the query but rather the data type on SQL Server. SQL Server Migration Assistant (SSMA) automatically maps any Number (Double) fields to float on SQL Server. This mapping needed manually changing to Decimal.
Now according to this SO post, Decimal is the preferred for its precision up to 38 points (which is more than enough for my application), While float allows more than this, the data is stored in approximates.
Source: Difference between numeric, float and decimal in SQL Server

What is the ProgressDB equivalent to SQL Server's statement Select ID = '1'?

When attempting to use a query I wrote against a Progress DB, I assumed I could use the following statement:
SELECT ID = '1' FROM Table
That returns a syntax error, but it's perfectly normal syntax for SQL Server.
Rule #1 -- Progress is not SQL. The more you try to make it act like SQL the unhappier you will be.
There is a SQL-92 interface. Naturally it does not support much in the way of syntax specific to Microsoft or Oracle. "Perfectly normal for MS SQL Server" is no assurance that Progress will think it normal.
There is an older still SQL-89 embedded within the 4GL engine. This is even less likely to behave like MS SQL.
Of course a great deal depends on which of these engines you are using and what version of Progress you are working with.
Having said that -- you have no table name. I'm just a Progress guy and maybe that's some sort of fancy-pants MS SQL magic or a code snippet from the midst of something more interesting but that looks like a problem to me. I would have been coding something more like:
SELECT name FROM customer WHERE id = 1

String concatenation in custom DBMS producing strange results

When trying a simple CONCAT I am getting syntax errors. The connection uses a proprietary ODBC driver, which I'm sure is the issue, but want to know if there is anything I can do from my end. The following is the query I initially tried:
SELECT CONCAT('DF', '01', ServiceCode) AS RecordID
FROM ServiceCodes
However this throws a syntax error near , ServiceCode, no other information. So I tried:
SELECT 'DF' + '01' + ServiceCode AS RecordID
FROM ServiceCodes
But this returns the error term_expr type not supported which I assume is a specific error from the driver.
So then I tried:
SELECT CONCAT(CONCAT('DF', '01'), ServiceCode) AS RecordID
FROM ServiceCodes
Which seems to have worked! Returning a string like DF01MOT. This works across all servers and so I added extra fields producing the query:
SELECT CONCAT(CONCAT('DF', '01'), ServiceCode) AS RecordID, 'DF' AS Environment
FROM ServiceCodes
This also seems to have worked, except on 1 server. When running the above query on this one server it will now return DF0DFMO instead of DF01MOT, last 2 chars change in each row depending on ServiceCode, adding further fields changes this again such as:
SELECT CONCAT(CONCAT('DF', '01'), ServiceCode) AS RecordID, 'DF' AS Environment, '01' AS SO
FROM ServiceCodes
This returns DF0DF01 for this field in ALL rows.
So why would this be happening? Why is it only on one server? Driver is the same on all servers but is this an issue with the driver? What can I do to get around it?
EDIT: To avoid confusion I have modified the title and tags. This is actually accessing a custom DBMS provided by a third party from with a DTS package in SQL Server Enterprise Manager. I do believe contacting the third party is the only option.
Having seen your comments, the SQL Server tag is actually a bit misleading here. You are running on a vendor DB using their driver. Clearly their DB supports a CONCAT function that only takes two arguments, rather than the SQL Server 2012 function which can take more. That explains your first issue.
As for the other results you are seeing lower down, you'd need to raise this with the vendor. The version of SQL Server you're using for DTS doesn't really have any relevance here.

Linked Informix table in MS SQL Server ignoring criteria?

I’m having a problem with a linked Informix table in MS SQL Server 2008r2. When I query this table, it seems to ignore some of the criteria I’m passing to it but not others. For example if I put a condition on the rowdate field the remote query part of the execution plan does not show any WHERE clause but if I put criteria on another field such as ACD it does show.
It seems it does not pass any criteria on the rowdate field but does on all others.
I know the field is indexed on the Informix side. If it helps the table I’m linking is from Avaya CMS and it is linked via the OpenLink ODBC driver.
EDIT:
As far as I know it is Informix Dynamic Server 2000 and it is on Solaris. The column comes up as a DATE data type which is correct. I have tried passing the criteria as ‘2010-08-03 00:00:00’, ‘2010-08-03’, CONVERT(date,’2010-08-03’) and a few more variations. When the data is returned to SQL server it is in the format yyyy-mm-dd.
When I view the execution plan I can see the remote query with all the other criteria followed by a filter for only the rowdate field.
I know that rowdate is indexed and that the driver does normally communicate that information as we use it in other applications (Business objects and MS Access) and they don’t have a problem
I managed to figure it out but it is the strangest thing ever. I went down the route of passing the date in different formats. My default is to use the normal YYYY-MM-DD that of course did not work so I tried YYYY-MMM-DD, still nothing. After going through LOTS of combinations I found one that works Mmm-DD-YY and it has to be exactly that! SEP-21-2010 wont work but Sep-21-2010.
I wonder if this is just a strange hang up from Informix or something in the driver, anyway it works.
On a side note has anyone noticed how strange it is that people from the America write the date month, day year? Stop and think about it for a second, do you say the number 2410 as “Four hundred, ten, two thousand”? The best part about it is try asking yourself this, what day is American independence day? Most Americans say “That’s easy you limey person it is the 4th of July” hmmmm day month (year), the only date they say round the right way is the date they got their independence. I will leave it up to the SO community to see the irony in that
Example query below:
select *
from OPENQUERY (AVAYA, 'select row_date,starttime,intrvl,acd from root.hagent where
row_date = ''NOV-22-2012'' and acd = 1 and split = 1 and starttime = 1900')
By the way I managed to extract accurate data via both MMM-DD-YYYY and Mmm-DD-YYYY.

Resources