How do I get the contents of all of the nodes in a root node with SQL and XQuery? - tsql

I have the following table structure:
CREATE TABLE SpecialTable
(
Key UNIQUEIDENTIFIER,
XMLField VARCHAR(MAX)
)
In the first tuple:
Key = "28384841-17283848-7836-18292939"
XMLField =
"<RootNode>
<ForeignKey>92383829-27374848-1298-19283789</ForeignKey>
<ForeignKey>47585853-27374848-4759-19283939</ForeignKey>
<ForeignKey>37383829-27374848-3747-19283930</ForeignKey>
</RootNode>"
In another tuple, I see:
Key = "89984841-17283848-7836-18292939"
XMLField =
"<RootNode>
<ForeignKey>92383829-27374848-1298-19283789</ForeignKey>
<ForeignKey>37383829-27374848-3747-19283930</ForeignKey>
</RootNode>"
In a further tuple, I see:
Key = "11114841-17283848-7836-18292939"
XMLField =
"<RootNode>
<ForeignKey>37383829-27374848-3747-19283930</ForeignKey>
</RootNode>"
What I need to do is to get the following dataset out:
Key ForeignKey
28384841-17283848-7836-18292939 92383829-27374848-1298-19283789
28384841-17283848-7836-18292939 47585853-27374848-4759-19283939
28384841-17283848-7836-18292939 37383829-27374848-3747-19283930
89984841-17283848-7836-18292939 92383829-27374848-1298-19283789
89984841-17283848-7836-18292939 37383829-27374848-3747-19283930
11114841-17283848-7836-18292939 37383829-27374848-3747-19283930
I must say that this is a simplified data set and that the data was more complex than this and I have got to a point where I cannot get any further.
I have tried this:
SELECT sp.Key,
x.XmlCol.Query('.')
FROM SpecialTable AS sp
CROSS APPLY sp.XMLField.nodes('/RootNode') x(XmlCol)
However, it seems just to show the Key and the whole of the XML of the XMLField.
Also, I tried this:
SELECT sp.Key,
x.XmlCol.Query('ForeignKey[text]')
FROM SpecialTable AS sp
CROSS APPLY sp.XMLField.nodes('/RootNode') x(XmlCol)
And I get only the first value in the first ForeignKey node and not the others.
What am I doing wrong?
Kindest regards,
QuietLeni

First of all - if your data looks like XML, quacks like XML, smells like XML - then it IS XML and you should use the XML datatype to store it!
Also: be aware that Key is a very generic term, and also a T-SQL reserved keyword, so it makes for a really bad column name - use something more meaningful that doesn't clash with a keyword!
Once you've done that, you should be able to use this code to get your desired results:
SELECT
[Key],
ForeignKey = xc.value('.', 'varchar(50)')
FROM
dbo.SpecialTable
CROSS APPLY
XMLField.nodes('/RootNode/ForeignKey') AS XT(XC)
This will only work if you column XMLField is of XML datatype !! (which it really should be anyway)

Related

I need to change the output of a query so that instead of it coming back as the abbreviation 'em' it says 'employee'. Tsql

I have the correct result coming back. I just need to convert 6 abbreviations in that result to their correct names. There are 20k names assigned to 1 of 6 abbreviated names.
I tried aliasing but that seems to only work for table names.
I tried doing a case statement but that didn't work.
You need to provide more details (like some sample input and output), but if you have data like EM100, and you want to make it EMPLOYEE 100, then you could use an expression such as:
CASE WHEN ColumnName like 'EM%' THEN 'EMPLOYEE ' + SUBSTRING (ColumnName,3,100)
WHEN ColumnName like 'RN%' THEN 'REGNURSE' + SUBSTRING (ColumnName,3,100)
else ColumnName END
But providing more details will help provide a more specific answer.

Documentation on Postgres Parse tree?

