Read XML string in SQL and Dynamically select value from XMl string - sql

I have One table "tblXMLData" with One column "coreData".
I am inserting XML string in this table.
I want to read this xml and insert record into another table.
When i write bellow code it is working fine,
Declare #xmlRec xml
Select TOP 1 #xmlRec = coreData FROM tblXMLData
SELECT
tmpTable.col.query ('FName').value('.','varchar(50)') AS FName,
tmpTable.col.query ('LName').value('.','varchar(50)') AS LName,
tmpTable.col.query ('Mobile').value('.','varchar(50)') AS Mobile
FROM #xmlRec.nodes('data/data') AS tmpTable(col)
But I want to create select statement Dynamically. Like I am read column name from another table and creating dynamic select statement.
When i execute that statement it will not work because "#xmlRec.nodes" is giving error.
I want to create dynamic Query and insert selected record into another table.
Please help....

Related

SQL Error 'ORA-00904' When Retrieving Data From an XML File Via XMLQuery

I've been trying for days now to retrieve data from an XML file with a SELECT statement in SQL Developer but I constantly get the 'ORA-00904' when trying to execute the statement. So far, these are the steps I've been following:
Create the table and directory where I want the XML data to be stored in:
CREATE TABLE PLAYER OF XMLTYPE; / CREATE DIRECTORY PLDIR AS 'C:\Users\marta\OneDrive\Escritorio\UOC\Sem3\ABD\PR2'; /
Insert into my PLAYER table said data:
INSERT INTO PLAYER VALUES (XMLTYPE(bfilename('PLDIR', 'InfoPlayersWPT.xml'),nls_charset_id('AL32UTF8')))
/
So far so good. The issue appears when I try to execute the SELECT statement
What could it be? I've changed the $Name parameter a million times as well as the Name field but nothing changes. The thing is that in the XML file, these are the fields:
--Update--
I've modified a little bit the structure and this is the new error I get:
enter image description here
I've reached a point where I don't get if there could be a problem with my database connection or if the variable are incorrect.
Any form of help would be much appreciated.
Your table doesn't have a name or id column. Your query is trying to get those, while also transforming the XML to an info node making the id a node rather than an attribute, but you still don't extract the values from that new XML. You don't need to though.
If the document only has one player (which seems unlikely with the outer node 'Players') then you can get the data with an XMLQuery call for each value:
select XMLQuery('/Players/Player/#id' passing p.object_value returning content) as id,
XMLQuery('/Players/Player/Name/text()' passing p.object_value returning content) as name
from player p
ID
NAME
1
Francinsco Navarro Compán
But it's a bit simpler, particularly if you add more data items, to use a single XMLTable instead:
select x.id, x.name
from player p
cross apply XMLTable(
'/Players/Player'
passing p.object_value
columns id number path '#id',
name varchar2(30) path 'Name'
) x
ID
NAME
1
Francinsco Navarro Compán
fiddle
... which would also handle multiple players in each XML document.

Moving everything from one table to another in sql server while converting one of the rows

I am trying to move everything from one table to another table. I know how to do that if all of the columns had the same data type, however one column is different. On the new table its varcahar(24) and on the old table its a bigint.
This is what i have so far, Im using the first select statement to set the id, and the rest to add it to the new table. However this always returns the last id, and i need it to be synced up with the second select statement
Any suggestions on how to do this would be awesome. i tried to google it for about an hour but couldnt come up with anything. I am using SQL Server 2012
use DatabaseA
GO
DECLARE #id bigint;
SELECT #id= columnName FROM differentDB.tableB
INSERT INTO tableA
(buyer_id, restOfTheColumns)
Select CAST(#id AS varchar(24)), restOfTheColumns
FROM differentDB.tableB
GO
Variables don't work like that. T-SQL variables hold data values, not object names. Furthermore, that's a scalar variable. It only holds one value.
That said, you don't need the variable at all. You can just do this:
INSERT INTO tableA (buyer_id, restOfTheColumns)
SELECT CAST(columnName AS varchar(24)), restOfTheColumns
FROM differentDB.tableB

Compare XML data to String

I have a table that houses a bunch of data in an XML field. I can get to the data and display what I need in the select statement, but I also need to use that to compare to another table that houses a translation I am trying to do. Is there a way to compare the value being returned from the XML data to a string value that exists in another table?
The code in my select to return the XML data is:
prv.reported_attributes.value('(/row[#ATTRIBUTE="FIELD"][1])/#VALUE', 'varchar(5)')
I need to compare that text output to another table, but I keep getting NULL like the values I am trying to compare do not match. I have confirmed they do in fact have matches.

Select distinct from single column in lightswitch

I'm trying to execute this SQL query inside lightswitch:
Select distinct [columnName] from [table]
The query window only has Filter, Sort, and Parameters
I have arrived on the DataService.cs file where I can manualy edit the queries, but im stuck here:
partial void QueryName_PreprocessQuery(ref IQueryable<ServiceUser> query)
{
query = query.Distinct();
}
This selects the whole table. I want the distinct values of only one column.
Note that I can add a view to the database and update it as an entity but thats not what I'm looking for..

Inserting a new column into SQL

I have these queries:
SELECT *
FROM dbo.GRAUD_ProjectsByCostCategory
select right(CostCategoryId,14) as CostBreak
from dbo.GRAUD_ProjectsByCostCategory
They work well in that they give me the correct data, but I would like to know how to combine the new column CostBreak into the table of results rather than as a separate query result.
An example of the results I get are as below:
Where I want them in the same table
The data is coming from the same table so you should be able to just add that value to your initial query. You do not even have to perform a join to get it:
SELECT name,
description,
project,
CostCategoryId,
right(CostCategoryId,14) as CostBreak
FROM dbo.GRAUD_ProjectsByCostCategory