用SQL语句写 求平面两点的距离.....

刘欣的博客 2008-04-29 03:38:54
已经知道平面2点,x1,y1,x2,y2

在sqlserver中,怎么实现存储过程中或调函数就2点的距离?

谢谢
...全文
188 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
刘欣的博客 2008-04-29
  • 打赏
  • 举报
回复
CREATE FUNCTION pdistance(@x1 int,@y1 int,@x2 int,@y2 int) RETURNS int
as
begin
declare @dis as int
set @dis = SQRT((@y2-@y1)*(@y2-@y1) + (@x2-@x1)*(@x2-@x1))
return @dis
end

----------------------------------------

SELECT dbo.pdistance(10, 10, 9, 10) AS 距离



已经OK了,我就要个整数的,感谢大家!!!!!!
cson_cson 2008-04-29
  • 打赏
  • 举报
回复
13 楼的肯定不行。
dawugui 2008-04-29
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 ot512csdn 的回复:]
请问各位高手这样可以吗?


CREATE FUNCTION pdistance(@x1 int,@x2 int,@y1 int,@y2 int) RETURNS int
as
begin
declare @sqrt as int
set @sqrt = SQRT((@y2-@y1)*(@y2-@y1) + (@x2-@x1)*(@x2-@x1))
return @sqrt
end
[/Quote]

RETURNS int

改为RETURNS decimal(18,2) 或更多的小数位,因为取平方根后一般是带小数的多.
刘欣的博客 2008-04-29
  • 打赏
  • 举报
回复
请问各位高手这样可以吗?


CREATE FUNCTION pdistance(@x1 int,@x2 int,@y1 int,@y2 int) RETURNS int
as
begin
declare @sqrt as int
set @sqrt = SQRT((@y2-@y1)*(@y2-@y1) + (@x2-@x1)*(@x2-@x1))
return @sqrt
end
zccmy22 2008-04-29
  • 打赏
  • 举报
回复

create proc proc_name
@x1 numeric(10,2),
@x2 numeric(10,2),
@y1 numeric(10,2),
@y2 numeric(10,2)
as
declare @s numeric(12,2)
begin
set @s = SQRT((X2-X1)*(X2-X1) +(Y2-Y1)*(Y2-Y1))
return @s
end

-狙击手- 2008-04-29
  • 打赏
  • 举报
回复
create proc proc_name
@x1 numeric(12,2),
@x2 numeric(12,2),
@y1 numeric(12,2),
@y2 numeric(12,2),
@ret numeric(12,2) output
as
begin

set @ret= SQRT(POWER ((@X1-@X2),2)+POWER ((@Y1-@Y2),2))
end

go
declare @i numeric(12,2)
exec proc_name 3.0,0.0,4.0,2.2,@i output
select @i
drop proc proc_name

/*

--------------
3.50

(所影响的行数为 1 行)
*/

dawugui 2008-04-29
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ot512csdn 的回复:]
我需要一个可以直接调用的....
谢谢
[/Quote]
6楼.
刘欣的博客 2008-04-29
  • 打赏
  • 举报
回复
我需要一个可以直接调用的....
谢谢
-狙击手- 2008-04-29
  • 打赏
  • 举报
回复
晕,是平方,可以 不管正负


create proc proc_name
@x1 numeric(12,2),
@x2 numeric(12,2),
@y1 numeric(12,2),
@y2 numeric(12,2)
as
begin

return SQRT(POWER ((@X1-@X2),2)+POWER ((@Y1-@Y2),2))
end

go
declare @i numeric(12,2)
exec @i =proc_name 3.0,0.0,4.0,0.0
select @i
drop proc proc_name

/*

--------------
5.00

(所影响的行数为 1 行)
*/

-狙击手- 2008-04-29
  • 打赏
  • 举报
回复
create proc proc_name
@x1 numeric(12,2),
@x2 numeric(12,2),
@y1 numeric(12,2),
@y2 numeric(12,2)
as
begin

return SQRT(POWER (abs(@X1-@X2),2)+POWER (abs(@Y1-@Y2),2))
end

go
declare @i numeric(12,2)
exec @i =proc_name 3.0,0.0,4.0,0.0
select @i
drop proc proc_name

/*

--------------
5.00

(所影响的行数为 1 行)
*/
dawugui 2008-04-29
  • 打赏
  • 举报
回复
create table tb(x1 int,y1 int,x2 int,y2 int)
insert into tb values(0,0,1,1)
go

--直接查询
select 距离 = SQRT((y2-y1)*(y2-y1) + (x2-x1)*(x2-x1)) from tb
go
/*
距离
-----------------------------------------------------
1.4142135623730951

(所影响的行数为 1 行)
*/

--创建函数
CREATE FUNCTION get_sqrt(@x1 int,@x2 int,@y1 int,@y2 int) RETURNS decimal(18,2)
as
begin
declare @sqrt as decimal(18,2)
select @sqrt = SQRT((y2-y1)*(y2-y1) + (x2-x1)*(x2-x1)) from tb
return @sqrt
end
go

select * , 距离 = dbo.get_sqrt(x1,x2,y1,y2) from tb
/*
x1 y1 x2 y2 距离
----------- ----------- ----------- ----------- --------------------
0 0 1 1 1.41

(所影响的行数为 1 行)
*/

drop table tb
drop function dbo.get_sqrt

-狙击手- 2008-04-29
  • 打赏
  • 举报
回复
还得加一个判断
areswang 2008-04-29
  • 打赏
  • 举报
回复
关注
-狙击手- 2008-04-29
  • 打赏
  • 举报
回复
create proc proc_name
@x1 numeric(12,2),
@x2 numeric(12,2),
@y1 numeric(12,2),
@y2 numeric(12,2)
as
begin

return SQRT(POWER ((X1-X2),2)+POWER ((Y1-Y2),2))
end
dawugui 2008-04-29
  • 打赏
  • 举报
回复
create table tb(x1 int,y1 int,x2 int,y2 int)
insert into tb values(0,0,1,1)
go

select 距离 = SQRT((y2-y1)*(y2-y1) + (x2-x1)*(x2-x1)) from tb

drop table tb

/*

距离
-----------------------------------------------------
1.4142135623730951

(所影响的行数为 1 行)
*/
dawugui 2008-04-29
  • 打赏
  • 举报
回复
select SQRT((y2-y1)*(y2-y1) + (x2-x1)*(x2-x1)) from tb

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