22,207
社区成员
发帖
与我相关
我的任务
分享
--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
create table [test]([id] int,[smx] numeric(5,1),[smy] numeric(5,1))
insert [test]
select 1,1.1,1.5 union all
select 2,2.2,2.6 union all
select 3,3.3,3.7 union all
select 4,4.4,6.0 union all
select 5,5.5,6.1
--写一个存储过程,指定求任意两点的距离:
if OBJECT_ID('pro_test')is not null
drop proc pro_test
go
create proc pro_test
(
@StartPoint int,
@EndPoint int
)
as
declare @x1 numeric(5,1),@y1 numeric(5,1)
declare @x2 numeric(5,1),@y2 numeric(5,1)
select @x1=[smx],@y1=[smy] from test where id=@StartPoint
select @x2=[smx],@y2=[smy] from test where id=@EndPoint
declare @lenth numeric(5,2)
set @lenth=sqrt(SQUARE(@x1-@x2)+SQUARE(@y1-@y2))
select @lenth as 距离
go
exec pro_test 1,5
/*
距离
---------
6.37
*/
select sqrt(9)
--生成测试数据
;with T as (
select 1 as ID ,1.1 as smx,1.5 as smy union all
select 2 as ID ,2.2 as smx,2.6 as smy union all
select 3 as ID ,3.3 as smx,3.7 as smy union all
select 4 as ID ,4.4 as smx,6.0 as smy union all
select 5 as ID ,5.5 as smx,6.1 as smy
),T2 as
(
select a.*,b.smx as smx2,b.smy as smy2 from t a inner join t b on a.id = b.id -1
)
--求线段距离
--select * , SQRT(SQUARE(SMX2-SMX) + SQUARE(SMY2-SMY)) as [线段距离]
--from T2
--求总距离
select sum(SQRT(SQUARE(SMX2-SMX) + SQUARE(SMY2-SMY)) )as [总距离]
from T2