combining sql server spatial and NHibernate with CreateSqlQuery - sql-server

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.

Related

cast MS Access DATE() to Getdate()

I have an ODBC linked table in MS Access to SQL Server. In a query in MS Access, I have the following expression on a field:
DATE() - [DATE] (a field).
I need to change MS Access function DATE(), i.e. current date to SQL Server GETDATE(). How can I cast this in the expression builder in MS Access?
You can't really use the GetDate() t-sql server side value.
However, what you could do, is in place of using linked table to the sql server table?
You could create a view and have this
SELECT *, GETDATE() as MyServerDate FROM tblInvoices.
Now, in your client side query, you have a column called MyServerDate.
And thus you could do this:
SELECT *, (DATE() - [MyServerDate] as MydueDate from MyView
Of course the other way would be to use a pass-though query, but they are read-only. So if the sql is only for a report, or some screen display, then you could consider using a 100% server side query. So, you create the query in Access, but set it as a PT query. As a result, the raw SQL syntax in that Access query you build will have to be t-sql sql, not access sql.

SQL Server 2005 query uses ? which doesn't work in SQL Server 2012

I'm working on an application which queries live data on SQL Server. The user enters a name within '% %' marks to search. Ie. if the user was to search for the owner of a property such as Noble, they would enter %noble%.
We recently upgraded both the application and the SQL Server that stores the data from SQL Server 2005 to SQL Server 2012.
The existing query and the new query are identical:
SELECT aurtvalm.pcl_num
FROM aurtvalm
INNER JOIN rtpostal ON aurtvalm.ass_num = rtpostal.ass_num
WHERE rtpostal.fmt_nm2 LIKE ?
In the old version, the above query produces 16 results. The exact same query in 2012 version produces an error:
Incorrect Syntax near '?'
Has the use of the ? symbol changed since SQL Server 2005?
That because you have incorrect syntax. You have to use parameter instead of question mark. Something like:
SELECT aurtvalm.pcl_num
FROM aurtvalm
INNER JOIN rtpostal ON aurtvalm.ass_num = rtpostal.ass_num
WHERE rtpostal.fmt_nm2 like #param

Why can't SQL Server database name begin with a number if I run a query?

Recently I found an anomaly with SQL Server database creation. If I create with the sql query
create database 6033SomeDatabase;
It throws an error.
But with the Management Studio UI, I can manually create a database with a name of 6033SomeDatabase.
Is this expected behaviour or is it a bug? Please throw some light on this issue.
Try like this,
IF DB_ID('6033SomeDatabase') IS NULL
CREATE DATABASE [6033SomeDatabase]
I'll try to give you detailed answer.
SQL syntax imposes some restrictions to names of database, tables, and fields. F.e.:
SELECT * FROM SELECT, FROM WHERE SELECT.Id = FROM.SelectId
SQL parser wouldn't parse this query. You should rewrite it:
SELECT * FROM [SELECT], [FROM] WHERE [SELECT].Id = [FROM].SelectId
Another example:
SELECT * FROM T1 WHERE Code = 123e10
Is 123e10 the name of column in T1, or is it a numeric constant for 123×1010? Parser doesn't know.
Therefore, there are rules for naming. If you need some strange database or table name, you can use brackets to enclose it.

How to convert 71632.0638353154 to 71,632.06 in SQL Server and SYBASE?

Here I have a question about data converting regarding SQL Server and SYBASE.
Take 71632.0638353154 as an example, how to convert it to 71,632.06 in SQL Server and SYBASE?
I know that there is a convert() function in SQL Server and SYBASE, but every time when I tried to use it to convert that number, the database UI will throw me an exception.
I use sybase UI to excute below SQL instance:
select convert(varchar(30),convert(varchar(8),convert(money,71632.0638353154),1))
but this causes this error:
Insufficient result space for explicit conversion of MONEY value '71,632.06' to a VARCHAR field.
Would anyone tell me how to do it? thx.
I'm unable to test in Sybase but
SELECT CONVERT(VARCHAR(30), CAST(71632.0638353154 AS MONEY),1)
works for me in SQL Server.
The more generic solution is:
select cast(71632.0638353154 as decimal(10, 2))
The cast function is standard SQL and available in most databases. DECIMAL is a built-in data type.

SQL Server 2005 and Datanucleus problem (ntext query)

Started getting errors with SQL Server 2005
This is the first time i've tested our app with Datanucleus 2.x (last test was made with DN 1.x)
I use Eclipse RCP.
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The ntext data type cannot be selected as DISTINCT because it is not comparable.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1493)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPrepared
Statement.java:390)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:340)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:179)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:154)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java
:283)
at org.datanucleus.store.rdbms.SQLController.executeStatementQuery(SQLController.java:463)
at org.datanucleus.store.rdbms.query.JDOQLQuery.performExecuteInternal(JDOQLQuery.java:755)
... 5 more
As the first line mentions, you are trying to do a DISTINCT on a result set that has ntext type fields inside it .. this is not possible..
have a look at Is there any way to DISTINCT or group by a text (or ntext) in SQL Server 2005? for possible solution. (cast the field to nvarchar(max))

Resources