SQL Server guide

Create event


This example creates a new event in the specified calendar. Provide start and end times, time zones, summary, and optional details such as location and attendees. Supply the target calendar ID (e.g. primary or a custom calendar ID) in the WITH clause.

Note: Time zones are required for timed events. Attendees can be supplied as a JSON array.

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.

INSERT INTO Events(StartsAt, StartsInTimeZone, EndsAt, EndsInTimeZone, Summary,
  Attendees, Description, Location, Status, Transparency, Visibility)
VALUES ('2021-11-26T16:30:50', 'Europe/Vilnius', '2021-11-27T16:30:50','Europe/Vilnius', 'This is an event',
        null, 'My Description', 'Vilnius', 'confirmed', 'opaque', 'default')
--WITH (CalendarId='primary')		   -- Use 'primary' for the main calendar
--WITH (CalendarId='YourCalendarId')  -- Or specify a custom calendar ID

Using OPENQUERY in SQL Server

SELECT * FROM OPENQUERY([LS_TO_GOOGLE_CALENDAR_IN_GATEWAY], 'INSERT INTO Events(StartsAt, StartsInTimeZone, EndsAt, EndsInTimeZone, Summary,
  Attendees, Description, Location, Status, Transparency, Visibility)
VALUES (''2021-11-26T16:30:50'', ''Europe/Vilnius'', ''2021-11-27T16:30:50'',''Europe/Vilnius'', ''This is an event'',
        null, ''My Description'', ''Vilnius'', ''confirmed'', ''opaque'', ''default'')
--WITH (CalendarId=''primary'')		   -- Use ''primary'' for the main calendar
--WITH (CalendarId=''YourCalendarId'')  -- Or specify a custom calendar ID')

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) = 'INSERT INTO Events(StartsAt, StartsInTimeZone, EndsAt, EndsInTimeZone, Summary,
  Attendees, Description, Location, Status, Transparency, Visibility)
VALUES (''2021-11-26T16:30:50'', ''Europe/Vilnius'', ''2021-11-27T16:30:50'',''Europe/Vilnius'', ''This is an event'',
        null, ''My Description'', ''Vilnius'', ''confirmed'', ''opaque'', ''default'')
--WITH (CalendarId=''primary'')		   -- Use ''primary'' for the main calendar
--WITH (CalendarId=''YourCalendarId'')  -- Or specify a custom calendar ID'
EXEC (@MyQuery) AT [LS_TO_GOOGLE_CALENDAR_IN_GATEWAY]