Spatial index is not working in MS SQL Server - sql-server

I was working with SQL Server 2008 Spatial Data, but I got a weird problem that the Spatial Index created for the tables doesn’t work when I query them with the view which is created based on this table. Following is the scripts I was using:
declare #Geometry1 geometry = geometry::STGeomFromText(
'POINT(937767.89433333278 -230404.864666667)', 102003)
exec sp_executesql
N'SELECT shape FROM view WHERE (#Geometry1.STIntersects(SHAPE)=1);',
N'#Geometry1 geometry', #Geometry1
I googled a lot and found an workaround at http://www.sqlskills.com/blogs/bobb/how-to-ensure-your-spatial-index-is-being-used/ , but seems like this workaround just works when the queried geometry is point type, for polygons, like the script as following:
declare #Geometry2 geometry = geometry::STGeomFromText(
'POLYGON((-2079214.0399 1392052.275,-2079214.0399 -1156112.025,
1981332.1069 -1156112.025,1981332.1069 1392052.275,
-2079214.0399 1392052.275))', 102003)
exec sp_executesql
N'SELECT shape FROM view WHERE (#Geometry2.STIntersects(SHAPE)=1);',
N'#Geometry2 geometry', #Geometry2
The spatial still doesn’t work. Anybody knows how to deal with this situation? Seems like the Microsoft doesn’t give a good instructions about this.
Any response will be appreciated.

Please use the diagnostics stored procedure sp_help_spatial_geography_index_xml and look at its' output.
This is the primary stored procedure you can use to get an idea of how your geospatial index could be used.
Given that your query is so simple, the #query_sample argument to sp_help_spatial_geography_index_xml can be the same as your #geometry parameter. This will tell you some fairly useful information, such as whether the spatial index needs a full scan to evaluate the question.
Another thing to point out: The dynamic management object view sys.dm_db_missing_index_details explicitly states in the documentation that it does not support spatial indexes, ergo SQL Server does not instrument a lot of intelligence around these indexes.

Related

Describe result set in PostgreSQL?

In Microsoft Sql Server, there is a proc sys.sp_describe_first_result_set which takes an arbitrary sql query string and returns data describing the types that would be in the result set, were it to be executed. Note, this is describing the results of a complex query, not a table. MSDN reference
Is there anything similar for Postgres?
I tried to do this by creating a stored procedure to create temperory view then check information_schema.columns before droping the view.
There is at the protocol level - in the form of the Describe message in the extended query protocol.
I'm not aware of any SQL-level equivalent for arbitrary SQL strings. Good idea though; raise it on pgsql-general . I suspect it'd be pretty easy to implement.
In fact, a quick look at exec_describe_statement_message in src/backend/tcop/postgres.c shows that the brains are in SendRowDescriptionMessage in src/backend/access/common/printtup.c. It shouldn't be hard to write a simple C extension to do similar work and produce a resultset. Hopefully a couple of hours work if you're familar with the codebase; the idea would be:
Feed the SQL string into the parser/rewriter/planner to obtain a plan
Find the targetlist of the top level node, if any. This will be the RETURNING node for DML, or the top-level Query tlist for a SELECT.
Feed the tlist into ExecTypeFromTL
Loop over the tupledesc entries, skipping resjunk columns by looking them up in the tlist, and emit a row for each non-resjunk output from the query.

Sybase system procedure - how to get result?

Is there any way of saving the result set of a SYSTEM procedure in Sybase ASE 15?
For example, if i want to get details about all the columns of a table, i would use the following code: sp_columns 'TABLE_NAME'.
I want to save the result into a temporary table or get it by using a cursor to use it for other queries. Is there any way of doing it?
Note: I am aware i could write my query by using the system tables and get the same result, but if this is possible, i would prefer this method.
The system stored procedures are not intended to be used for inputs into other tables/procedures.
If you don't want to write your own queries, you can look at the code behind the stored procedure by using sp_helptext. For system stored procedures you need to be in the sybsystemprocs db.
use sybsystemprocs
go
sp_helptext sp_columns
go
From there you can take a look at what is being queried and just grab what you need.
It's also helpful to take a look at the Sybase ASE System Tables Diagram: This shows all the system tables, and all the relationships between tables.
You can also use proxy tables to store the output of the SP in table.
This example is very helpful

