You could use LEAD(expression,offset)
, but this doesn't work with SELECT *
. In addition to forcing your column collection to be verbose, this method will also end up with the potential to return more rows than there are results - filling in not-found rows with default values such as NULL
.
SELECT TOP(5)
LEAD( d.name , 5 ) OVER( ORDER BY d.name ASC ) [name]
, LEAD( d.database_id , 5 ) OVER( ORDER BY d.name ASC ) [database_id]
, LEAD( d.source_database_id , 5 ) OVER( ORDER BY d.name ASC ) [source_database_id]
, LEAD( d.owner_sid , 5 ) OVER( ORDER BY d.name ASC ) [owner_sid]
, LEAD( d.create_date , 5 ) OVER( ORDER BY d.name ASC ) [create_date]
, LEAD( d.compatibility_level , 5 ) OVER( ORDER BY d.name ASC ) [compatibility_level]
, LEAD( d.collation_name , 5 ) OVER( ORDER BY d.name ASC ) [collation_name]
, LEAD( d.user_access , 5 ) OVER( ORDER BY d.name ASC ) [user_access]
, LEAD( d.user_access_desc , 5 ) OVER( ORDER BY d.name ASC ) [user_access_desc]
, LEAD( d.is_read_only , 5 ) OVER( ORDER BY d.name ASC ) [is_read_only]
, LEAD( d.is_auto_close_on , 5 ) OVER( ORDER BY d.name ASC ) [is_auto_close_on]
, LEAD( d.is_auto_shrink_on , 5 ) OVER( ORDER BY d.name ASC ) [is_auto_shrink_on]
, LEAD( d.state , 5 ) OVER( ORDER BY d.name ASC ) [state]
, LEAD( d.state_desc , 5 ) OVER( ORDER BY d.name ASC ) [state_desc]
, LEAD( d.is_in_standby , 5 ) OVER( ORDER BY d.name ASC ) [is_in_standby]
, LEAD( d.is_cleanly_shutdown , 5 ) OVER( ORDER BY d.name ASC ) [is_cleanly_shutdown]
, LEAD( d.is_supplemental_logging_enabled , 5 ) OVER( ORDER BY d.name ASC ) [is_supplemental_logging_enabled]
, LEAD( d.snapshot_isolation_state , 5 ) OVER( ORDER BY d.name ASC ) [snapshot_isolation_state]
, LEAD( d.snapshot_isolation_state_desc , 5 ) OVER( ORDER BY d.name ASC ) [snapshot_isolation_state_desc]
, LEAD( d.is_read_committed_snapshot_on , 5 ) OVER( ORDER BY d.name ASC ) [is_read_committed_snapshot_on]
, LEAD( d.recovery_model , 5 ) OVER( ORDER BY d.name ASC ) [recovery_model]
, LEAD( d.recovery_model_desc , 5 ) OVER( ORDER BY d.name ASC ) [recovery_model_desc]
, LEAD( d.page_verify_option , 5 ) OVER( ORDER BY d.name ASC ) [page_verify_option]
, LEAD( d.page_verify_option_desc , 5 ) OVER( ORDER BY d.name ASC ) [page_verify_option_desc]
, LEAD( d.is_auto_create_stats_on , 5 ) OVER( ORDER BY d.name ASC ) [is_auto_create_stats_on]
, LEAD( d.is_auto_create_stats_incremental_on , 5 ) OVER( ORDER BY d.name ASC ) [is_auto_create_stats_incremental_on]
, LEAD( d.is_auto_update_stats_on , 5 ) OVER( ORDER BY d.name ASC ) [is_auto_update_stats_on]
, LEAD( d.is_auto_update_stats_async_on , 5 ) OVER( ORDER BY d.name ASC ) [is_auto_update_stats_async_on]
, LEAD( d.is_ansi_null_default_on , 5 ) OVER( ORDER BY d.name ASC ) [is_ansi_null_default_on]
, LEAD( d.is_ansi_nulls_on , 5 ) OVER( ORDER BY d.name ASC ) [is_ansi_nulls_on]
, LEAD( d.is_ansi_padding_on , 5 ) OVER( ORDER BY d.name ASC ) [is_ansi_padding_on]
, LEAD( d.is_ansi_warnings_on , 5 ) OVER( ORDER BY d.name ASC ) [is_ansi_warnings_on]
, LEAD( d.is_arithabort_on , 5 ) OVER( ORDER BY d.name ASC ) [is_arithabort_on]
, LEAD( d.is_concat_null_yields_null_on , 5 ) OVER( ORDER BY d.name ASC ) [is_concat_null_yields_null_on]
, LEAD( d.is_numeric_roundabort_on , 5 ) OVER( ORDER BY d.name ASC ) [is_numeric_roundabort_on]
, LEAD( d.is_quoted_identifier_on , 5 ) OVER( ORDER BY d.name ASC ) [is_quoted_identifier_on]
, LEAD( d.is_recursive_triggers_on , 5 ) OVER( ORDER BY d.name ASC ) [is_recursive_triggers_on]
, LEAD( d.is_cursor_close_on_commit_on , 5 ) OVER( ORDER BY d.name ASC ) [is_cursor_close_on_commit_on]
, LEAD( d.is_local_cursor_default , 5 ) OVER( ORDER BY d.name ASC ) [is_local_cursor_default]
, LEAD( d.is_fulltext_enabled , 5 ) OVER( ORDER BY d.name ASC ) [is_fulltext_enabled]
, LEAD( d.is_trustworthy_on , 5 ) OVER( ORDER BY d.name ASC ) [is_trustworthy_on]
, LEAD( d.is_db_chaining_on , 5 ) OVER( ORDER BY d.name ASC ) [is_db_chaining_on]
, LEAD( d.is_parameterization_forced , 5 ) OVER( ORDER BY d.name ASC ) [is_parameterization_forced]
, LEAD( d.is_master_key_encrypted_by_server , 5 ) OVER( ORDER BY d.name ASC ) [is_master_key_encrypted_by_server]
, LEAD( d.is_query_store_on , 5 ) OVER( ORDER BY d.name ASC ) [is_query_store_on]
, LEAD( d.is_published , 5 ) OVER( ORDER BY d.name ASC ) [is_published]
, LEAD( d.is_subscribed , 5 ) OVER( ORDER BY d.name ASC ) [is_subscribed]
, LEAD( d.is_merge_published , 5 ) OVER( ORDER BY d.name ASC ) [is_merge_published]
, LEAD( d.is_distributor , 5 ) OVER( ORDER BY d.name ASC ) [is_distributor]
, LEAD( d.is_sync_with_backup , 5 ) OVER( ORDER BY d.name ASC ) [is_sync_with_backup]
, LEAD( d.service_broker_guid , 5 ) OVER( ORDER BY d.name ASC ) [service_broker_guid]
, LEAD( d.is_broker_enabled , 5 ) OVER( ORDER BY d.name ASC ) [is_broker_enabled]
, LEAD( d.log_reuse_wait , 5 ) OVER( ORDER BY d.name ASC ) [log_reuse_wait]
, LEAD( d.log_reuse_wait_desc , 5 ) OVER( ORDER BY d.name ASC ) [log_reuse_wait_desc]
, LEAD( d.is_date_correlation_on , 5 ) OVER( ORDER BY d.name ASC ) [is_date_correlation_on]
, LEAD( d.is_cdc_enabled , 5 ) OVER( ORDER BY d.name ASC ) [is_cdc_enabled]
, LEAD( d.is_encrypted , 5 ) OVER( ORDER BY d.name ASC ) [is_encrypted]
, LEAD( d.is_honor_broker_priority_on , 5 ) OVER( ORDER BY d.name ASC ) [is_honor_broker_priority_on]
, LEAD( d.replica_id , 5 ) OVER( ORDER BY d.name ASC ) [replica_id]
, LEAD( d.group_database_id , 5 ) OVER( ORDER BY d.name ASC ) [group_database_id]
, LEAD( d.resource_pool_id , 5 ) OVER( ORDER BY d.name ASC ) [resource_pool_id]
, LEAD( d.default_language_lcid , 5 ) OVER( ORDER BY d.name ASC ) [default_language_lcid]
, LEAD( d.default_language_name , 5 ) OVER( ORDER BY d.name ASC ) [default_language_name]
, LEAD( d.default_fulltext_language_lcid , 5 ) OVER( ORDER BY d.name ASC ) [default_fulltext_language_lcid]
, LEAD( d.default_fulltext_language_name , 5 ) OVER( ORDER BY d.name ASC ) [default_fulltext_language_name]
, LEAD( d.is_nested_triggers_on , 5 ) OVER( ORDER BY d.name ASC ) [is_nested_triggers_on]
, LEAD( d.is_transform_noise_words_on , 5 ) OVER( ORDER BY d.name ASC ) [is_transform_noise_words_on]
, LEAD( d.two_digit_year_cutoff , 5 ) OVER( ORDER BY d.name ASC ) [two_digit_year_cutoff]
, LEAD( d.containment , 5 ) OVER( ORDER BY d.name ASC ) [containment]
, LEAD( d.containment_desc , 5 ) OVER( ORDER BY d.name ASC ) [containment_desc]
, LEAD( d.target_recovery_time_in_seconds , 5 ) OVER( ORDER BY d.name ASC ) [target_recovery_time_in_seconds]
, LEAD( d.delayed_durability , 5 ) OVER( ORDER BY d.name ASC ) [delayed_durability]
, LEAD( d.delayed_durability_desc , 5 ) OVER( ORDER BY d.name ASC ) [delayed_durability_desc]
, LEAD( d.is_memory_optimized_elevate_to_snapshot_on , 5 ) OVER( ORDER BY d.name ASC ) [is_memory_optimized_elevate_to_snapshot_on]
, LEAD( d.is_federation_member , 5 ) OVER( ORDER BY d.name ASC ) [is_federation_member]
, LEAD( d.is_remote_data_archive_enabled , 5 ) OVER( ORDER BY d.name ASC ) [is_remote_data_archive_enabled]
, LEAD( d.is_mixed_page_allocation_on , 5 ) OVER( ORDER BY d.name ASC ) [is_mixed_page_allocation_on]
FROM sys.databases d
ORDER BY d.name asc
As you can see, being a "Function", this also requires re-aliasing column names as well.
ROW_NUMBER()
and limiting withTOP
for the width of the range and aWHERE
condition for a bound of the range is best I've been able to achieve. I've also noticed much better performance if theTOP
clause uses a literal instead of variable – Sporocyte