Подскажите пожалуйста, как SQL запросм выташить информацию о месте проживания или прописки в соответствии с КЛАДР, чтобы получилась примерно такая таблица:
Табельный номер: 1210
Место проживания: ,663318,Красноярский край,,Норильск г,,Павлова ул,20,,13
Например поле ADDRESSN.SADDRESS1 содержит только часть информации - Павлова ул,20,,13
Заранее благодарен!
Помогите с SQL-запросом
Модераторы: m0p3e, edward_K, Модераторы
А если несколько раз применить запрос типа (в Delphi, правда, у меня ):
где pt - некоторое значение T$PERSONS.F$NREC
Код: Выделить всё
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('SELECT T$STERR.F$SNAME');
ADOQuery1.SQL.Add('FROM T$STERR');
ADOQuery1.SQL.Add('WHERE T$STERR.F$NREC=(');
ADOQuery1.SQL.Add('SELECT T$STERR.F$CPARENT');
ADOQuery1.SQL.Add('FROM T$STERR');
ADOQuery1.SQL.Add('WHERE T$STERR.F$NREC=(');
ADOQuery1.SQL.Add('SELECT T$ADDRESSN.F$CSTERR');
ADOQuery1.SQL.Add('FROM T$ADDRESSN');
ADOQuery1.SQL.Add('WHERE T$ADDRESSN.F$NREC='+pt+'));');
ADOQuery1.Open;
У нас используются несколько функций для работы с каталогом АТД.
Вот вырезал кусок ...
SELECT
TabN = ls.F$tabN,
FIO = p.F$FIO,
LiveIdx = ISNULL(addr.F$SPOSTIND, ''),
LiveRegion = dbo.fnSTErrFromAddress(p.F$LiveAddr,2,1),
LiveRaion = dbo.fnSTErrFromAddress(p.F$LiveAddr,3,1),
LiveCity = CASE
WHEN dbo.fnSTErrFromAddress(p.F$LiveAddr,4,1) <> '' THEN dbo.fnSTErrFromAddress(p.F$LiveAddr,4,1)
ELSE dbo.fnSTErrFromAddress(p.F$LiveAddr,5,1)
END,
LiveStreet = ISNULL(addr.F$SADDRESS2, ''),
LiveHome = ISNULL(addr.F$SHOUSE, ''),
LiveKorpus = ISNULL(addr.F$SBLOCK, ''),
LiveFlat = ISNULL(addr.F$SFLAT, ''),
PropIdx = CASE WHEN dbo.fnGAddress(p.F$PasspAddr) != dbo.fnGAddress(p.F$LiveAddr) THEN
paddr.F$SPOSTIND ELSE ''
END,
PropRegion = dbo.fnSTErrFromAddress(p.F$PasspAddr,2,1),
PropRaion = dbo.fnSTErrFromAddress(p.F$PasspAddr,3,1),
PropCity = CASE WHEN dbo.fnSTErrFromAddress(p.F$PasspAddr,4,1) <> '' THEN dbo.fnSTErrFromAddress(p.F$PasspAddr,4,1)
ELSE dbo.fnSTErrFromAddress(p.F$PasspAddr,5,1) END,
PropStreet = paddr.F$SADDRESS2,
PropHome = paddr.F$SHOUSE,
PropKorpus = paddr.F$SBLOCK,
PropFlat = paddr.F$SFLAT
FROM
T$LSchet ls
INNER JOIN T$Persons p ON p.F$TabNmb = ls.F$TabN
LEFT JOIN T$ADDRESSN addr ON addr.F$NREC = p.F$LiveAddr
LEFT JOIN T$ADDRESSN pAddr ON pAddr.F$NREC = p.F$PasspAddr
WHERE
F$DatUV = 0
ORDER BY FIO
Где fnSTErrFromAddress и fnSTerrParentByType:
CREATE FUNCTION dbo.fnSTerrFromAddress(
@CAddressN VARBINARY(8),
@WType INT,
@ResultType INT -- 1 - F$SName, 2 - F$SysCode
)
RETURNS VARCHAR(1000)
BEGIN
DECLARE @Result VARCHAR(1000), @CSTerr VARBINARY(8)
SELECT
@CSterr = F$CSterr
FROM
T$AddressN
WHERE
F$NREC = @CAddressN
RETURN(dbo.fnSTerrParentByType(@CSTerr, @WType, @ResultType))
END
CREATE FUNCTION dbo.fnSTerrParentByType(
@CSTerr VARBINARY(8),
@WType INT,
@ResultType INT -- 1 - F$SName, 2 - F$SysCode
)
RETURNS VARCHAR(1000)
BEGIN
DECLARE @Result VARCHAR(1000), @CParent VARBINARY(8)
DECLARE @Type INT
SELECT
@Result = CASE @ResultType WHEN 1 THEN t.F$SName WHEN 2 THEN F$SCode END,
@CParent = t.F$CParent,
@Type = SUBSTRING(LTRIM(t.F$WType), 1, 1)
FROM
T$STerr t
WHERE
t.F$NREC = @CSterr
IF @Type != @WType
BEGIN
IF @Type != 0 AND @CParent IS NOT NULL
SELECT @Result = dbo.fnSTerrParentByType(@CParent, @WType, @ResultType)
ELSE
SELECT @Result = ''
END
RETURN(ISNULL(@Result, ''))
END
Вот вырезал кусок ...
SELECT
TabN = ls.F$tabN,
FIO = p.F$FIO,
LiveIdx = ISNULL(addr.F$SPOSTIND, ''),
LiveRegion = dbo.fnSTErrFromAddress(p.F$LiveAddr,2,1),
LiveRaion = dbo.fnSTErrFromAddress(p.F$LiveAddr,3,1),
LiveCity = CASE
WHEN dbo.fnSTErrFromAddress(p.F$LiveAddr,4,1) <> '' THEN dbo.fnSTErrFromAddress(p.F$LiveAddr,4,1)
ELSE dbo.fnSTErrFromAddress(p.F$LiveAddr,5,1)
END,
LiveStreet = ISNULL(addr.F$SADDRESS2, ''),
LiveHome = ISNULL(addr.F$SHOUSE, ''),
LiveKorpus = ISNULL(addr.F$SBLOCK, ''),
LiveFlat = ISNULL(addr.F$SFLAT, ''),
PropIdx = CASE WHEN dbo.fnGAddress(p.F$PasspAddr) != dbo.fnGAddress(p.F$LiveAddr) THEN
paddr.F$SPOSTIND ELSE ''
END,
PropRegion = dbo.fnSTErrFromAddress(p.F$PasspAddr,2,1),
PropRaion = dbo.fnSTErrFromAddress(p.F$PasspAddr,3,1),
PropCity = CASE WHEN dbo.fnSTErrFromAddress(p.F$PasspAddr,4,1) <> '' THEN dbo.fnSTErrFromAddress(p.F$PasspAddr,4,1)
ELSE dbo.fnSTErrFromAddress(p.F$PasspAddr,5,1) END,
PropStreet = paddr.F$SADDRESS2,
PropHome = paddr.F$SHOUSE,
PropKorpus = paddr.F$SBLOCK,
PropFlat = paddr.F$SFLAT
FROM
T$LSchet ls
INNER JOIN T$Persons p ON p.F$TabNmb = ls.F$TabN
LEFT JOIN T$ADDRESSN addr ON addr.F$NREC = p.F$LiveAddr
LEFT JOIN T$ADDRESSN pAddr ON pAddr.F$NREC = p.F$PasspAddr
WHERE
F$DatUV = 0
ORDER BY FIO
Где fnSTErrFromAddress и fnSTerrParentByType:
CREATE FUNCTION dbo.fnSTerrFromAddress(
@CAddressN VARBINARY(8),
@WType INT,
@ResultType INT -- 1 - F$SName, 2 - F$SysCode
)
RETURNS VARCHAR(1000)
BEGIN
DECLARE @Result VARCHAR(1000), @CSTerr VARBINARY(8)
SELECT
@CSterr = F$CSterr
FROM
T$AddressN
WHERE
F$NREC = @CAddressN
RETURN(dbo.fnSTerrParentByType(@CSTerr, @WType, @ResultType))
END
CREATE FUNCTION dbo.fnSTerrParentByType(
@CSTerr VARBINARY(8),
@WType INT,
@ResultType INT -- 1 - F$SName, 2 - F$SysCode
)
RETURNS VARCHAR(1000)
BEGIN
DECLARE @Result VARCHAR(1000), @CParent VARBINARY(8)
DECLARE @Type INT
SELECT
@Result = CASE @ResultType WHEN 1 THEN t.F$SName WHEN 2 THEN F$SCode END,
@CParent = t.F$CParent,
@Type = SUBSTRING(LTRIM(t.F$WType), 1, 1)
FROM
T$STerr t
WHERE
t.F$NREC = @CSterr
IF @Type != @WType
BEGIN
IF @Type != 0 AND @CParent IS NOT NULL
SELECT @Result = dbo.fnSTerrParentByType(@CParent, @WType, @ResultType)
ELSE
SELECT @Result = ''
END
RETURN(ISNULL(@Result, ''))
END