Froschkoenig84
Aktiver Benutzer
- Beiträge
- 27
Ist es eigentlich möglich den RESULT einer solchen Abfrage in eine Variable zu schreiben? Der besteht ja aus mehreren Spalten und Zeilen.
Folge dem Video um zu sehen, wie unsere Website als Web-App auf dem Startbildschirm installiert werden kann.
Anmerkung: Diese Funktion ist in einigen Browsern möglicherweise nicht verfügbar.
USE [MyDatabase]
GO
-- user: MN
-- date: 20151214
-- desc: distances check (existing/outdate) -> if not existing then new calculation (insert/update) -> output
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetDistancesBetweenDestinations]
@id INT,
@limit INT = 25,
@outdays INT = 180,
@meters INT = 111045 /* this is the distance between equator and poles / 90° */
--@meters FLOAT = 111045.00
--@kilometers FLOAT = 111.045,
--@miles FLOAT = 69.00,
--@seamiles FLOAT = 60.00,
--@furlongs FLOAT = 552.00,
--@feet FLOAT = 364320
AS
BEGIN
-- // GET DISTANCES //--
SELECT
CASE WHEN id_a = @id THEN id_b ELSE id_a END AS 'id',
distance
INTO
[tempdb].[dbo].[TmpDistancesForDestinations]
FROM
[MyDatabase].[dbo].[Distances]
WHERE
DATEDIFF(DAY, updated, GETDATE()) < @outdays AND
id_a = @id OR id_b = @id AND
id_a <> id_b AND
distance > 0
ORDER BY
distance ASC
;
-- // CHECK DISTANCES //--
IF (SELECT COUNT(*) FROM [tempdb].[dbo].[TmpDistancesForDestinations]) < @limit BEGIN
-- // GET NEW DISTANCES (BY DESTINATIONS) //--
SELECT TOP (@limit)
D.[id] AS 'id',
ROUND(@meters * DEGREES(ACOS(COS(RADIANS(Q.[geo_lat])) * COS(RADIANS(D.[geo_lat])) * COS(RADIANS(Q.[geo_lon]) - RADIANS(D.[geo_lon])) + SIN(RADIANS(Q.[geo_lat])) * SIN(RADIANS(D.[geo_lat])))),0) AS 'distance'
INTO
[tempdb].[dbo].[TmpDestinationDistances]
FROM
[MyDatabase].[dbo].[Destinations] AS D
LEFT JOIN
[MyDatabase].[dbo].[Destinations] AS Q
ON(Q.id = @id)
WHERE
D.[is_country] <> 1 AND
D.[market] = Q.market AND
D.[geo_lat] IS NOT NULL AND D.[geo_lat] <> 0 AND D.[geo_lat] <> Q.[geo_lat] AND
D.[geo_lon] IS NOT NULL AND D.[geo_lon] <> 0 AND D.[geo_lon] <> Q.[geo_lon] AND
D.[count_pois] >= 2 AND
D.[tg_core_region_priority_rank] BETWEEN 1 AND 1000
ORDER BY
'distance' ASC
;
-- // SHOW NEW (TEMP) DISTANCES //--
SELECT TOP (@limit)
*
FROM
[tempdb].[dbo].[TmpDestinationDistances]
ORDER BY
[distance] ASC
;
-- // MERGE NEW (TEMP) DISTANCES INTO (OLD) DISTANCES TABLE //--
MERGE [MyDatabase].[dbo].[Distances] AS TARGET
USING [tempdb].[dbo].[TmpDestinationDistances] AS SOURCE
ON ((TARGET.id_a = @id AND TARGET.id_b = SOURCE.id) OR (TARGET.id_a = SOURCE.id AND TARGET.id_b = @id))
WHEN NOT MATCHED BY TARGET THEN
INSERT(
id_a, id_b, distance
)
VALUES(
@id, SOURCE.id, SOURCE.distance
)
WHEN MATCHED THEN
UPDATE
SET
TARGET.distance = SOURCE.distance,
TARGET.updated = GETDATE()
;
-- // DELETE TEMP DISTANCES //--
DROP TABLE [tempdb].[dbo].[TmpDestinationDistances]
END ELSE BEGIN
-- // SHOW (OLD) DISTANCES //--
SELECT TOP (@limit)
*
FROM
[tempdb].[dbo].[TmpDistancesForDestinations]
ORDER BY
[distance] ASC
;
END
-- // DELETE TEMP DISTANCES //--
DROP TABLE [tempdb].[dbo].[TmpDistancesForDestinations]
END
public static string GetDistancesBetweenDestinations(int Id)
{
SqlConnection conn = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand("dbo.GetDistancesBetweenDestinations", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter IN_Id = cmd.Parameters.Add("@id", SqlDbType.Int, 11);
IN_Id.Value = 12;
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
return rdr[1].ToString();
}