Scrivere funzioni e stored procedure in SQL Server
Meghan Kwartler
IT Consultant

-- Dichiara variabile e assegna valore iniziale
DECLARE @StartTime as time = '08:00 AM'
-- Dichiara variabile e poi imposta il valore
DECLARE @StartTime AS time
SET @StartTime = '08:00 AM'
-- Dichiara variabile poi imposta il valore
DECLARE @BeginDate as date
SET
@BeginDate = (
SELECT TOP 1 PickupDate
FROM YellowTripData
ORDER BY PickupDate ASC
);
-- Sintassi CAST
CAST ( expression AS data_type [ ( length ) ] )
-- Restituisce expression in base a data_type
-- Dichiara variabile datetime
-- Imposta @BeginDate e @StartTime facendo CAST
DECLARE @StartDateTime as datetime
SET @StartDateTime = CAST(@BeginDate as datetime) + CAST(@StartTime as datetime)
-- Dichiara variabile tabella con due colonne
DECLARE @TaxiRideDates TABLE(
StartDate date,
EndDate date)
-- Inserisce valori statici nella variabile tabella
INSERT INTO @TaxiRideDates (StartDate, EndDate)
SELECT '3/1/2018', '3/2/2018'
-- Inserisce risultato di una query
INSERT INTO @TaxiRideDates(StartDate, EndDate)
SELECT DISTINCT
CAST(PickupDate as date),
CAST(DropOffDate as date)
FROM YellowTripData;
Scrivere funzioni e stored procedure in SQL Server