简体   繁体   中英

Update table column using table in another SQL Server database while using a spatial function

I would like to update a table column using a spatial function with another database table. This is what I've come up with....

UPDATE FirstDatabase.dbo.track_logs
SET county = t2.CountyName
FROM OtherDatabase.dbo.tblCounty AS t2

WHERE t2.cty_geog.STIntersects(
GEOGRAPHY::STPointFromText('Point(' + FirstDatabase.dbo.track_logs.lng + ' ' +
FirstDatabase.dbo.track_logs.lat + ')', 26915)
)

but I get this error... An expression of non-boolean type specified in a context where a condition is expected, near ')'.

Almost there. This is one of those silly things in TSQL. You need a = 1 .

That's the answer to your question, but I would also be tempted to use POINT(lat, lon, SRID) to address your comment.

UPDATE FirstDatabase.dbo.track_logs
SET county = t2.CountyName
FROM OtherDatabase.dbo.tblCounty AS t2
WHERE t2.cty_geog.STIntersects(geography::Point(FirstDatabase.dbo.track_logs.lat,
                                                FirstDatabase.dbo.track_logs.lng,
                                                26915)) = 1

In a language like C#, you could write something like:

bool val = true;

if (val)
    // do stuff

But in TSQL, you have to write the equivalent to:

bool val = true;

if (val == true)
    // do stuff

This isn't specific to SQL Spatial, of course, you'd also have to specify WHERE bitColumnName = 1 or, as your example illustrates, WHERE bitReturningFunction(args) = 1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM