Код: Выделить всё
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[CheckAddress]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[CheckAddress]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetAreas]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[GetAreas]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetIndexByDom]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[GetIndexByDom]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetOrgWithINN]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[GetOrgWithINN]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetPlaces]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[GetPlaces]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetRegions]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[GetRegions]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetStreets]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[GetStreets]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetTowns]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[GetTowns]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Get_only_Number]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[Get_only_Number]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[isAdress]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[isAdress]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[toDat]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[toDat]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SellFromPeriod]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[SellFromPeriod]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SetOrg]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[SetOrg]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[NaklPrih]') and OBJECTPROPERTY(id, N'IsView') = 1)
drop view [dbo].[NaklPrih]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[NaklRash]') and OBJECTPROPERTY(id, N'IsView') = 1)
drop view [dbo].[NaklRash]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[E_Clients]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[E_Clients]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Full_Address]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Full_Address]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[KLADR_Areas]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[KLADR_Areas]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[KLADR_DOMA]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[KLADR_DOMA]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[KLADR_Places]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[KLADR_Places]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[KLADR_Regions]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[KLADR_Regions]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[KLADR_Towns]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[KLADR_Towns]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SAVEORG]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[SAVEORG]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[STREET]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[STREET]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[kladr]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[kladr]
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE FUNCTION dbo.CheckAddress (
@post_index varchar(6),
@Region varchar(2),
@Area varchar(40),
@Town varchar(40),
@Place varchar(40),
@Street varchar(40),
@dom varchar(40)
)
RETURNS @ResultTable table (
resvalue varchar(80)
)
AS
begin
--Текущие найденные коды для элементов адреса
declare @post_index_Solved as varchar(6)
Declare @index varchar (6);
Declare @Region_Code varchar(13);
Declare @Area_Code varchar(13);
Declare @Town_Code varchar(13);
Declare @Place_Code varchar(13);
Declare @Street_Code varchar(17);
Set @post_index_Solved='';
--Проверяем наличие такого регона. Описываем курсор для извленчения кодов для следующего элемента
set @region = ltrim(rtrim(isnull(@region,'')))
if len(@region)=1 set @region='0'+@region
declare find_regions cursor for
SELECT [index],
code AS Region_Code,
code AS Area_Code,
code AS Town_Code,
code AS Place_Code
FROM dbo.KLADR_Regions
WHERE (@region=Region_Code)
for read only;
open find_regions;
fetch next from find_regions
into @index, @Region_Code, @Area_Code, @Town_Code, @Place_Code;
declare @isError as varchar(100);
Set @isError=null;
if (@@fetch_status <> 0) Set @isError='Не найден регион.'
else if @index is not null Set @post_index_Solved=@index;
--select @isError
deallocate find_regions
--Теперь проверяем райончик
if (@isError is null) and (ltrim(rtrim(@area))<>'')
begin
declare find_areas cursor for
SELECT [index],
Region_Code + '00000000000' AS Region_Code,
code AS Area_Code,
code AS Town_Code,
code AS Place_Code
FROM dbo.kladr_Areas WHERE ((@region =Region_Code) and ((name+' '+socr)=@area) )
open find_areas;
fetch next from find_areas
into @index, @Region_Code, @Area_Code, @Town_Code, @Place_Code;
if (@@fetch_status <> 0) Set @isError='Не найден район'
else if @index is not null Set @post_index_Solved=@index;
Deallocate find_areas;
end
--Проверяем город
if (@isError is null) and (ltrim(rtrim(@Town))<>'')
begin
declare find_towns cursor for
select
[index],
Region_code + '00000000000' AS Region_code,
Region_code+Area_Code + '00000000' as Area_Code,
code as Town_Code,
code as Place_Code
FROM dbo.KLADR_Towns WHERE (@Region_code=Region_code + '00000000000')
and ((@Area_Code IS NULL) or (@Area_Code=Region_code+Area_Code+ '00000000')) and (ltrim(rtrim((name+' '+socr)))=@town)
open find_towns;
fetch next from find_towns
into @index, @Region_Code, @Area_Code, @Town_Code, @Place_Code;
if (@@fetch_status <> 0) Set @isError='Не найден город'
else if @index is not null Set @post_index_Solved=@index;
Deallocate find_towns;
end
--теперь проверим населенный пункт
if (@isError is null) and (ltrim(rtrim(@place))<>'')
begin
declare find_NasPunkt cursor for
SELECT
[index],
Region_Code + '00000000000' AS Region_code,
Region_Code+Area_Code + '00000000' as Area_Code,
Region_Code+Area_Code+Town_Code + '00000' as Town_Code,
code as Place_Code
FROM dbo.KLADR_Places
WHERE (ltrim(rtrim((name+' '+socr)))=@place) and
@Region_Code= Region_Code + '00000000000'
and @Area_Code = Region_Code+Area_Code + '00000000'
and ((@Town_Code IS NULL) OR (@Town_Code=Region_Code+Area_Code+Town_Code + '00000'))
open find_NasPunkt;
fetch next from find_NasPunkt
into @index, @Region_Code, @Area_Code, @Town_Code, @Place_Code;
if (@@fetch_status <> 0) Set @isError='Не найден населенный пункт'
else if @index is not null Set @post_index_Solved=@index;
Deallocate find_NasPunkt;
end
--Теперь нужно проверить улочку
if (@isError is null) and (ltrim(rtrim(@street))<>'')
begin
declare find_Street cursor for
SELECT [index],
[code]
from street
where (ltrim(rtrim((name+' '+socr)))=@street)
and @Region_Code= SUBSTRING(code, 1, 2) + '00000000000'
and ((@Area_Code IS NULL) OR (@Area_Code = SUBSTRING(code, 1, 5)+ '00000000'))
and ((@Town_code IS NULL) OR (@Town_code=SUBSTRING(code, 1, 8)+ '00000'))
and ((@Place_code IS NULL) OR (@Place_code = SUBSTRING(code, 1, 11)+ '00'))
open find_Street;
fetch next from find_Street
into @index, @Street_Code;
if (@@fetch_status <> 0) Set @isError='Не найдена улица'
else if @index is not null Set @post_index_Solved=@index;
Deallocate find_Street;
end
-- Теперь нужно вычислить индекс по дому
if (@isError is null) and (ltrim(rtrim(@dom))<>'')
begin
declare find_Index cursor for
select [index] from dbo.GetIndexByDom(@Region_Code,@Area_Code, @Town_Code, @Place_Code, @Street_Code,@dom);
open find_Index ;
fetch next from find_Index
into @index;
Deallocate find_Index;
if ltrim(rtrim(@index))='' begin --Если не нашли индекс для данного дома, то попробуем найте его для дома в числовом наименовании т.е. на для "10Б", а "10"
Set @dom=dbo.Get_only_Number(@dom)
declare find_Index cursor for
select [index] from dbo.GetIndexByDom(@Region_Code,@Area_Code, @Town_Code, @Place_Code, @Street_Code,@dom);
open find_Index ;
fetch next from find_Index
into @index;
Deallocate find_Index;
end
if (@index is not null) and (rtrim(ltrim(@index))<>'') and (rtrim(ltrim(@index))<>'NoDom') Set @post_index_Solved=@index;
end;
-- Теперь нужно проверить индекс
if (@isError is null) and (@post_index_Solved<>@post_index )
set @isError='Индекс '+coalesce(@post_index,'')+' не совпадает с вычесленым из Кладра '+coalesce(@post_index_Solved,'')
insert into @ResultTable (resvalue) Values(@isError);
return
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE FUNCTION GetAreas (@Region_Code varchar(13)= null)
RETURNS @RegionsTable table (
Country_code varchar(3),
N_Region varchar(2),
[name] varchar (40),
[socr] varchar (10),
[code] varchar (13),
[index] varchar (6) ,
[gninmb] varchar (4),
[Region_Code] varchar(13),
[Area_Code] varchar(13),
[Town_Code] varchar(13),
[Place_Code] varchar(13),
ShortAreaCode varchar(3)
)
AS
begin
declare @reg_code varchar(2)
set @reg_code = ltrim(rtrim(isnull(@Region_Code,'')))
if len(@reg_code)=1 set @reg_code='0'+@reg_code
insert @RegionsTable
SELECT 643, Region_Code as N_Region,
name, lower(socr), code, [index], gninmb,
Region_Code + '00000000000' AS Region_Code,
code AS Area_Code,
code AS Town_Code,
code AS Place_Code,
Area_Code AS ShortAreaCode
FROM dbo.kladr_Areas
WHERE ((@reg_code=Region_Code))
return
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE FUNCTION dbo.GetIndexByDom (
@Region_Code varchar(13),
@Area_Code varchar(13)=null,
@Town varchar(13)=null,
@Place varchar(13)=null,
@Street varchar(17)=null,
@dom varchar(10)=null)
RETURNS @IndexTable table (
[Index] varchar(80)
)
AS
BEGIN
Declare @ReturnIndex varchar(80)
Declare @isInt_nomerDoma int
Declare @Int_nomerDoma int
Declare @Int_nomerDoma_chetn int
Declare @Primern_Int_nomerDoma int --примерный целочисленный номер дома
Set @ReturnIndex=''
--Set @dom='1F'
Set @dom=Ltrim(Rtrim(@dom))
Set @isInt_nomerDoma=1
declare @len_doma int
Set @len_doma =Len(@dom)
Set @Primern_Int_nomerDoma=0
If (@len_doma >=1) begin
if substring(@dom,1,1) not in ('1','2','3','4','5','6','7','8','9','0')
Set @isInt_nomerDoma=0
else if @isInt_nomerDoma=1 Set @Primern_Int_nomerDoma=cast(substring(@dom,1,1) as int)+@Primern_Int_nomerDoma*10;
end
If (@len_doma >=2) begin
if substring(@dom,2,1) not in ('1','2','3','4','5','6','7','8','9','0') Set @isInt_nomerDoma=0
else if @isInt_nomerDoma=1 Set @Primern_Int_nomerDoma=cast(substring(@dom,2,1) as int)+@Primern_Int_nomerDoma*10;
end
If (@len_doma >=3) begin
if substring(@dom,3,1) not in ('1','2','3','4','5','6','7','8','9','0') Set @isInt_nomerDoma=0
else if @isInt_nomerDoma=1 Set @Primern_Int_nomerDoma=cast(substring(@dom,3,1) as int)+@Primern_Int_nomerDoma*10;
end
If (@len_doma >=4) begin
if substring(@dom,4,1) not in ('1','2','3','4','5','6','7','8','9','0') Set @isInt_nomerDoma=0
else if @isInt_nomerDoma=1 Set @Primern_Int_nomerDoma=cast(substring(@dom,4,1) as int)+@Primern_Int_nomerDoma*10;
end
If (@len_doma >=5) begin
if substring(@dom,5,1) not in ('1','2','3','4','5','6','7','8','9','0') Set @isInt_nomerDoma=0
else if @isInt_nomerDoma=1 Set @Primern_Int_nomerDoma=cast(substring(@dom,5,1) as int)+@Primern_Int_nomerDoma*10;
end
If (@len_doma >=6) begin
if substring(@dom,6,1) not in ('1','2','3','4','5','6','7','8','9','0') Set @isInt_nomerDoma=0
else if @isInt_nomerDoma=1 Set @Primern_Int_nomerDoma=cast(substring(@dom,6,1) as int)+@Primern_Int_nomerDoma*10;
end
If (@len_doma >=7) begin
if substring(@dom,7,1) not in ('1','2','3','4','5','6','7','8','9','0') Set @isInt_nomerDoma=0
else if @isInt_nomerDoma=1 Set @Primern_Int_nomerDoma=cast(substring(@dom,7,1) as int)+@Primern_Int_nomerDoma*10;
end
If (@len_doma >=8) begin
if substring(@dom,8,1) not in ('1','2','3','4','5','6','7','8','9','0') Set @isInt_nomerDoma=0
else if @isInt_nomerDoma=1 Set @Primern_Int_nomerDoma=cast(substring(@dom,8,1) as int)+@Primern_Int_nomerDoma*10;
end
If (@len_doma >=9) begin
if substring(@dom,9,1) not in ('1','2','3','4','5','6','7','8','9','0') Set @isInt_nomerDoma=0
else if @isInt_nomerDoma=1 Set @Primern_Int_nomerDoma=cast(substring(@dom,9,1) as int)+@Primern_Int_nomerDoma*10;
end
If (@len_doma >=10) begin
if substring(@dom,10,1) not in ('1','2','3','4','5','6','7','8','9','0') Set @isInt_nomerDoma=0
else if @isInt_nomerDoma=1 Set @Primern_Int_nomerDoma=cast(substring(@dom,10,1) as int)+@Primern_Int_nomerDoma*10;
end
If @isInt_nomerDoma=1 begin
Set @Int_nomerDoma=cast(@dom as int)
if (ROUND(cast(@Int_nomerDoma as float)/2,0)) = (cast(@Int_nomerDoma as float)/2) Set @Int_nomerDoma_chetn=1 else Set @Int_nomerDoma_chetn=0
end
declare @Index as varchar (6);
--set @Region_code = '4300000000000'
--set @Area_Code = '4300000000000'
--set @Town = '4300000100000'
--set @Place = '4300000100000'
--Set @Street = '43000001000048200'
set @Region_code = SUBSTRING(@Region_Code, 1, 2)
set @Area_Code = SUBSTRING(@Area_Code, 3, 3)
set @Town = SUBSTRING(@Town, 6, 3)
set @Place = SUBSTRING(@Place, 9, 3)
set @Street = SUBSTRING(@Street, 12, 4)
declare nomera_domov cursor for
select ltrim(rtrim(name)), [index] from dbo.Kladr_Doma where
@Region_Code = Region_Code and
@Area_Code = Area_Code and
@Town = Town_Code and
@Place = Place_code and
@Street = Street_Code
order by [Index]
for read only;
open nomera_domov;
Declare @Name_diapazonov varchar(40);
fetch next from nomera_domov
into @Name_diapazonov,@ReturnIndex;
declare @isFindIndex int
Set @isFindIndex =0
if @@fetch_status <>0 --Если ни одного дома не найдено, значит надо взять индекс от улицы
begin
set @ReturnIndex='NoDom'
end
while @@fetch_status = 0
begin
--Ну попытаемся проанализоровать чего тут есть. Если не получиться будем использовать первый найденный индекс
--все лучше чем ничего
--Действуем в соответствии с документацией на кладр. . Длина блока не более 40 символов
--стр № 7
--вариант 1 - в блоке содержится номер одного дома или перечень домов разделеных символом ","
--вариант 2 - в блоке содержится интревалы домов. Буду считать что они не начинаются на символы "Н" и "Ч"
--вариант 3 - в блоке содержится комбинация первого и второго варианта
Declare @CurNomer as varchar(40)
Declare @num_zapjat int
while Len(@Name_diapazonov)>0 begin
Declare @begin_num int;
Declare @end_num int;
--Вычленяем все что до зыпятой
Set @num_zapjat=CHARINDEX(',',@Name_diapazonov);
if @num_zapjat=0 Set @CurNomer=@Name_diapazonov;
if @num_zapjat>1 Set @CurNomer=substring(@Name_diapazonov,1,@num_zapjat-1);
-- print @CurNomer
if @num_zapjat<>len(@Name_diapazonov) begin
Set @Name_diapazonov=ltrim(rtrim(substring(@Name_diapazonov,@num_zapjat+1,40)));
end;
--Это вариант номер 1
if @CurNomer =@dom begin
Set @isFindIndex =1;
break;
end;--if
--Это вариант номер 2. Применим лишь в том случае если у нас числовое значение дома
if @isInt_nomerDoma=1 begin
if Len(@CurNomer)>2 begin
if (substring(@CurNomer,1,2)='Н(' or substring(@CurNomer,1,2)='Ч(') and
(substring(@CurNomer,Len(@CurNomer),1)=')') and
(CHARINDEX('-',@CurNomer)>0)
begin
--Теперь выделяем номер. Должно быть число, затем дефис и опять число
-- '-'
set @begin_num=cast(substring(@CurNomer,3,CHARINDEX('-',@CurNomer)-2-1) as int)
set @end_num =cast(substring(@CurNomer, CHARINDEX('-',@CurNomer)+1,
(len(@CurNomer)-(CHARINDEX('-',@CurNomer)+1))
) as int)
if substring(@CurNomer,1,2)='Н(' and @Int_nomerDoma_chetn=0 begin
if (@Int_nomerDoma>=@begin_num and @Int_nomerDoma<=@end_num) or
(@Int_nomerDoma>=@begin_num and @end_num=999) begin
Set @isFindIndex =1;
break;
end
end
if substring(@CurNomer,1,2)='Ч(' and @Int_nomerDoma_chetn=1 begin
if (@Int_nomerDoma>=@begin_num and @Int_nomerDoma<=@end_num) or
(@Int_nomerDoma>=@begin_num and @end_num=999) begin
Set @isFindIndex =1;
break;
end
end
end--if Len(@CurNomer)>2 begin
end;--if
end;-- if @isInt_nomerDoma=1 begin
--Это вариант номер 3. Применим лишь в том случае если у нас числовое значение дома
if @isInt_nomerDoma=1 begin
if Len(@CurNomer)>2 begin
--Если диапазон начался на номер и у ного есть знак тире
if (substring(@CurNomer,1,1)in ('1','2','3','4','5','6','7','8','9','0')) and
(CHARINDEX('-',@CurNomer)>0) and CHARINDEX('-',@CurNomer)<>Len(@CurNomer) and
(substring(@CurNomer,CHARINDEX('-',@CurNomer)+1,1)in ('1','2','3','4','5','6','7','8','9','0'))
begin
--Теперь выделяем номер. Должно быть число, затем дефис и опять число
set @begin_num=cast(substring(@CurNomer,1,CHARINDEX('-',@CurNomer)-1) as int)
set @end_num =cast(substring(@CurNomer, CHARINDEX('-',@CurNomer)+1,
(len(@CurNomer)-CHARINDEX('-',@CurNomer))
) as int)
if ( @Int_nomerDoma>=@begin_num and @Int_nomerDoma<=@end_num) or
(@Int_nomerDoma>=@begin_num and @end_num=999) begin
Set @isFindIndex =1;
break;
end
end--if Len(@CurNomer)>2 begin
end;--if
end;-- if @isInt_nomerDoma=1 begin
if (@num_zapjat=0) or (@num_zapjat=1 and Len(ltrim(rtrim(@Name_diapazonov)))=1) begin
break;
end;--if
if @isFindIndex =1 break;
end;--while
if @isFindIndex =1 break;
fetch next from nomera_domov
into @Name_diapazonov,@ReturnIndex;
end; --while
deallocate nomera_domov;
if @isFindIndex =1 or @ReturnIndex='NoDom'
insert into @IndexTable([Index]) values(@ReturnIndex)
else
insert into @IndexTable([Index]) values('')
return
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE FUNCTION GetOrgWithINN(@INN varchar(15),@KPP varchar(15), @KOD varchar(3))
RETURNS @RetTable table (
[Kod] varchar(3),
[NameOrg] varchar(200),
[INN] varchar(15),
[KPP] varchar(15),
[Region] varchar(2),
[Adress] varchar(200),
[Lic] varchar(50),
[LicBy] varchar(200)
)
AS
begin
insert @RetTable
Select Kod,NameOrg,INN,KPP,Region,Adress,Lic,LicBy from saveorg Where inn=@INN and kpp=@KPP and kod=@KOD
insert @RetTable
Select Kod,NameOrg,INN,KPP,Region,Adress,Lic,LicBy from saveorg Where inn=@INN and kpp=@KPP
return
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE FUNCTION GetPlaces (@Region_Code varchar(13), @Area_Code varchar(13), @Town_Code varchar(13)=null)
RETURNS @PlacesTable table (
Country_code varchar(3),
N_Region varchar(2),
[name] varchar (40),
[socr] varchar (10),
[code] varchar (13),
[index] varchar (6) ,
[gninmb] varchar (4),
[Region_Code] varchar(13),
[Area_Code] varchar(13),
[Town_Code] varchar(13),
[Place_Code] varchar(13)
)
AS
begin
if len(@Region_code) <11
set @Region_code = (SUBSTRING(@Region_Code, 1, 2) + '00000000000')
set @Region_code = (SUBSTRING(@Region_Code, 1, 11) + '00')
set @Area_Code = ltrim(rtrim(isnull(@Area_Code,@Region_code)))
if len(@Area_Code) <11
set @Area_Code = (SUBSTRING(@Area_Code, 1, 2) + '00000000000')
set @Area_Code = (SUBSTRING(@Area_Code, 1, 11) + '00')
set @Town_Code = (SUBSTRING(@Town_Code, 1, 11) + '00')
insert @PlacesTable
SELECT 643,
Region_Code as N_Region,
name, socr, code, [index], gninmb,
Region_Code + '00000000000' AS Region_code,
Region_Code+Area_Code + '00000000' as Area_Code,
Region_Code+Area_Code+Town_Code + '00000' as Town_Code,
code as Place_Code
FROM dbo.KLADR_Places
WHERE
@Region_Code= Region_Code + '00000000000'
and @Area_Code = Region_Code+Area_Code + '00000000'
and ((@Town_Code IS NULL) OR (@Town_Code=Region_Code+Area_Code+Town_Code + '00000'))
return
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE FUNCTION GetRegions (@Region_Code varchar(13) = null)
RETURNS @RegionsTable table (
Country_code varchar(3),
N_Region varchar(2),
[name] varchar (40),
[socr] varchar (10),
[code] varchar (13),
[index] varchar (6) ,
[gninmb] varchar (4),
[Region_Code] varchar(13),
[Area_Code] varchar(13),
[Town_Code] varchar(13),
[Place_Code] varchar(13),
ShortRegionCode varchar(2)
)
AS
begin
declare @reg_code varchar(2)
set @reg_code = ltrim(rtrim(isnull(@Region_Code,'')))
if len(@reg_code)=1 set @reg_code='0'+@reg_code
insert @RegionsTable
SELECT 643,
Region_Code as N_Region,
[name], lower(socr), code, [index], gninmb,
code AS Region_Code,
code AS Area_Code,
code AS Town_Code,
code AS Place_Code,
Region_Code AS ShortRegionCode
FROM dbo.KLADR_Regions
WHERE (@reg_code=Region_Code) or (@reg_code='') or (@reg_code is Null)
return
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE FUNCTION GetStreets ( @Region_Code varchar(13),
@Area_Code varchar(13)=null,
@Town varchar(13)=null,
@Place varchar(13)=null)
RETURNS @StreetsTable table
(
Country_Code varchar(3),
N_Region varchar(2),
[name] varchar (40),
[socr] varchar (10),
[code] varchar (17),
[index] varchar (6) ,
[gninmb] varchar (4)
)
AS
BEGIN
set @Region_code = (SUBSTRING(@Region_Code, 1, 11) + '00')
set @Area_Code = (SUBSTRING(@Area_Code, 1, 11) + '00')
set @Town = (SUBSTRING(@Town, 1, 11) + '00')
set @Place = (SUBSTRING(@Place, 1, 11) + '00')
insert @StreetsTable
select
643,
Region_code as N_Region,
[name] ,
[socr],
[code],
[index],
[gninmb]
from street
where
@Region_Code= SUBSTRING(code, 1, 2) + '00000000000'
and ((@Area_Code IS NULL) OR (@Area_Code = SUBSTRING(code, 1, 5)+ '00000000'))
and ((@Town IS NULL) OR (@Town=SUBSTRING(code, 1, 8)+ '00000'))
and ((@Place IS NULL) OR (@Place = SUBSTRING(code, 1, 11)+ '00'))
return
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE FUNCTION GetTowns (@Region_Code varchar(13),@Area_Code varchar(13))
RETURNS @RetTable table (
Country_Code varchar(3),
N_Region varchar(2),
[name] varchar (40),
[socr] varchar (10),
[code] varchar (13),
[index] varchar (6) ,
[gninmb] varchar (4),
[Region_Code] varchar(13),
[Area_Code] varchar(13),
[Town_Code] varchar(13),
[Place_Code] varchar(13)
)
AS
begin
if len(@Region_code) <11
set @Region_code = (SUBSTRING(@Region_Code, 1, 2) + '00000000000')
set @Region_code = (SUBSTRING(@Region_Code, 1, 11) + '00')
set @Area_Code = ltrim(rtrim(isnull(@Area_Code,@Region_code)))
if len(@Area_Code) <11
set @Area_Code = (SUBSTRING(@Area_Code, 1, 2) + '00000000000')
set @Area_Code = (SUBSTRING(@Area_Code, 1, 11) + '00')
insert @RetTable
SELECT 643,
Region_code as N_Region,
name, socr, code, [index], gninmb,
Region_code + '00000000000' AS Region_code,
Region_code+Area_Code + '00000000' as Area_Code,
code as Town_Code,
code as Place_Code
FROM dbo.KLADR_Towns
WHERE (@Region_code=Region_code + '00000000000')
and ((@Area_Code IS NULL) or (@Area_Code=Region_code+Area_Code+ '00000000'))
return
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE FUNCTION Get_only_Number
(@str_dom varchar(40))
RETURNS varchar(40)
AS
BEGIN
declare @i as integer
Set @i=1
Declare @is_bukva as integer
Set @is_bukva =0
Declare @num_dom_zifra as varchar(40)
Set @is_bukva =0
Set @num_dom_zifra=''
while (@i<=len(@str_dom)) and (@is_bukva=0) begin
if substring(@str_dom,@i,1) not in ('1','2','3','4','5','6','7','8','9','0') begin
Set @is_bukva =1
end
else Set @num_dom_zifra=@num_dom_zifra+substring(@str_dom,@i,1)
set @i=@i+1
end
RETURN (ltrim(rtrim(@num_dom_zifra)))
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE FUNCTION isAdress (@Adress varchar(300))
RETURNS @RetTable table (
Count_rec int)
AS
BEGIN
DECLARE @Region varchar(2)
DECLARE @Area varchar(51)
DECLARE @Town varchar(51)
DECLARE @Place varchar(51)
DECLARE @Street varchar(51)
DECLARE @N int
DECLARE @M int
Set @Region = ''
Set @Area = ''
Set @Town = ''
Set @Place = ''
Set @Street = ''
Set @Adress = ','+@Adress
Set @N = 1
Set @M = 0
While Len(@Adress)>=@N
Begin
if (SubString(@Adress,@N,1) = ',')
Begin
Set @M = @M + 1
End
Else
Begin
If @M = 3 Set @Region = @Region + cast(SubString(@Adress,@N,1) as char(1))
If @M = 4 Set @Area = @Area + cast(SubString(@Adress,@N,1) as char(1))
If @M = 5 Set @Town = @Town + cast(SubString(@Adress,@N,1) as char(1))
If @M = 6 Set @Place = @Place + cast(SubString(@Adress,@N,1) as char(1))
If @M = 7 Set @Street = @Street + cast(SubString(@Adress,@N,1) as char(1))
End
Set @N = @N + 1
end
Insert @RetTable
Select Count(*) From dbo.Full_Address F Where (F.Region = @Region and F.Area = @Area and F.Town = @Town and F.Place = @Place and F.Street = @Street)
RETURN
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE FUNCTION toDat (@dateFrom datetime)
RETURNS datetime AS
BEGIN
return convert(datetime,convert(varchar(12),@dateFrom, 112),112)
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE TABLE [dbo].[Full_Address] (
[Country] [varchar] (3) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[Index] [nvarchar] (6) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[Region] [nvarchar] (2) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[Area] [varchar] (51) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[Town] [varchar] (51) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[Place] [varchar] (51) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[Street] [varchar] (51) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[Full_Adress] [nvarchar] (221) COLLATE Cyrillic_General_CI_AS NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[KLADR_Areas] (
[name] [varchar] (40) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[socr] [varchar] (10) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[code] [varchar] (13) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[index] [varchar] (6) COLLATE Cyrillic_General_CI_AS NULL ,
[gninmb] [varchar] (4) COLLATE Cyrillic_General_CI_AS NULL ,
[Country_Code] [int] NULL ,
[Region_Code] [varchar] (2) COLLATE Cyrillic_General_CI_AS NULL ,
[Area_Code] [varchar] (3) COLLATE Cyrillic_General_CI_AS NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[KLADR_DOMA] (
[name] [varchar] (100) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[korp] [varchar] (100) COLLATE Cyrillic_General_CI_AS NULL ,
[socr] [varchar] (10) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[code] [varchar] (19) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[index] [varchar] (6) COLLATE Cyrillic_General_CI_AS NULL ,
[gninmb] [varchar] (4) COLLATE Cyrillic_General_CI_AS NULL ,
[uno] [varchar] (100) COLLATE Cyrillic_General_CI_AS NULL ,
[ocatd] [varchar] (11) COLLATE Cyrillic_General_CI_AS NULL ,
[Region_code] [varchar] (2) COLLATE Cyrillic_General_CI_AS NULL ,
[Area_Code] [varchar] (3) COLLATE Cyrillic_General_CI_AS NULL ,
[Town_Code] [varchar] (3) COLLATE Cyrillic_General_CI_AS NULL ,
[Place_Code] [varchar] (3) COLLATE Cyrillic_General_CI_AS NULL ,
[Street_Code] [varchar] (4) COLLATE Cyrillic_General_CI_AS NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[KLADR_Places] (
[name] [varchar] (40) COLLATE Cyrillic_General_CI_AS NULL ,
[socr] [varchar] (10) COLLATE Cyrillic_General_CI_AS NULL ,
[code] [varchar] (13) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[index] [varchar] (6) COLLATE Cyrillic_General_CI_AS NULL ,
[gninmb] [varchar] (4) COLLATE Cyrillic_General_CI_AS NULL ,
[Region_Code] [varchar] (2) COLLATE Cyrillic_General_CI_AS NULL ,
[Area_Code] [varchar] (3) COLLATE Cyrillic_General_CI_AS NULL ,
[Town_Code] [varchar] (3) COLLATE Cyrillic_General_CI_AS NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[KLADR_Regions] (
[name] [varchar] (40) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[socr] [varchar] (10) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[code] [varchar] (13) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[index] [varchar] (6) COLLATE Cyrillic_General_CI_AS NULL ,
[gninmb] [varchar] (4) COLLATE Cyrillic_General_CI_AS NULL ,
[Country_Code] [int] NULL ,
[Region_Code] [varchar] (2) COLLATE Cyrillic_General_CI_AS NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[KLADR_Towns] (
[name] [varchar] (40) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[socr] [varchar] (10) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[code] [varchar] (13) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[index] [varchar] (6) COLLATE Cyrillic_General_CI_AS NULL ,
[gninmb] [varchar] (4) COLLATE Cyrillic_General_CI_AS NULL ,
[Region_code] [varchar] (2) COLLATE Cyrillic_General_CI_AS NULL ,
[Area_Code] [varchar] (3) COLLATE Cyrillic_General_CI_AS NULL ,
[Town_Code] [varchar] (3) COLLATE Cyrillic_General_CI_AS NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[SAVEORG] (
[id] [bigint] IDENTITY (1, 1) NOT NULL ,
[Kod] [char] (3) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[NameOrg] [char] (200) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[INN] [char] (15) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[KPP] [char] (15) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[Region] [char] (2) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[Adress] [char] (200) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[Lic] [char] (50) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[LicBy] [char] (200) COLLATE Cyrillic_General_CI_AS NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[STREET] (
[NAME] [nvarchar] (40) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[SOCR] [nvarchar] (10) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[CODE] [nvarchar] (17) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[INDEX] [nvarchar] (6) COLLATE Cyrillic_General_CI_AS NULL ,
[GNINMB] [nvarchar] (4) COLLATE Cyrillic_General_CI_AS NULL ,
[UNO] [nvarchar] (4) COLLATE Cyrillic_General_CI_AS NULL ,
[OCATD] [nvarchar] (11) COLLATE Cyrillic_General_CI_AS NULL ,
[Region_code] [nvarchar] (2) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[Area_code] [nvarchar] (3) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[Town_code] [nvarchar] (3) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[Place_code] [nvarchar] (3) COLLATE Cyrillic_General_CI_AS NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[kladr] (
[name] [char] (40) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[socr] [char] (10) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[code] [char] (13) COLLATE Cyrillic_General_CI_AS NOT NULL ,
[index] [char] (6) COLLATE Cyrillic_General_CI_AS NULL ,
[gninmb] [char] (4) COLLATE Cyrillic_General_CI_AS NULL ,
[uno] [char] (4) COLLATE Cyrillic_General_CI_AS NULL ,
[ocatd] [char] (11) COLLATE Cyrillic_General_CI_AS NULL ,
[status] [char] (1) COLLATE Cyrillic_General_CI_AS NULL
) ON [PRIMARY]
GO
Часть скрипта я вырезал, она не имеет отношения к теме. Так что полностью скрипт может не выполнятся, но в общем и целом, он содержит все что нужно.