Ale*_*dro 1 sql sql-server stored-procedures
我有这个存储过程来查找英国邮政编码..
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_postcode_UK]
-- Add the parameters for the stored procedure here
@post_code varchar(10)
AS
DECLARE @intFlag INT
SET @intFlag = 4
WHILE (@intFlag >=1)
BEGIN
SET NOCOUNT ON;
SELECT top 1 id,lat,lng from [postcodes].[dbo].UKREGIONS
where postcode = left(@post_code,@intFlag)
order by newid()
IF @@rowcount > 0
BREAK;
SET @intFlag = @intFlag - 1
END
GO
Run Code Online (Sandbox Code Playgroud)
基本上我有一个主要区域和他们的地理位置的数据库..所以w140df的邮政编码将属于数据库中的W14 ...有时它只回到一个字母..我怎么做,所以存储过程不返回第一批搜索的空白记录
你可以这样做(假设你真的想要最长的匹配,因为它更精确):
SELECT top 1 id,lat,lng from [postcodes].[dbo].UKREGIONS
where postcode IN (
left(@post_code,1), left(@post_code,2),
left(@post_code,3), left(@post_code,4)
)
ORDER BY LEN(postcode) DESC
Run Code Online (Sandbox Code Playgroud)
无需循环.
你也可以使用,但我认为它会表现得更差.
SELECT top 1 id,lat,lng from [postcodes].[dbo].UKREGIONS
where @post_code LIKE postcode+'%'
ORDER BY LEN(postcode) DESC
Run Code Online (Sandbox Code Playgroud)
请注意LIKE子句中参数和列的反转顺序.
| 归档时间: |
|
| 查看次数: |
770 次 |
| 最近记录: |