SQL-Server Error Could not find method 'STContains' - sql-server

I'm using the mssql server to implement a spatial database. I have one table (Ways) containing a geography column called geoline, that is of LINESTRING type.
I want to select the geoline containing a specific POINT, with the coordinates (38.731611,-9.135336).
I tried this:
SELECT geoLine.STAsText()
FROM Ways
WHERE geoLine.STContains(geometry::STGeomFromText('POINT(38.731611 -9.135336)', 0));
But it returned the following error:
Msg 4145, Level 15, State 1, Line 2 An expression of non-boolean type
specified in a context where a condition is expected, near ';'.
What can I do? Any idea for a success query?
Thanks

I think you are using SQL SERVER 2008 or below thats why you are getting that error because
STContains (geography Data Type) will work from SQL SERVER 2012 and above (source).
In your case to find instance completely contains another geometry instance you need a geometry data type.
FIDDLE DEMO HERE
CREATE TABLE #Ways
(geoLine GEOMETRY)
INSERT INTO #Ways
(geoLine)
VALUES (geometry::STGeomFromText('LINESTRING (100 100, 20 180, 180 180)', 0)),
(geometry::STGeomFromText('POINT(38.731611 -9.135336)', 0))
SELECT geoLine.STAsText() AS geoLine,geoLine
FROM #Ways
SELECT geoLine.STAsText() AS geoLine
FROM #Ways
WHERE geoLine.STContains(geometry::STGeomFromText('POINT(38.731611 -9.135336)', 0)) = 1

According to T-SQL documentation, STContains returns a BIT data type, so your WHERE condition should look like this I think :
SELECT geoLine.STAsText()
FROM Ways
WHERE geoLine.STContains(geometry::STGeomFromText('POINT(38.731611 -9.135336)', 0)) = 1;

Related

Microsoft SQL Server: Error with Group By

I'm new to Microsoft SQL Server 2014. I run this SQL code:
SELECT TOP(10) 'DBSG' as seek_entity, *
FROM DBSG..PM00200
and get this result:
Next, I want to find out total line items for that entity with code below.
WITH vw_pm00200_all AS
(
SELECT TOP(10)
'DBSG' as seek_entity, *
FROM
DBSG..PM00200
)
SELECT
seek_entity,
COUNT(*) AS total
FROM
vw_pm00200_all
GROUP BY
1
Sadly, I get this error. I have no idea why it failed.
Msg 164, Level 15, State 1, Line 9
Each GROUP BY expression must contain at least one column that is not an outer reference.
Lastly, please advise is Microsoft SQL Server based on Transact-SQL?
It looks like you are running into this problem here: Each GROUP BY expression must contain at least one column that is not an outer reference
As the answer points out, grouping by a constant literal is pointless as it is the same for all results. Count(*) will return the same result as Count(*) with a GROUP BY.
If this is just test code and you plan on using a CASE statement (with different values) in place of the string literal, you may have better luck.
Yes, T-SQL is Microsoft SQL Server's flavor of SQL.

XQuery is Expected Error but XML already in SQL Table

Using 11.0 SP2 sql server manager.
I am trying to extract the values from the xml but I keep receiving the error 2205 "XQuery was expected". I do not understand why I am receiving that error since the XML is coming from a sql table. It is my understanding that the XQuery is needed to specify a path but if the data is already in the sql table referenced then why is a specific path needed?
Select
XmlData.value('(/ItemInformation Culture/title)[1]','varchar(max)') as Title
From
[ArcDev].[dbo].[XmlRetrieval2]
Msg 2205, Level 16, State 1, Line 3 XQuery
[ArcDev.dbo.XmlRetrieval2.XmlData.value()]: ")" was expected.
The table looks like this:
XmlRetrieval2
You picture does not show the structure of your XML, if this does not solve your problem you should paste a (reduced) example of your XML.
From the picture I take that there is one element "ESRI_ItemInformation" with an attribute "Culture". It might be "ESRI" and "ItemInformation_Culture", but I don't think so...
If "title" is an element one level below the "ESRI_ItemInformation" your query might be this:
Select
XmlData.value('(/ESRI_ItemInformation/title)[1]','varchar(max)') as Title
From
[ArcDev].[dbo].[XmlRetrieval2]

Query fails column_name is null when column is a char

I'm using hibernate with sql_server, but am finding problems with the following:
select col_code
from table
where col_code is null
Query fails: sql error 0, SQLState S1093 The column col_code is not valid
col_code is a char(5), and reads okay from other database browsers. Its just from hibernate - Any Ideas?
I got the same error when I was doing an native SQL query from Java EE/Hibernate. The SQL statement that I passed to persistence was "SELECT Count (1) From Table" and I made the mistake of including a mapping class as a parameter. COUNT(1) returns an integer, so it doesn't need to be mapped. Hope that helps.
I would check that you're mapping is right.

combining sql server spatial and NHibernate with CreateSqlQuery

I have not mapped geography table in sql server 2008, and I want to query it with a polygon instance in c# using nhibernate.
In the beggining I was trying to use sql server spatial directly but encountered this problem:
Using SQL Server 2008 Geography types with nHibernate's CreateSQLQuery
My second try was this:
session.CreateSQLQuery("select [shape] from [table] where (:codeShape).STIntersects([shape]) = 1").SetParameter("codeShape", codeShape, NHibernateUtil.Custom(typeof(MsSql2008GeographyType)));
however this try also raise an exception:
Could not execute query [select [shape] from [table] where (?).STIntersects([shape]) = 1")] Name:codeShape - Value:POLYGON((30 40, ...))
and the inner exception is:
The specified input does not represent a valid geography instance.
That's despite codeShape.IsValid returns true.
When I run this query directly in sql server I get the expected result.
any ideas or solutions?
Thanks.
problem resolved. I had to reverse the points of the polygon.

How to fix "domain error" in SQL Server 2005 when using LOG() function to get product of set

I have a inline select statement to calculate the product of the set of values.
Since SQL Server 2005 doesn't have a built in Product aggregate function, I am using LOG/EXP to get it.
My select statement is:
(select exp(sum(log(value))) from table where value > 0)
Unfortunately I keep getting the following error:
Msg 3623, Level 16, State 1, Line 1
A domain error occurred.
I've ensured that none of the values are zero or negative so I'm not really sure why this error is occurring. Does anyone have any ideas?
One of the features of the query planner introduced in SQL 2005 is that, in some circumstances where the table statistics indicate it will be more efficient, the WHERE clause of a statement will be processed after the SELECT clause.
(I can't find the Books On-Line reference for this right now).
I suspect this is what is happening here. You either need to exclude the rows where value = 0 before carrying out the calculation - the most reliable way being to store the rows you need in a temporary (#) table - or to modify your query to handle zero internally:
SELECT EXP(SUM(LOG(ISNULL(NULLIF(VALUE,0),1)))) AS result
FROM [table]
The NULLIF\ISNULL pair I have added to your query substitutes 1 for 0 - I think this will work, but you will need to test it on your data.

Resources