Is there a place that says what that documents the Postgres parse tree? For example from Query Parsing it gives an example of:
SELECT * FROM foo where bar = 42 ORDER BY id DESC LIMIT 23;
(
{SELECT
:distinctClause <>
:intoClause <>
:targetList (
{RESTARGET
:name <>
:indirection <>
:val
...
But I can't find a place that gives more information, for example, what is indirection in the RESTARGET clause? It does link to the Postgres internals docs # https://www.postgresql.org/docs/current/overview.html, but I couldn't find anything about the Postgres parse tree in detail from that. Is anything documented on that?
Note: for the question I'm only concerned with the SELECT statement.
Most of the relevant structs are defined in parsenodes.h, e.g. ResTarget and its attributes are defined here:
https://github.com/postgres/postgres/blob/master/src/include/nodes/parsenodes.h#L461
You may also find pg_query useful, a library I wrote to parse queries using the Postgres parser: https://github.com/pganalyze/pg_query (the output format is either JSON or Protobuf, and maps to the parsenodes.h struct names 1:1)

PSQL Apply mapping to an array

I have an array of an enum type order_options that gets passed through to a function and it is pretty simple, it looks like this:
'{master_date, e_name}'
I have 6 possible values and the enum type looks like this:
create type order_options as enum ('master_date','e_name','r_data','n_count','creation_date','last_updated_on');
In the function however, I want to apply a mapping to these values to change the value of sort_params which is the array passed through.
For each enum, I have an 'alternative name' that I want to use in the order by clause of a subsequent select statement. For example:
'master_date' = o.master_date
'e_name' = d.e_name
and so on.
I've looked in to doing a replace whereby I loop through each element of the array and attempt a replace with each mapping but it gets pretty messy and complex.
So for example, this works for one individual mapping:
select array_replace('{master_date}'::text[],'master_date','o.master_date');
I'd like my sort_params to look like this after mapping:
'{o.master_date, d.e_name}'
Is there any easier way to do this?
For now, this is what I have:
select array_replace(sort_params_u,'master_date','o.master_date') into sort_params_u;
select array_replace(sort_params_u,'e_name','d.e_name') into sort_params_u;
etc

SQL query with XML parameter

EDIT: I have found a relevant answer already on stack overflow here:
XQuery [value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *'
I have not dealt with XML in T-SQL before, and I am modifying an existing legacy stored proc, and picking most if it up through trial and error.
however I have hit a problem where trial and error is proving fruitless, and very slow. Think it's time to appeal to stack overflow gurus!
Here is some XML
<?xml version=\"1.0\"?>
<Notification xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">
<NotificationId>0</NotificationId>
<UserNotifications>
<UserNotification>
<UserNotificationId>0</UserNotificationId>
<NotificationId>0</NotificationId>
<UserId>13514</UserId>
<MessageTypeId>1</MessageTypeId>
</UserNotification>
<UserNotification>
<UserNotificationId>0</UserNotificationId>
<NotificationId>0</NotificationId>
<UserId>13514</UserId>
<MessageTypeId>2</MessageTypeId>
</UserNotification>
</UserNotifications>
</Notification>
The Stored Proc in question accepts the above XML as a parameter:
CREATE PROCEDURE [dbo].[Notification_Insert]
#ParametersXml XML
AS
BEGIN
The XML contains child "UserNotification" elements. I would like to select the UserId, MessageTypeId of each UserNotification, into a table like this
UserId | MessageTypeId
13514 | 1
13514 | 2
Obviously the size of the collection is not fixed.
My current attempt (which doesn't work - is along these lines:
DECLARE #UserDetails TABLE ( UserId INT, MessageTypeId INT);
INSERT INTO #UserDetails (UserId, MessageTypeId)
SELECT Tab.Col.value('#UserId','INT'),
Tab.Col.value('#MessageTypeId','INT')
FROM #ParametersXml.nodes('/Notification/UserNotifications[not(#xsi:nil = "true")][1]/UserNotification') AS Tab(Col)
But this never inserts anything..
I have been playing around with this for a while now and not had any joy :(
I would suggest going through the links below. I found them short and quick to go through:
http://blog.sqlauthority.com/2009/02/12/sql-server-simple-example-of-creating-xml-file-using-t-sql/
http://blog.sqlauthority.com/2009/02/13/sql-server-simple-example-of-reading-xml-file-using-t-sql/
I found the solution to this problem through further searching stack overflow.
The query I need (thanks to XQuery [value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *')
INSERT INTO #UserDetails (UserId, MessageTypeId)
SELECT UserNotification.value('UserId[1]','INT'),
UserNotification.value('MessageTypeId[1]','INT')
FROM #ParametersXml.nodes('//Notification/UserNotifications') AS x(Coll)
cross apply #ParametersXml.nodes('//Notification/UserNotifications/UserNotification') as un(UserNotification)

Specifying a DB table dynamically? Is it possible?

I am writing a BSP and based on user-input I need to select data from different DB tables. These tables are in different packages. Is it possible to specify the table I want to use, based on its path, like this:
data: path1 type string value 'package1/DbTableName',
path2 type string value 'package2/OtherDbTableName',
table_to_use type string.
if some condition
table_to_use = path1.
elseif some condition
table_to_use = path2.
endif.
select *
from table_to_use
...
endselect
I am new to ABAP & Open SQL and am aware this could be an easy/silly question :) Any help at all would be very much appreciated!
You can define the name of the table to use in a variable, and then use the variable in the FROM close of your request :
data tableName type tabname.
if <some condition>.
tableName='PA0001'.
else.
tableName='PA0002'.
endif.
select * from (tableName) where ...
there are a few limitation to this method, as the stable can not contains fields of type RAWSTRING, STRING or SSTRING.
as for the fact that the table are in different package, i don't think it matters.
Regards,