Importing OLAP metadata in SQL Server via linked server results in out-of-range date - sql

Currently, I am trying to extract metadata from an OLAP cube in SQL Server (via a linked server) using this simple query:
select *
into [dbo].[columns_metadata]
from openquery([LINKED_SERVER], '
select *
from $System.TMSCHEMA_COLUMNS
')
But in the result set, there is a column named RefreshedTime with values 31.12.1699 00:00:00.
Because of this value, the query gives this error message:
Msg 8114, Level 16, State 9, Line 1 Error converting data type (null)
to datetime.
The problem is that I need to run the query without specifying the columns in the SELECT statement.
Do you know a trick to avoid this error?

I know you wanted not to have to mention the columns explicitly, but in case nobody can suggest a way to have it handle the 1699-12-31 dates, then you can fallback to this:
select *
into [dbo].[columns_metadata]
from openquery([LINKED_SERVER], '
SELECT [ID]
,[TableID]
,[ExplicitName]
,[InferredName]
,[ExplicitDataType]
,[InferredDataType]
,[DataCategory]
,[Description]
,[IsHidden]
,[State]
,[IsUnique]
,[IsKey]
,[IsNullable]
,[Alignment]
,[TableDetailPosition]
,[IsDefaultLabel]
,[IsDefaultImage]
,[SummarizeBy]
,[ColumnStorageID]
,[Type]
,[SourceColumn]
,[ColumnOriginID]
,[Expression]
,[FormatString]
,[IsAvailableInMDX]
,[SortByColumnID]
,[AttributeHierarchyID]
,[ModifiedTime]
,[StructureModifiedTime]
,CStr([RefreshedTime]) as [RefreshedTime]
,[SystemFlags]
,[KeepUniqueRows]
,[DisplayOrdinal]
,[ErrorMessage]
,[SourceProviderType]
,[DisplayFolder]
from $System.TMSCHEMA_COLUMNS
')

Related

Divide by zero error when converting Access to SQL Server query

I am trying to convert an Access query to one that works in SQL server. The original query in Access works perfectly well (just terribly slow).
I only changed things slightly to make it compatible with SQL server instead of Access, like changing "NOW()" to "GETDATE()" and we can no longer divide aliases.
Running this query in SQL Server:
SELECT batches.[price-group],
[development].verifier,
Count([development].company) AS SENT,
Sum([order] *- 1) AS ORDS,
Count([development].company) / Sum([order] *- 1) AS PCT
FROM [development]
INNER JOIN batches
ON [development].batch = batches.batch
WHERE (( ( [development].[mail-date] ) < Getdate() - 50 ))
GROUP BY batches.[price-group],
[development].verifier
HAVING (( ( batches.[price-group] ) = 'pgb' ))
ORDER BY batches.[price-group],
[development].verifier,
Count([development].company) DESC;
Returns this error:
Msg 8134, Level 16, State 1, Line 1 Divide by zero error encountered.
Only real change, was like I said, in Access we could do this
[ords] / [sent] AS PCT
Any help will be appreciated, I'm not sure exactly why it isn't working! Removing the converted line above, does work in SQL server without any errors.
Thank you!
Use NULLIF():
Count([development].company) / NULLIF(Sum([order] * -1), 0) AS PCT

Data conversion or data mapping error HY000 in multi row concatenation in sub select in view

I have a sub-select as part of a view that generates a concatenated list of attributes from sub table. When doing a select from the view the data displays fine, but if it do a select in the concatenated list field I sometimes get the error Select or omit error on field.
AS400: V7R1M0
Main enitity:
MSGOCCID : CHAR 20
other fields.....
The detail entity fields (MSDPF):
MSGOCCID : CHAR 20
OCC : Integer 4
FIELDVAL : VARCHAR 128
I got the code for the sub select from StackOverflow and modified it for my situation. (I have also tried the recursive select but it very slow. The XMLSERIALIZE route is executable.)
To try and solve the problem it have added and removed REPLACE,COALESCE and TRIM of the value. I have added CCSID 1208 to the cast, changed the CHAR cast to VARCHAR... not change. (I have change CCSID 1208 to CCSID 37 at one stage to see if that will not solve it.)
If I run the select:
select MSGOCCID, COALESCE(DETLIST,' ')
from WBVIEW MSGP
where 1=1
If returns all the rows with not errors.
Sample output:
MSGOCCID: 2019020443165590
MSGDLIST: 14620, 1, C20180914023575582, 4179792C, C20180914023575582, 4179792C, WIPSTOCK, REMOVE, ROU07561
But if I use:
select MSGOCCID, COALESCE(DETLIST,' ')
from WBVIEW MSGP where 1=1 and DETLIST like '%4179792C%'
It fails with on the IBM backend:
Data conversion or data mapping error.
Select or omit error on field MapXmlData(Cast(Concat(',
',Strip(Replace(Cast(P6MSDPF_4.FIELDVAL AS VarChar(128) CCSID
1208),'-'), Both,' ')) AS VarChar(260) CCSID 1208)) member
COMMSWKBL1. Select/omit error on member COMMSWKBL1.
Short version of the view:
CREATE VIEW WBVIEW (
MSGOCCID ,
DETLIST )
AS
(SELECT MSG.MSGOCCID,
FROM P6DEVCDB00.P6MSGPF MSG
LEFT OUTER JOIN LATERAL
(SELECT XMLSERIALIZE(XMLAGG(XMLTEXT(CONCAT(', ',TRIM(REPLACE(REPLACE(REPLACE(FIELDVAL,',','-'),'>','-'),'<','-'))))) AS VARCHAR(4096) CCSID 1208) AS DETLIST
FROM MSDPF MSGDET
WHERE COALESCE(MSGDET.MSGOCCID,' ') != ' ' and
MSG.MSGOCCID = MSGDET.MSGOCCID AND
MSGDET.FIELDVAL != ''
GROUP BY MSGDET.MSGOCCID)MSGDLIST ON 1=1);
Character concatenation should not be a problem (except if it gets confused somewhere with the CCSIDs). I replace the <>, form the detail for in case that may interfere with xml functions... I have catered for Null and '' values ....
I have search lots of threads on lots of different sites and have run out of ideas.
Any suggestions will be appreciated.
I'm just guessing a quick comment answer here. Those left outer joins can introduce nulls into the mix you can simply coalesce them in the where clause ...
select MSGOCCID, COALESCE(DETLIST,' ')
from WBVIEW MSGP where
coalesce(DETLIST,' ') like '%4179792C%'
IRL I would use locate because it just feels faster when it runs.
select MSGOCCID, COALESCE(DETLIST,' ')
from WBVIEW MSGP where
locate('4179792C' ,coalesce(DETLIST,' ')) <> 0
HY000 is a CLI (ODBC) General error, the explaination in the IBM documentation is:
An error occurred for which there is no specific SQLSTATE and for which no implementation defined SQLSTATE is defined. The error message returned by SQLError in the argument szErrorMsg describes the error and its cause.
Since this is probably not something you are coding by hand, you probably do not have the opportunity to issue SQLError, but I suspect that the message you quoted in your question is the result of some internal process retrieving that error. so the problem should be:
Data conversion or data mapping error.
Select or omit error on field MapXmlData(Cast(Concat(',
',Strip(Replace(Cast(P6MSDPF_4.FIELDVAL AS VarChar(128) CCSID 1208),'-'), Both,' ')) AS VarChar(260) CCSID 1208)) member COMMSWKBL1. Select/omit error on member COMMSWKBL1.
I don't know your files, but I am guessing that there is bad data in the file and that COMMSWKBL1 is a select logical that is accessed to process the selection when you add the DETLIST like '%4179792C%' to your query. Your task is to find out which file is associated with member COMMSWKBL1, and see if you can determine why a Select or omit error would be occurring on that file.
It is fine. I have modified the view to select both the header and detail, and search on both entity entries (linked on the key MSGOCCID) and do a DISTINCT on the key MSGOCCID. It seems a little bit slower than the view the question is about but is working fine. Thank you for you time.

XML parsing: semicolon expected - bypass bad record in SQL

I know the "XML parsing: semicolon expected" error is a data error, and not a SQL error. The first error I am trying to get rid of is the one about DTD subsets, so I am using CONVERT(xml, xml_data, 2), but then I get this:
Msg 9411, Level 16, State 1, Line 14
XML parsing: line 1, character 94, semicolon expected
I don't have access to modify the actual xml. I am querying. It is data in a table (with >1 million records, so I don't even know which record is throwing the error), received from a vendor as responses to API calls. I am trying to shred the xml in a stored procedure, in order to extract bits of data.
Is it possible to somehow bypass the records with the bad data? I am NOT processing row by row at the moment, nor do I want to.
Thanks!
I tried this, but it didn't work, either:
SELECT s.id as SessionID
, st.iD as TransmissionID
, st.TransmissionTypeID
, st.xml_data
, CONVERT(xml, replace(cast(st.xml_data as varchar(max)),'&','&'), 2) as converted_xml
, s.insertdt
, st.responsecode
INTO #Session_transmissions
FROM XML_Sessions s with (nolock)
join XML_Session_Transmissions st with (nolock) on s.id = st.tbl_Subscription_XML_SessionID
WHERE s.insertdt >= '3/1/2018`
This is what finally worked:
SELECT PrimaryKeyColumn, TRY_CONVERT(xml, VarcharColumn) AS XMLValue, VarcharColumn
FROM YourTable
WHERE TRY_CONVERT(xml, VarcharColumn) IS NULL
AND VarcharColumn IS NOT NULL;

Sybase Database Error: Invalid data conversion

I need help with this Query, I'm making some calculations based on some results, but I'm receiving the following error:
Sybase Database Error: Invalid data conversion.
The query is as follows:
SELECT
DC.DIM_DATE.DATE_ID,
DC.DIM_E_RAN_UCELL.RBS_ID,
(SUM(DC.DC_E_RAN_UCELL_RAW.pmSumBestDchPsIntRabEstablish)+SUM(DC.DC_E_RAN_UCELL_RAW.pmSumFachPsIntRabEstablish)+SUM(DC.DC_E_RAN_UCELL_RAW.pmSumBestPsHsAdchRabEstablish)+SUM(DC.DC_E_RAN_UCELL_RAW.pmSumBestPsEulRabEstablish))/720 AS '3G_DATA_ERLANG',
(SUM(DC.DC_E_RAN_UCELL_RAW.pmSumBestCs12Establish)+SUM(DC.DC_E_RAN_UCELL_RAW.pmSumBestAmr12200RabEstablish)+SUM(DC.DC_E_RAN_UCELL_RAW.pmSumBestAmr7950RabEstablish)+SUM(DC.DC_E_RAN_UCELL_RAW.pmSumBestAmr5900RabEstablish)+SUM(DC.DC_E_RAN_UCELL_RAW.pmSumBestAmr4750RabEstablish)+SUM(DC.DC_E_RAN_UCELL_RAW.pmSumBestAmrWbRabEstablish)+SUM(DC.DC_E_RAN_UCELL_RAW.pmSumBestAmrNbMmRabEstablish))/720 AS '3G_SPEECH_ERLANG',
(100*(SUM(DC.DC_E_RAN_UCELL_RAW.pmTotNoRrcConnectReqCsSucc)/(1+SUM(DC.DC_E_RAN_UCELL_RAW.pmTotNoRrcConnectReqCs)-SUM(DC.DC_E_RAN_UCELL_RAW.pmNoLoadSharingRrcConnCs))*(SUM(DC.DC_E_RAN_UCELL_RAW.pmNoRabEstablishSuccessSpeech)+SUM(DC.DC_E_RAN_UCELL_RAW.pmNoRabEstablishSuccessCs64)+SUM(DC.DC_E_RAN_UCELL_RAW.pmRabEstablishEcSuccess))/(SUM(DC.DC_E_RAN_UCELL_RAW.pmRabEstablishEcAttempt)+SUM(DC.DC_E_RAN_UCELL_RAW.pmNoRabEstablishAttemptSpeech)+SUM(DC.DC_E_RAN_UCELL_RAW.pmNoRabEstablishAttemptCs64)-SUM(DC.DC_E_RAN_UCELL_RAW.pmNoDirRetryAtt)))) AS '3G_CSSR_CS'
FROM
DC.DIM_DATE,
DC.DIM_TIME,
DC.DIM_E_RAN_UCELL,
DC.DC_E_RAN_UCELL_RAW
WHERE
(DC.DC_E_RAN_UCELL_RAW.HOUR_ID=DC.DIM_TIME.HOUR_ID and DC.DC_E_RAN_UCELL_RAW.MIN_ID=DC.DIM_TIME.MIN_ID)
AND (DC.DC_E_RAN_UCELL_RAW.DATE_ID=DC.DIM_DATE.DATE_ID)
AND (DC.DC_E_RAN_UCELL_RAW.OSS_ID=DC.DIM_E_RAN_UCELL.OSS_ID)
AND (DC.DC_E_RAN_UCELL_RAW.RNC=DC.DIM_E_RAN_UCELL.RNC_ID)
AND (DC.DC_E_RAN_UCELL_RAW.UtranCell=DC.DIM_E_RAN_UCELL.UCELL_ID)
AND
(
DC.DIM_DATE.DATE_ID IN ('2017-08-14')
AND
DC.DIM_E_RAN_UCELL.RBS_ID IN ('DN1U0441')
)
GROUP BY
DC.DIM_DATE.DATE_ID,
DC.DIM_E_RAN_UCELL.RBS_ID
The problem is with the last row of the SELECT statement, without it the result is this:
DATE_ID RBS_ID 3G_DATA_ERLANG 3G_SPEECH_ERLANG
8/14/2017 DN1U0441 421.8541 33.5055
When is included I got this error:
Lookup Error - Sybase Database Error: Invalid data conversion
Any Help? Is Sybase ASE DataBase

UNION data type mismatch

Running a UNION query in an access database. I've defined every variable as a int since there was a data type mismatch error prompt. This has not resolved the issue. Each of the variables have values of either 1 or 0 and no nulls. Any ideas?
SELECT
CInt(qryGB.BM∞) AS [BM∞],
CInt(qryGB.PM∞) AS [PM∞],
CInt(qryGB.P∞) AS [P∞],
CInt(qryGB.[RAG_B<0]) AS [RAG_B<0],
CInt(qryGB.[RAG_P<0]) AS [RAG_P<0],
CInt(qryGB.[RAG_C<0]) AS [RAG_C<0],
CInt(qryGB.[B<0]) AS [B<0],
CInt(qryGB.[P<0]) AS [P<0],
CInt(qryGB.[C<0]) AS [C<0],
CInt(qryGB.[P-1]) AS [P-1],
CInt(qryGB.[C-1]) AS [C-1],
CInt(qryGB.P0) AS [P0],
CInt(qryGB.C0) AS [C0],
CInt(qryGB.[P+1]) AS [P+1],
CInt(qryGB.[P+2]) AS [P+2],
CInt(qryGB.[P+3]) AS [P+3]
FROM qryGB
UNION ALL SELECT
CInt(qryTMD.BM∞) AS [BM∞],
CInt(qryTMD.PM∞) AS [PM∞],
CInt(qryTMD.P∞) AS [P∞],
CInt(qryTMD.[RAG_B<0]) AS [RAG_B<0],
CInt(qryTMD.[RAG_P<0]) AS [RAG_P<0],
CInt(qryTMD.[RAG_C<0]) AS [RAG_C<0],
CInt(qryTMD.[B<0]) AS [B<0],
CInt(qryTMD.[P<0]) AS [P<0],
CInt(qryTMD.[C<0]) AS [C<0],
CInt(qryTMD.[P-1]) AS [P-1],
CInt(qryTMD.[C-1]) AS [C-1],
CInt(qryTMD.P0) AS [P0],
CInt(qryTMD.C0) AS [C0],
CInt(qryTMD.[P+1]) AS [P+1],
CInt(qryTMD.[P+2]) AS [P+2],
CInt(qryTMD.[P+3]) AS [P+3]
FROM qryTMD;
Check you don't have any nulls in any of the columns.
Access SQL is a little strange when it comes to nulls (noting that Standard SQL nulls are strange to begin with!). For example you can't cast a null to a data type:
SELECT DISTINCT CINT( NULL ) AS null_cast_to_int FROM AnyPopulatedTable;
errors with "Invalid use of Null".
So all Access SQL nulls are of the same type but what type?:
SELECT DISTINCT TYPENAME ( NULL ) AS type_name FROM AnyPopulatedTable;
does not error and returns 'Null' !!
The one thing I can think of is that doing the CInt() conversion during the UNION may be screwing things up. I'd try doing the conversion in subqueries before doing the UNION. Something like:
SELECT
a.[BM∞],
a.[PM∞],
a.[P∞],
a.[RAG_B<0],
a.[RAG_P<0],
a.[RAG_C<0],
a.[B<0],
a.[P<0],
a.[C<0],
a.[P-1],
a.[C-1],
a.[P0],
a.[C0],
a.[P+1],
a.[P+2],
a.[P+3]
FROM
(SELECT
CInt(qryGB.[BM∞]) AS [BM∞],
CInt(qryGB.[PM∞]) AS [PM∞],
CInt(qryGB.[P∞]) AS [P∞],
CInt(qryGB.[RAG_B<0]) AS [RAG_B<0],
CInt(qryGB.[RAG_P<0]) AS [RAG_P<0],
CInt(qryGB.[RAG_C<0]) AS [RAG_C<0],
CInt(qryGB.[B<0]) AS [B<0],
CInt(qryGB.[P<0]) AS [P<0],
CInt(qryGB.[C<0]) AS [C<0],
CInt(qryGB.[P-1]) AS [P-1],
CInt(qryGB.[C-1]) AS [C-1],
CInt(qryGB.[P0]) AS [P0],
CInt(qryGB.[C0]) AS [C0],
CInt(qryGB.[P+1]) AS [P+1],
CInt(qryGB.[P+2]) AS [P+2],
CInt(qryGB.[P+3]) AS [P+3]
FROM qryGB) as a
UNION ALL SELECT
b.[BM∞],
b.[PM∞],
b.[P∞],
b.[RAG_B<0],
b.[RAG_P<0],
b.[RAG_C<0],
b.[B<0],
b.[P<0],
b.[C<0],
b.[P-1],
b.[C-1],
b.[P0],
b.[C0],
b.[P+1],
b.[P+2],
b.[P+3]
FROM
(SELECT
CInt(qryTMD.[BM∞]) AS [BM∞],
CInt(qryTMD.[PM∞]) AS [PM∞],
CInt(qryTMD.[P∞]) AS [P∞],
CInt(qryTMD.[RAG_B<0]) AS [RAG_B<0],
CInt(qryTMD.[RAG_P<0]) AS [RAG_P<0],
CInt(qryTMD.[RAG_C<0]) AS [RAG_C<0],
CInt(qryTMD.[B<0]) AS [B<0],
CInt(qryTMD.[P<0]) AS [P<0],
CInt(qryTMD.[C<0]) AS [C<0],
CInt(qryTMD.[P-1]) AS [P-1],
CInt(qryTMD.[C-1]) AS [C-1],
CInt(qryTMD.[P0]) AS [P0],
CInt(qryTMD.[C0]) AS [C0],
CInt(qryTMD.[P+1]) AS [P+1],
CInt(qryTMD.[P+2]) AS [P+2],
CInt(qryTMD.[P+3]) AS [P+3]
FROM qryTMD) as b