how to write insert stored procedure when tablename and columnnames are unknown?

i want to write a stored procedure in SQL server that will insert records to database in which i want to pass table-name and column-names with their values as arguments to the stored procedure.
i am using asp.net three-tier technology if that helps.
if it's impossible than please tell me some alternatives.
thanks in advance
The best answer is "Don't Do That!"
A relational database is best used for modelling.... relationships and to do that you need to have bounds and a design about what data you are going to store. That means that you (or your ORM) will know what the underlying tables look like.
If you want a place to simply throw objects at then think about using a nosql document store like MongoDB instead.
Build your SQL in a NVARCHAR dynamically, and pass it to stored procedure sp_executesql:
DECLARE #sql NVARCHAR(MAX);
-- build your SQL in the NVARCHAR
EXEC sp_executesql #sql;
This is possible in theory but I’m afraid that’s not really the best solution.
Issues with this approach is that you don’t know many parameters upfront such as column names, how many column names exist, what is their type and such.
This means that you would have to pass parameters as some default type for example nvarchar and then convert this type to specific column type which can lead to performance and other issues.
I’d suggest you try using Entity framework or some other ORM tool to handle inserts for you.

SQL Server spatial and linked servers

I have a SQL Server instance that I've added a linked server to another SQL instance. The table I'm accessing on the linked server contains spatial types. When I try to query the table I receive an error:
Objects exposing columns with CLR types are not allowed in distributed
queries. Please use a pass-through query to access remote object.
If I use OPENQUERY with the same query I get another error:
A severe error occurred on the current command. The results, if any,
should be discarded.
Is there any way to query tables that contain spatial types via linked servers?
One way to work around this is to pass spatial data as NVARCHAR(MAX)
select go=geometry::STGeomFromText(go,0)
from openquery([other\instance],
'select go=convert(nvarchar(max),go) from tempdb.dbo.geom')
note: go is a column name, short for geometry-object
Or using the function instead of explicit cast
select go=geometry::STGeomFromText(go,0)
from openquery([other\instance],
'select go=go.STAsText() from tempdb.dbo.geom')
I came across the same problem, but accepted solution wasn't an option in my case, due to many applications that couldn't be changed to expect a totally different query.
Instead, I think I found a way to cheat the system. On local server run:
CREATE VIEW stage_table
AS
SELECT *
FROM OPENQUERY([REMOTESERVER],'SELECT * FROM [REMOTEDB].[SCHEMA].TARGET_TABLE');
GO
CREATE SYNONYM TARGET_TABLE FOR stage_table;
GO
Voila, you can now simply use
SELECT * FROM TARGET_TABLE;
Which is probably what your applications expect.
Tried the above scenario with local server: SQLEXPRESS 2008 R2, and remote server SQL EXPRESS 2014.
I have another workaround. It doesn't apply to the OP's question since they were trying to select the spatial data. Even if you are not selecting the columns containing spatial data, you'll still get this error. So if you need to query such a table, and do not need to retrieve the spatial data, then you could create a view for the table (selecting only the columns you need, excluding the spatial data columns), then query against that view instead.

Mass change datatype and rename of dependent store procedure variables

I am in the process of optimising my database and I was thinking of changing the datatype for some columns from DATETIME to SMALLDATETIME on my tables.
Is there a system stored procedure that returns both the contents/code of a store procedure and the dependent table which will then allow me to do a join on a filtered list of tables?
Cheers!
EDIT1:
Im looking to programatically rename the stored procedures not track dependencies!
The built-in dependency tracking for SQL isn't very good for this type of work. Two tools come to mind thought...
Red Gate SQL Dependency Tracker - Good for determining all the dependent code
Visual Studio for Database Developers - Contains TSQL Code Analysis which can identify if a piece of data is being treated as an incorrect type.
Red Gate has a free trial on their stuff, which might get you through this job
I answered a simliar question to this (link below) with a sample of a scipt I use to find text in stored procedures (and functions and views). It requires a bit of work, but might help you here.
[How to find data table column reference in stored procedures
[1]: http://How to find data table column reference in stored procedures
If your dependencies in SQL Server are accurate, you can use sys.sql_dependencies with appropriate joins.

Resources