How do I execute a dynamic SQL with over 8000 Characters? - mdx

I'm trying to execute a stored procedure that merges SQL with MDX data by using this code:
OPENROWSET('MSOLAP',..)-Function.
I do this by building a string inside the stored procedure and executing it like this
EXEC sp_executesql #sqlQuery
When I do that, however, I get the error mentioned in the title when #sqlQuery exceeds 8000 characters
The OPENROWSET() part is executing the MDX query and according to the error this is also the part that exceeds the 8000 character limit, due to the number of Ids in the #MDXEntityIdSet parameter.
SET #SQLMDXQuery=
'with MQ
(
Datum
,Messwert
,Schlüssel
,MDXName
)
as
(
SELECT
convert(DATETIME,"[Measures].[DateTimeKey]")
,convert(FLOAT,"[Measures].[KPIValue]")
,convert(nvarchar(max),"[Measures].[EntityKey]")
,convert(nvarchar(max),"[Measures].[EntityName]")
FROM
OPENROWSET(''MSOLAP'',''Persist Security Info=False;Data Source=dwh-test-50-sql; Catalog='+#DBCatalog+';'',';
--OPENQUERY(SSAS,';
set #MDXPart=convert(nvarchar(max),
'''WITH
MEMBER [Measures].[DateTimeKey] AS '+#MDXShortDateTimeKey+'.CurrentMember.Properties("KEY")
MEMBER [Measures].[KPIValue] AS '+#MDXAggregation+'
MEMBER [Measures].[EntityKey] As '+#MDXEntityString+'.CurrentMember.Properties("KEY")
MEMBER [Measures].[EntityName] As '+#MDXEntityString+'.CurrentMember.Properties("NAME")
SELECT
{
[Measures].[DateTimeKey],
[Measures].[KPIValue],
[Measures].[EntityKey],
[Measures].[EntityName]
}ON COLUMNS
,
{'
+#MDXDateTimeKey+'} * {'+convert(nvarchar(max),#MDXEntityIdSet)+'
}
dimension Properties MEMBER_CAPTION, MEMBER_KEY ON ROWS
FROM
(
'+#LocalTimeZoneId+'
FROM
(
SELECT
{
'+#MDXStartDate+':'+#MDXEndDate+'
} ON COLUMNS
from [Measurements]
)
)'''+
')) ')
SET #SQLPart = 'Select MQ.*, '+#MetaDataEntityObject+'.* '+IIF(#SelectStr<>'',','+#SelectStr,'')+' from MQ left join '+#MetaDataEntityObject+' on ''{''+CONVERT(nvarchar(max),'+#MetaDataEntityObject+'.Id)+''}'' = MQ.Schlüssel Order by MQ.Datum';
SET #sqlQuery = convert(nvarchar(max),#SQLMDXQuery + #MDXPart + #SQLPart);
I've read that it is possible to circumvent the 8000 character limit somehow, but the specifics elude me.
Any help here is appreciated.

I worked around the 8000 character limit by creating a second stored procedure that determines how many IDs can be passed at once to the first stored procedure without exceeding the 8000 character limit, calling it then multiple times and appending the resultset of each call to a temporary table.
After the loop is finished I merely select * from the temporyr table.

Related

How to Insert data using cursor into new table having single column containing XML type data in Oracle?

I'm able to insert values into table 2 from table 1 and execute the PL/SQL procedure successfully but somehow the output is clunky. I don't know why?
Below is the code :
create table airports_2_xml
(
airport xmltype
);
declare
cursor insert_xml_cr is select * from airports_1_orcl;
begin
for i in insert_xml_cr
loop
insert into airports_2_xml values
(
xmlelement("OneAirport",
xmlelement("Rank", i.Rank) ||
xmlelement("airport",i.airport) ||
xmlelement("Location",i.Location) ||
xmlelement("Country", i.Country) ||
xmlelement("Code_iata",i.code_iata) ||
xmlelement("Code_icao", i.code_icao) ||
xmlelement("Total_Passenger",i.Total_Passenger) ||
xmlelement("Rank_change", i.Rank_change) ||
xmlelement("Percent_Change", i.Percent_change)
));
end loop;
end;
/
select * from airports_2_xml;
Output:
Why it is showing &lt ,&gt in the output ? And why am I unable to see the output fully?
Expected output:
<OneAirport>
<Rank>3</Rank>
<Airport>Dubai International</Airport>
<Location>Garhoud</Location>
<Country>United Arab Emirates</Country>
<Code_IATA>DXB</Code_IATA>
<Code_ICAO>OMDB</Code_ICAO>
<Total_passenger>88242099</Total_passenger>
<Rank_change>0</Rank_change>
<Percent_Change>5.5</Percent_Change>
</OneAirport>
The main issue is how you are constructnig the XML. You have an outer XMLElement for OneAirport, and the content of that element is a single string.
You are generating individual XMLElements from the cursor fields, but then you are concenating those together, which gives you a single string which still has the angle brackets you're expecting. So you're trying to do something like, simplified a bit:
select
xmlelement("OneAirport", '<Rank>1</Rank><airport>Hartsfield-Jackson</airport>')
from dual;
XMLELEMENT("ONEAIRPORT",'<RANK>1</RANK><AIRPORT>HARTSFIELD-JACKSON</AIRPORT>')
--------------------------------------------------------------------------------
<OneAirport><Rank>1</Rank><airport>Hartsfield-Jackson</airp
and by default XMLElement() escapes entities in the passed-in values, so the angle-brackets are being converted to 'safe' equivalents like <. If it didn't do that, or you told it not to with noentityescaping:
select xmlelement(noentityescaping "OneAirport", '<Rank>1</Rank><airport>Hartsfield-Jackson</airport>')
from dual;
XMLELEMENT(NOENTITYESCAPING"ONEAIRPORT",'<RANK>1</RANK><AIRPORT>HARTSFIELD-JACKS
--------------------------------------------------------------------------------
<OneAirport><Rank>1</Rank><airport>Hartsfield-Jackson</airport></OneAirport>
then that would appear to be better, but you still actually have a single element with a single string (with characters that are likely to cause problems down the line), rather than the XML structure you almost certainly intended.
A simple way to get an zctual structure is with XMLForest():
xmlelement("OneAirport",
xmlforest(i.Rank, i.airport, i.Location, i.Country, i.code_iata,
i.code_icao, i.Total_Passenger, i.Rank_change, i.Percent_change)
)
You don't need the cursor loop, or any PL/SQL; you can just do:
insert into airports_2_xml (airport)
select xmlelement("OneAirport",
xmlforest(i.Rank, i.airport, i.Location, i.Country, i.code_iata,
i.code_icao, i.Total_Passenger, i.Rank_change, i.Percent_change)
)
from airports_1_orcl i;
The secondary issue is the display. You'll see more data if you issue some formatting commands, such as:
set lines 120
set long 32767
set longchunk 32767
Those will tell your client to retrieve and show more of the long (XMLType here) data, rather the default 80 characters it's giving you now.
Once you are generating a nested XML structure you can use XMLSerialize() to display that more readable when you query your second table.
Try this below block :
declare
cursor insert_xml_cr is select * from airports_1_orcl;
v_airport_xml SYS.XMLTYPE;
begin
for i in insert_xml_cr
loop
SELECT XMLELEMENT ( "OneAirport",
XMLFOREST(i.Rank as "Rank"
,i.airport as "Airport"
,i.Location as "Location"
,i.Country as "Country"
,i.code_iata as "Code_iata"
,i.code_icao as "code_icao"
,i.Total_Passenger as "Total_Passenger"
, i.Rank_change as "Rank_change"
,i.Percent_change as "Percent_Change"
))
into v_airport_xml
FROM DUAL;
insert into airports_2_xml values (v_airport_xml);
end loop;
end;

DB2 SQL Web Pagination - How to tell I reach EOF

Friends,
I am trying to find a very simple solution to tell me I have reach the End of file with a Web Pagination, using Fetch Next. I am using Previous & Next button to trigger stored procedure.
**FREE
// RFC Main Grid
CTL-OPT NOMAIN OPTION (*SRCSTMT : *NODEBUGIO);
DCL-PROC PUR027 EXPORT;
DCL-PI PUR027 EXTPROC(*DCLCASE);
StartingRow PACKED(3:0);
NbrOfRows PACKED(3:0);
TotalRows CHAR(10);
RowCount CHAR(10);
Search CHAR(30);
EndOfFile CHAR(3);
BOF CHAR(1);
EOF CHAR(1);
RSL CHAR(2);
END-PI;
IF Search = '';
EXEC SQL
Declare RSCURSOR cursor for
SELECT CDEPT, CDESC, ROW_NUMBER() OVER(ORDER BY CDESC, CDEPT) as ROWNUMBER
FROM CDPL03
ORDER BY CDESC, CDEPT
OFFSET (:StartingRow - 1) * :NbrOfRows ROWS
FETCH NEXT :NbrOfRows ROWS ONLY;
EXEC SQL Open RSCURSOR;
EXEC SQL SET RESULT SETS Cursor RSCURSOR;
ELSE;
EXEC SQL
Declare RSCURSOR2 cursor for
SELECT CDEPT, CDESC, ROW_NUMBER() OVER(ORDER BY CDESC, CDEPT) as ROWNUMBER
FROM CDPL03
WHERE CDESC LIKE '%' concat trim(:Search) concat '%' OR
CDEPT LIKE '%' concat trim(:Search) concat '%'
ORDER BY CDESC, CDEPT
OFFSET (:StartingRow - 1) * :NbrOfRows ROWS
FETCH NEXT :NbrOfRows ROWS ONLY;
EXEC SQL Open RSCURSOR2;
EXEC SQL SET RESULT SETS Cursor RSCURSOR2;
ENDIF;
// Begin & End of File
IF StartingRow = 1;
BOF = '1';
EOF = '0';
ELSE;
BOF = '0';
EOF = '0';
ENDIF;
// Validate for SQL errors
IF SQLSTATE = '00000';
RSL = '00';
//TotalRows2 = %CHAR(TotalRows);
ELSEIF SQLSTATE = '02000';
RSL = '10';
ELSE;
RSL = '20';
ENDIF;
RETURN;
END-PROC PUR027;
// To create the service program:
// CRTSRVPGM SRVPGM(BPCSO/PUR027WS)
// MODULE(BPCSO/PUR027W)
// SRCFILE(BPCSS/PURBNDF) SRCMBR(PUR027WB)
When reading multiple records in a block, I retrieve the number of records fetched with GET DIAGNOSTICS like this:
exec sql get diagnostics
:cnt = row_count;
Then if the number of records fetched is less than the requested number of records, I know that I am on the last page.
There is a problem with this method though. If the last page is full, you don't know it until you try to read the next page, and it is empty. So one way to handle that is to request one record more than you are going to present on the page. That is, if you are presenting 25 records per page, request 26. If your result set has 26 records, then there is at least one record on the next page. Still only present 25 records, and increment your offset by 25 records each time, just request 26 records. If the record set has less than 26 records, then you know you are on the last page.
Take a look at SQLERRD(2)
For an OPEN statement, if the cursor is insensitive to changes, SQLERRD(2) contains the actual number of rows in the result set. If the cursor is sensitive to changes, SQLERRD(2) contains an estimated number of rows in the result set.
You can also use GET DIAGNOSTICS after the open for the same info...
DB2_NUMBER_ROWS
If the previous SQL statement was an OPEN or a FETCH which caused the size of the result table to be known, returns the number of rows in the result table. For SENSITIVE cursors, this value can be thought of as an approximation since rows inserted and deleted will affect the next retrieval of this value. Otherwise, the value zero is returned.
Key point for both, for an exact count, you'd need to declare your cursor INSENSITIVE which will create a copy of your selected rows so that inserts, deletes and updates don't affect the results. There's also a performance hit.

SAP HANA SQL Query with Dynamic Placeholder

I have a query that is passing the current year as a placeholder parameter that right now is hard coded. How can I have this just pass the current year? I've seen a few different potential solutions but most of them are in HANA Studio or involve dynamic SQL generation.
I'm putting the SQL into Tableau so those are both off the table.
...sum("StockInQualityInspection") as in_quality,
sum("StockInTransit") as its
from "_SYS_BIC"."stream.models.marketing.poly/InventoryQuery" ('PLACEHOLDER' = ('$$IPCurrentYear$$', '2018'))
where "StockValuatedUnrestrictedUse" <> 0 or "StockInQualityInspection" <> 0 or "StockInTransit" <> 0
group by case when "ReceivingPlant" is null then "Plant" else "ReceivingPlant" end,
case....
Remove the parameters input of your CV
Add this expression: year(now())
If you don't have access to manipulate the CV, into your query use:
('PLACEHOLDER' = ('$$IPCurrentYear$$', select year(now()) from DUMMY))
Regards
while placing a query is not permitted, you can pass a parameter as following
do begin
declare lv_param nvarchar(100);
select max('some_date')
into lv_param
from dummy /*your_table*/;
select *
from "_SYS_BIC"."path.to.your.view/CV_TEST" (
PLACEHOLDER."$$P_DUMMY$$" => :lv_param
);
end;
more can be found here
credit to #astentx

SQL HTML email showing blank when one table has no results

The following code sends 2 diferent tables based on an sql query through the function sp_send_dbmail , the catch is , if both tables return results , the email shows up without any problem , perfectly. If one of the tables has NO results, the email comes up completly blank.
How can i fix this?
Thanks
declare #tableHTML NVARCHAR(MAX);
set #tableHTML = N'Este foi o resultado de Faturas Emitidas: <br><br><table border ="1">' +
N'<tr><th>Documento</th></tr>' +
cast (( select td = cc.tx
from cc
for xml path ('tr'),type) as nvarchar(max)) +
N' </table><table border ="1"><tr><th>Valor Total Vencido</th></tr>'
+
cast (( select td = fx.tc
from fx
for xml path ('tr'),type) as nvarchar(max)) +
N'</table>';
EXEC sp_send_dbmail
#profile_name ='xx_SqlMail',
#recipients ='ccccc#hotmail.com',
#subject ='Resumo',
#body =#tableHTML,
#body_format='HTML';
I would suspect that part of your query is returning a NULL value. Concatenating any value with a NULL will always result in NULL.
SELECT 'A' + NULL + 'B' will return NULL.
As you are doing multiple concatenations it would mean that if any value is NULL then #tableHTML will be NULL. Try wrapping your selects in an ISNULL().
select ISNULL(td, '') = cc.tx ...
Any table in your concatenation that returns a NULL will make the entire concatenation NULL.
To resolve this, just wrap each section that could potentially be NULL with an ISNULL().
I had a similar issue where I was running two separate queries and building two tables that I wanted to include in the body of the email. One would occasionally return no values and the email would come back blank. Using ISNULL fixed it for me.
See the code below for an example of what I did:
set #tablesHTML = **ISNULL**(#tableOneHTML,'NO RESULTS')
+ **ISNULL**(#tableTwoHTML,'NO RESULTS')
exec XXXXXX.[XXX].[sp_send_dbmail]
#profile_name='Mail'
,#recipients = #EmailRecipients
,#copy_recipients= #EmailCopyRecipients
,#subject = 'Email Subject Here'
,#body = #tablesHTML
,#body_format = 'HTML'

Extracting a portion of a value out of a database column using SQL server

I'm trying to extract a portion of a value out of a database column using SQL server.
The example below works in a simple context with a varchar field. The result is: &kickstart& which is what I want.
I now want to do the same when retrieving a column from the database.
But SQL does not like what I am doing. I'm thinking it is something easy that I am not seeing.
Declare #FileName varchar(20) = '&kickstart&.cfg'
Declare #StartPos integer = 0
Declare #FileNameNoExt varchar(20)
SELECT #FileNameNoExt = Left(#FileName,( (charindex('.', #FileName, 0)) - 1))
SELECT #FileNameNoExt
Here is the SQL statement that I can't seem to get to work for me:
Declare #FileNameNoExt as varchar(20)
SELECT
i.InstallFileType AS InstallFileType,
o.OSlabel AS OSLabel,
SELECT #FileNameNoExt = (LEFT(oi.FIleName,( (charindex('.', oi.FIleName, 0) ) - 1) )) AS FileNameNoExt,
oi.FIleName AS FIleName
FROM
dbo.OperatingSystemInstallFiles oi
JOIN dbo.InstallFileTypes i ON oi.InstallFileTypeId = i.InstallFileTypeId
JOIN dbo.OperatingSystems o ON oi.OperatingSystemId = o.OperatingSystemId
Why do you need the variable at all? What's wrong with:
SELECT
i.InstallFileType AS InstallFileType,
o.OSlabel AS OSLabel,
LEFT(oi.FIleName,( (charindex('.', oi.FIleName, 0) ) - 1) ) AS FileNameNoExt,
oi.FIleName AS FIleName
FROM
dbo.OperatingSystemInstallFiles oi
JOIN dbo.InstallFileTypes i ON oi.InstallFileTypeId = i.InstallFileTypeId
JOIN dbo.OperatingSystems o ON oi.OperatingSystemId = o.OperatingSystemId
You've put a SELECT inside another SELECT list without nesting, which is a syntax error in SQL Server.
You are also attempting to assign a variable while performing a data-retrieval operation. You can select all data to be shown, or all data into variables but not both at the same time.
When the two issues above are resolved, I think you may still run into issues when committing filenames into a variable which only allows 20 characters - but then I don't know anything about your dataset.