List events
This example lists events from a specified calendar. Supply the calendar ID to target a calendar; for a single event you can filter by event ID in the WHERE clause or use the EventId parameter.
Tip: Combine with time range parameters (StartTime, EndTime) for filtered results.
Standard SQL query example
This is the base query accepted by the connector. To execute it in SQL Server, you have to pass it to the Data Gateway via a Linked Server. See how to accomplish this using the examples below.
SELECT * FROM Events
WITH (CalendarId='MyCalendarId') -- Specify the calendar ID to list events from
--Get single event by Id
--SELECT * FROM Events WHERE Id='MyEventId' -- Filter by event ID in WHERE clause
--SELECT * FROM Events WITH(EventId='MyEventId') -- Use EventId parameter for direct lookup
Using OPENQUERY in SQL Server
SELECT * FROM OPENQUERY([LS_TO_GOOGLE_CALENDAR_IN_GATEWAY], 'SELECT * FROM Events
WITH (CalendarId=''MyCalendarId'') -- Specify the calendar ID to list events from
--Get single event by Id
--SELECT * FROM Events WHERE Id=''MyEventId'' -- Filter by event ID in WHERE clause
--SELECT * FROM Events WITH(EventId=''MyEventId'') -- Use EventId parameter for direct lookup')
Using EXEC in SQL Server (handling larger SQL text)
The major drawback of OPENQUERY is its inability to incorporate variables within SQL statements.
This often leads to the use of cumbersome dynamic SQL (with numerous ticks and escape characters).
Fortunately, starting with SQL 2005 and onwards, you can utilize the EXEC (your_sql) AT [LS_TO_GOOGLE_CALENDAR_IN_GATEWAY] syntax.
DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * FROM Events
WITH (CalendarId=''MyCalendarId'') -- Specify the calendar ID to list events from
--Get single event by Id
--SELECT * FROM Events WHERE Id=''MyEventId'' -- Filter by event ID in WHERE clause
--SELECT * FROM Events WITH(EventId=''MyEventId'') -- Use EventId parameter for direct lookup'
EXEC (@MyQuery) AT [LS_TO_GOOGLE_CALENDAR_IN_GATEWAY]