I'm calling a webservice (url below) and am getting the XML results back within SQL Server as a varchar(8000) and then converting that to XML. This works perfectly. I want to parse this XML information out into it's individual values but continue to get null values. This is my first attempt in using XML on my SQL 2008 server so I know I'm missing a very trivial item.
http://dev.virtualearth.net/Services/v1/GeocodeService/GeocodeService.asmx/Geocode?culture=en-us&count=10&query=1%20microsoft%20way,%20redmond,%20wa&landmark=&addressLine=&locality=&postalTown=&adminDistrict=&district=&postalCode=&countryRegion=&mapBounds=¤tLocation=&curLocAccuracy=&entityTypes=&rankBy=
I'm taking the response received and storing it into #XML.
SET #XML = CAST(#Response AS XML)
I'm next trying to pull out the Postal Code to get my results and receive a NULL or the wrong node.
Returns NULL
SELECT #XML.value('(/GeocodingResult/Results/Address/PostalCode) [1]', 'varchar(50)')
Returns "Copyright © 2010 Microsoft and its suppliers. All " (without the quotes)
SELECT #XML.value('(/) [1]', 'varchar(50)')
Your XPath is wrong - your root node is GeocodingResponse (not GeocodingResult), and you're missing a GeocodingResult along the way.
Try this XPath:
/GeocodingResponse/Results/GeocodingResult/Address/PostalCode
or this SQL XQuery:
SELECT
#XML.value('(/GeocodingResponse/Results/GeocodingResult/Address/PostalCode) [1]',
'varchar(50)')
Related
I'm converting the SQL server to db2..
I need a solution for stuff and for xml path
Ex
Select stuff(select something
from table name
Where condition
For xml path(''),1,1,'')
Pls convert this into db2.
Your code is an old school XML "trick" to convert multiple values to a single string. (Often comma separated but in this case space separated.) Since those days DB2 (and the sql standards) have added a new function called listagg which is designed to solve this exact problem:
Select listagg(something,' ')
from table name
Where condition
db2 docs -
https://www.ibm.com/support/knowledgecenter/en/SSEPEK_12.0.0/sqlref/src/tpc/db2z_bif_listagg.html
https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/db2/rbafzcollistagg.htm
I am trying to pass a list of IDs to a REST api (apex ords).
I have a url like this :
https://apex.oracle.com/***/apex/anime_keeper/ak/getAnimeList/:ids
when I do :
https://apex.oracle.com/***/apex/anime_keeper/ak/getAnimeList/1
I get the item with id = 1 but if I do :
https://apex.oracle.com/***/apex/anime_keeper/ak/getAnimeList/1,2,3
I get a 500 Internal Server Error
How should I format my url so I can use the 1,2,3 list in a where id in (ids) in apex ords?
this is a screenshot of ords if it can help :
That SQL won't work becase ORDS does not split the csv values out. So that sql as-is will be checking for id in ( '1,2,3') not id in ( 1,2,3)
There's multiple ways to accomplish what the intent is.
For example, using XMLTABLE
SELECT rownum,object_id
FROM user_objects
WHERE rownum IN (
select (column_value).getstringval() csv_values
FROM
xmltable(('"' || REPLACE(:ids, ',', '","')|| '"'))
)
There are other ways mentioned here:
Using the "IN" clause with a comma delimited string from the output of a replace() function in Oracle SQL
Here's an ORDS REST API doing exactly what you intend.
In a URL, comma ',' has special meaning/purpose. It is to separate query arguments in url e.g
https://test.me/mypage?firstname=jon,lastname=doe,gender=m
So server is throwing 500 error as it finds corrupted or incomplete key/value pairs. It expects key=value pair after each comma. To get around this we need to urlencode value e.g
https://apex.oracle.com/***/apex/anime_keeper/ak/getAnimeList/1%2C2%2C3
I am working on a Jasper report using iReport 5.6 and IBM DB2 as data source. I am passing a list of strings as a parameter to the report and that is where the problem rises. The query is below;
SELECT customers.mobile_number,
COALESCE(Count(DISTINCT transaction_entries.transaction_id), 0) AS
number_of_transactions,
COALESCE(Sum(Abs(transaction_entries.amount)) / 100, 0) AS
volume_of_transactions
FROM transaction_entries
JOIN customers
ON customers.id = transaction_entries.customer_id
WHERE transaction_entries.transaction_type = 'Seasonal'
AND transaction_entries.notification_text <> 'Notification'
AND customers.mobile_number IN ( $p ! {listOfMobileNumbers} )
GROUP BY customers.mobile_number
When I try to generate the report I get the error Caused by: com.ibm.db2.jcc.am.SqlDataException: DB2 SQL Error: SQLCODE=-420, SQLSTATE=22018, SQLERRMC=DECFLOAT, DRIVER=3.66.46.
Any idea why ? and the possible solution ?
I would first verify that by commenting-out the last predicate of the WHERE clause avoids the error; i.e. redact the failing statement such that the IN predicate referencing the Jasper variable as input is no longer part of the query.
Then, determine what defines that variable replacement, from the output of the following query:
select '$p ! {listOfMobileNumbers}' from sysibm.sysdummy1
If the effect of the above query, used to reveal the data in that list, presents something like '1234,567,890', then I would suggest modifying the data that defines that list to reflect either of '1234','567','890' or 1234, 567, 890 instead.
FWiW: IMO the actual DDL [for column(s) or the TABLE] is much clearer to a reader than suggesting merely that:
The mobile_number field is returned from the database as a String and not a DECIMAL
I am trying to determine a solution to filter records using LIKE with a multi-valued parameter. In a simplistic example a user wants to return a set of 5-digit Object codes by entering the following in a parameter window in a SSRS report:
#parm_Object
1,24,333,34567
This ideally would return Object codes satisfying the following criteria:
1 : All Object codes starting with '1'
24: All Object codes starting with '24'
333: Similar
34567: Object code '34567'
I guess a starting point for me would be to determine whether this could be handled in the actual query, or should I do it on the SSRS side.
general good practice is to get rid of the data you don't need ASAP. so that would be in the query.
a SSRS multivalued parameter will show up as a comma separated list in the query.
the first step is to get from this comma separated list to a table (or table function), then you can join this table and apply like operators
for example
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS a
INNER JOIN dbo.split1('test,fafa,tata') b
ON 1=1
WHERE a.COLUMN_NAME like b.Value + '%'
will return rows having column names starting with test, fafa or tata. the dbo.split1 function you have to write your own or get one from the internet. the link suggested by Tab Alleman for example.
I have a complicated dynamic query in TSQL that I want to export to Excel.
[The result table contains fields with text longer than 255 chars, if it matters]
I know I can export result using the Management Studio menus but I want to do it automatically by code. Do you know how?
Thanks in advance.
You could have a look at sp_send_dbmail. This allows you to send an email from your query after it's run, containing an attached CSV of the resultset. Obviously the viability of this method would be dependent on how big your resultset is.
Example from the linked document:
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'AdventureWorks2008R2 Administrator',
#recipients = 'danw#Adventure-Works.com',
#query = 'SELECT COUNT(*) FROM AdventureWorks2008R2.Production.WorkOrder
WHERE DueDate > ''2006-04-30''
AND DATEDIFF(dd, ''2006-04-30'', DueDate) < 2' ,
#subject = 'Work Order Count',
#attach_query_result_as_file = 1 ;
One way is to use bcp which you can call from the command line - check out the examples in that reference, and in particular see the info on the -t argument which you can use to set the field terminator (for CSV). There's this linked reference on Specifying Field and Row Terminators.
Or, directly using TSQL you could use OPENROWSET as explained here by Pinal Dave.
Update:
Re;: 2008 64Bit & OPENROWSET - I wasn't aware of that, quick dig throws up this on MSDN forums with a link given. Any help?
Aside from that, other options include writing an SSIS package or using SQL CLR to write an export procedure in .NET to call directly from SQL. Or, you could call bcp from TSQL via xp_cmdshell - you have to enable it though which will open up the possible "attack surface" of SQL Server. I suggest checking out this discussion.
Some approaches here: SQL Server Excel Workbench
I needed to accept a dynamic query and save the results to disk so I can download it through the web application.
insert into data source didn't work out for me because of continued effort in getting it to work.
Eventually I went with sending the query to powershell from SSMS
Read my post here
How do I create a document on the server by running an existing storedprocedure or the sql statement of that procedure on a R2008 sql server
Single quotes however was a problem and at first i didn't trim my query and write it on one line so it had line breaks in sql studio which actually matters.