Wednesday, 20 July 2016

Tuesday, 19 July 2016

SQL Server - drop an index if exists

if exists (select name from sysindexes where name = 'pk_apc_scg')
  drop index tbl_apc_scg.pk_apc_scg

SQL Server - How to create a view with an index

Creating views with index

create view vw_ukhd_english_postcodes
WITH SCHEMABINDING as
  select
    Char_8_ASCII_Index
  from
    ODS.Postcode_Grid_Refs_Eng_Wal_Sco_And_NI_SCD
  where
    is_latest = 1
    and substring(country, 1,1) = 'E'

create unique clustered index pk_ukhd_country on vw_ukhd_english_postcodes(Char_8_ASCII_Index)

SQL Server - How to create a view with an index

Creating views with index

create view vw_ukhd_english_postcodes
WITH SCHEMABINDING as
  select
    Char_8_ASCII_Index
  from
    ODS.Postcode_Grid_Refs_Eng_Wal_Sco_And_NI_SCD
  where
    is_latest = 1
    and substring(country, 1,1) = 'E'

create unique clustered index pk_ukhd_country on vw_ukhd_english_postcodes(Char_8_ASCII_Index)

SQL Server - How to create a view with an index

Creating views with index

create view vw_ukhd_english_postcodes
WITH SCHEMABINDING as
  select
    Char_8_ASCII_Index
  from
    ODS.Postcode_Grid_Refs_Eng_Wal_Sco_And_NI_SCD
  where
    is_latest = 1
    and substring(country, 1,1) = 'E'

create unique clustered index pk_ukhd_country on vw_ukhd_english_postcodes(Char_8_ASCII_Index)

Wednesday, 6 July 2016

How to monitor backup and restore progress in SQL Server using TSQL

SELECT
  session_id as SPID,
  command,
  a.text AS Query,
  start_time,
  percent_complete,
  dateadd(second,estimated_completion_time/1000, getdate()) as estimated_completion_time
FROM
  sys.dm_exec_requests r CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) a
WHERE
  r.command in ('BACKUP DATABASE','RESTORE DATABASE')