Discussions
Categories
- 197K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.8K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 556 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.4K SQL Developer
- 296.3K Development
- 17 Developer Projects
- 139 Programming Languages
- 293K Development Tools
- 110 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 158 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 205 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 468 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
Need help converting from oracle to sql server

Hi All I have an Oracle Query that i want to convert to sql server, i could manage the date conversions but still there are some XMLTABLE that i cannot wrap my head around, Can someone please help me what the XMLTABLE part is doing and what is the equivalent in SQL Server.
Thanks
ORACLE QUERY
SELECT aosp.osi_key,
Cast(aa.osi_data_type AS NUMBER(3)) AS osi_data_type,
aa.hour_ctr,
sp.uom_name
FROM air_so aosp,
Sap sp,
(WITH t
AS (SELECT 1 d1,
5 d2
FROM dual)
SELECT bb.hour_ctr,
d1 + i osi_data_type
FROM t,
XMLTABLE('for $i in 0 to xs:int(D) return $i' passing XMLELEMENT(
d, d2
-d1) COLUMNS i INTEGER path '.'),
(WITH t
AS (SELECT ( To_date('07/30/2012 07', 'MM/DD/YYYY HH24') -
Numtodsinterval(10, 'hour') ) d1
,
( To_date('09/19/2012 11', 'MM/DD/YYYY HH24')
+ Numtodsinterval(10, 'hour') )
d2
FROM dual)
SELECT i hour_ctr
FROM t,
XMLTABLE('for $i in 0 to xs:int(D) return $i' passing
XMLELEMENT
(d, (d2
-d1)*24) COLUMNS i INTEGER path '.')) bb) aa
WHERE sp.osi_key = aosp.osi_key
AND sp.active_flag = 1
ORDER BY 1,
2,
3
SQL SERVER SELECT aosp.osi_key, Cast(aa.osi_data_type AS NUMBER(3)) AS osi_data_type, aa.hour_ctr, sp.uom_name FROM air_so aosp, [dbo].[saP] sp ;WITH t AS(SELECT 1 d1, 5 d2 ) SELECT bb.hour_ctr, d1 + i osi_data_type FROM t, XMLTABLE('for $i in 0 to xs:int(D) return $i' passing XMLELEMENT( d, d2 -d1) COLUMNS i INTEGER path '.'), ;WITH t AS (select convert(varchar, dateadd(hour, -10,'07/30/2012'), 101) d1 , convert(varchar, dateadd(hour, 10,'09/19/2012'), 101) d2 ) SELECT i hour_ctr FROM t, XMLTABLE('for $i in 0 to xs:int(D) return $i' passing XMLELEMENT (d, (d2 -d1)*24) COLUMNS i INTEGER path '.')) bb) aa WHERE sp.osi_key = aosp.osi_key AND sp.active_flag = 1 ORDER BY 1, 2, 3
Answers
-
This is the Oracle community, I think you want the MSFT one.
-
The first XMLTABLE is a row generator that counts from d1 to d2 in column osi_data_type:
OSI_DATA_TYPE ------------- 1 2 3 4 5
The second XMLTABLE similarly creates a numbered row for each hour between d1 and d2 in column hour_ctr:
HOUR_CTR ---------- 0 1 2 3 4 5 6 <Truncated> 1246 1247 1248 1.249 rows selected.
I don't know if these work the same in SQL Server but they are not Oracle specific.
-
@cormaco thanks, is there a way of doing this without using XMLTABLE?
-
There are several other ways of generating rows.
Which work in SQL Server would definitely be a question for an SQL Server forum