SQL Server guide

Read users (incremental)


Reads users modified after a specified date and time. This example demonstrates incremental data fetching using the get_users_incr endpoint. Similar to tickets, you can use static or dynamic dates for the start_time parameter.

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 get_users_incr
WITH(
	    start_time='2012-01-31T00:00:00' --modified after exact date / time (yyyy-MM-dd or yyyy-MM-ddTHH:mm:ss)
		
	  --start_time='yesterday' --modified after yesterday
	  --start_time='now-5h' --modified after current time minus 5 hours
	  --start_time='today-60s' --modified after today minus 60 seconds
	  --start_time='weekstart' --modified after weekstart
	  --start_time='monthstart-1d' --modified after month start minus 1 day
	  --start_time='yearstart-1d' --modified after year start minus 1 day
	  --start_time='yearstart+1d' --modified after year start plus 1 day
	  --start_time='yearend+1d' --modified after year end plus 1 day
)

Using OPENQUERY in SQL Server

SELECT * FROM OPENQUERY([LS_TO_ZENDESK_IN_GATEWAY], 'SELECT * FROM get_users_incr
WITH(
	    start_time=''2012-01-31T00:00:00'' --modified after exact date / time (yyyy-MM-dd or yyyy-MM-ddTHH:mm:ss)
		
	  --start_time=''yesterday'' --modified after yesterday
	  --start_time=''now-5h'' --modified after current time minus 5 hours
	  --start_time=''today-60s'' --modified after today minus 60 seconds
	  --start_time=''weekstart'' --modified after weekstart
	  --start_time=''monthstart-1d'' --modified after month start minus 1 day
	  --start_time=''yearstart-1d'' --modified after year start minus 1 day
	  --start_time=''yearstart+1d'' --modified after year start plus 1 day
	  --start_time=''yearend+1d'' --modified after year end plus 1 day
)')

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_ZENDESK_IN_GATEWAY] syntax.

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * FROM get_users_incr
WITH(
	    start_time=''2012-01-31T00:00:00'' --modified after exact date / time (yyyy-MM-dd or yyyy-MM-ddTHH:mm:ss)
		
	  --start_time=''yesterday'' --modified after yesterday
	  --start_time=''now-5h'' --modified after current time minus 5 hours
	  --start_time=''today-60s'' --modified after today minus 60 seconds
	  --start_time=''weekstart'' --modified after weekstart
	  --start_time=''monthstart-1d'' --modified after month start minus 1 day
	  --start_time=''yearstart-1d'' --modified after year start minus 1 day
	  --start_time=''yearstart+1d'' --modified after year start plus 1 day
	  --start_time=''yearend+1d'' --modified after year end plus 1 day
)'
EXEC (@MyQuery) AT [LS_TO_ZENDESK_IN_GATEWAY]