Search notes:

SQL Server

SQL Server is part of the Microsoft data platform.
architecture
data tools
migration assistant
Data Migration assistant

Installing

With Chocolatey:
choco install -y sql-server-2019
TODO: can SSIS be installed with SQL Server in one step? Does it have to be installed in a separate step?
After performing a default installation of SQL Server, it can be connected to with SQL Server Management Studio by using a dot (.) as Server Name if SQL Server is running on the same machine as Management Studio:
If installed to the default loaction, the installation directory is %programFiles%\Microsoft SQL Server.

Management tools

SQL Server Management Studio
SQL Server configuration manager
Commandline tools that interact with SQL Server include
PowerShell module SqlServer
SQL Server Profiler
Database Engine Tuning Advisor (DTA.EXE, DTASHELL.EXE, dtaengine.exe)
The Microsoft.SqlServer.Management namespace provides functionality for SQL Server management.
Data Quality Client
SQL Server Data Tools (SSDT)

Server Mode (?)

PolyBase

PolyBase allows to query big data that is stored in HDFS-compatible Hadoop distributions and file systems (such as HortonWorks, Cloudera and Azure Blob Storage) from T-SQL.
With PolyBase, external tables are referenced in queries as though they were ordinary SQL Server tables.
PolyBase was added to SqlServer in Version 2016.
With Version 2019, PolyBase added new connectors that can link to external data stored in
It is possible to grant or deny access to data in external tables to/from users by adding them to Active Directory Users and Groups.

Backing up the database

backup database[$(db)] to disk='$(backup_file)'

XML

:xml on
select …

Things to get used to

If both sides of a division are integers, the result is also an integer. That is select 9/4 results in 2.
The aggregate function avg(), if applied on an integer data type returns an integer.

R

Enable remote R package managment for SQL Server
sp_execute_external_script

TODO

How can I query database options.
What is the guest account.
Apparently, the SQL-SMO API allows programmers to administer SQL Server in their applications.
sp_who
Show definition of a procedure: exec sp_helptext 'proc_name'.
What is the sysdiagrams table? Why does it have to be excluded manually when selecting from information_schema.tables?
DTSWizard.exe (The SQL Server Import and Export Wizard).
How do I loop over a result set in a query (for r in (select … ) loop)?
Setting columns to not null:
alter table TAB alter column COL integer not null;
How do I extend the length of the returned value in object_definition()? The returned string seems truncated at times.
SQL Server: comparison of object views
Metadata functions
sp_executesql
The Microsoft Container Registry at mcr.microsoft.com is the official container registry for the distribution of Microsoft product containers.
How do I comment a table or a column? Apparently with sp_addextendedproperty:
execute sp_addextendedproperty 'MS_Description', 
   'This is my table comment',
      'user', @CurrentUser, 'table', 'TABLE_1'
      go
And how will I then get the comment from the sys tables?
if object_id('dbo.my_procedure', 'P') is not null
   drop procedure dbo.my_procedure
go
Apparently, in sqlcmd, commands starting with a colon (such as :listvar, are executed immediately, rather than stored in the execution buffer (which is executed with the next GO):
select
:listvar
@@servername
go
c:\> sqlcmd
:setvar name Rene
:setvar bla  moreBla
exec dbo.my_procedure $(name), $(bla)
go
convert(data-type, expression [, format])
Automatic filling of primary key:
create table tq84 (
  id bigint IDENTITY(1, 1),
  …
);
With ansi_nulls off -> null = null is true.

From Oracle to SQL Server

Oracle SQL Server
substr substring
instr charindex
nvl isNull
sysdate getDate
decode choose
raise_application_error raisError
dbms_output.put_line print
dbms_lock.sleep waitfor delay

Create database

create database tq84_db;
create database another_db;
go
use tq84_db
go

use another_db
go
Determine current database name:
select db_name()

Collation

A database can be created in a specific collation:
create database tq84_db
       collate SQL_Latin1_General_CP1_CI_AS;
Determine collation of a database:
select databasepropertyex('tq84_db', 'collation');

VB Script

set oServer = createObject("SQLDmo.SqlServer")  

oServer.loginSecure = true  
oServer.connect "(local)"  

…

oServer.disconnect  
set oServer = nothing  
Apparently obsoleted in next release.

Declaring variables

declare  @var_01    bigint
declare  @var_02       int
declare  @var_03       int           = 42
declare  @var_04    decimal  (18, 6);
declare  @var_05    nvarchar ( max ) = 'abc';
declare  @var_true  bit              =  1   ;
declare   cur_ab    cursor   for select foo, bar from tab;
declare  @tab       table (col_nm int, col_dt datetime); insert into @tab values (…); select * from @tab;

Assigning values to variables

An assignment requires set (or select):
declare @foo int
declare @bar varchar(10)
set    @foo =   42
select @bar = 'baz'

Scope of variables

go apparently deletes or removes previously declared variables.

Concatenation of strings

@string_one + 'string two'
If one of the concatenated elements is null, then the entire resulting string will be null, too. (concat might help here)

Creating an index if it does not exist

if not exists (
  select *
  from sys.indexes
  where
    name=’IX_TQ84_TAB’ AND
    object_id = OBJECT_ID('[dbo].[TQ84_TAB]')
  )
begin
  create nonclustered index IX_TQ84_TAB
    on dbo.TQ84_TAB(COL_1, COL_2)
end

date datatype

set dateformat dmy
Use exec sp_helplanguage to determine available date formats.
cast as date:
select * from tab
where
  cast(col_dt as date) = '2017-08-28'
Apparently, where col_dt = '20170828' works also.
select
  convert(nvarchar(10), getdate(), 112) as iso_date,
  convert(nvarchar(10), getdate(), 108) as hour_minute_second,
  format (@someDate, 'd', 'en-us'         )  date_en_us,
  format (@someDate, 'd', 'de-de'         )  date_de_de,
  format (@someDate, 'yyyy-MM-dd hh:mm:ss') yyyy_MM_dd__hh_mm_ss
  …

T-SQL blocks

begin
declare
  @someVar = 42

  …
end

if

if @a = @b
   stmt
if @a = @b
begin
   stmtOne
   stmtTwo
   stmtThree
end
begin try
  …
end try
begin catch
  select
    error_number   (),
    error_severity (),
    error_state    (),
    error_procedure(),
    error_line     (),
    error_message  ()
  …
end catch

Service accounts

SQL Server Express 2012 seemed to install or use the following accounts
Service Account
SQL Server Database Engine NT Service\MSSQL$SQLEXPRESS
SQL Server Browser NT AUTHORITY\LOCAL SERVICE

Data Directories

Data directories apparently can be specified for
  • System database
  • User databases
  • User database logs
  • Temp databases
  • Temp database logs
  • backups

SMO - SQL Server Management Objects

SQL Server Management Objects allow to manage an SQL Server programatically. Thus, these object complement the functionality of Management Studio.

Schemas

Schemas can be owned by any database principal.
A principal can own multiple schemas
By default, objects in a schema are owned by the security principal that owns the schema. However, ownership can be transferred with alter authorization ….
There are ten predefined schemas. They have the same name as the built-in database users.
Of these ten predefined schemas, the following four cannot be deleted:

Users, roles, princicpals etc.

Roles are either
  • fixed server roles
  • fixed database roles
  • user-defined database roles
The fixed server roles are
  • sysadmin
  • serveradmin
  • securityadmin
  • processadmin
  • setupadmin
  • bulkadmin
  • diskadmin
  • dbcreator
  • public
Everyone belongs to the public fixed server role and receives any permission assigned there.

Security

Extensible Key Management (EKM) module holds symmetric or asymmetric keys outside of SQL Server.
Transparent Data Encryption (TDE) encrypts an entire database with a symmetric key, the so-called database encryption key (DEK). TDE secures against the threat of someone having direct access to the media on which the senstitive data is stroed.
The database encryption key itself is protected by
  • a certificate protected by the database master key of the master database, or
  • by an asymmetric key stored in an EKM.
Database master keys are protected by the Service Master Key. The Service Master Key is created when SQL Server starts up and is encrypted with the Windows Data Protection API (DPAPI).

Components

SQL Server compenents are

Features that support High Availability

Features that support HA include
  • Log shipping
  • Database mirroring
  • Backup compression
  • Database snapshot
  • Always On failover cluster instances
  • Always On availability groups
  • Basic availability groups
  • Automatic read write connection re-routing
  • Online page and file restore
  • Online index create and rebuild
  • Resumable online index rebuilds
  • Online schema change
  • Fast recovery
  • Accelerated database recovery
  • Mirrored backups
  • Hot add memory and CPU
  • Database recovery advisor
  • Encrypted backup
  • Hybrid backup to Windows Azure (backup to URL)
  • Cluster-less availability group
  • Failover servers for disaster recovery
  • Failover servers for high availability
  • Failover servers for disaster recovery in Azure

Scalability and performance feeatures

Scalability and performance features include
  • Columnstore
  • Large object binaries in clustered columnstore indexes
  • Online non-clustered columnstore index rebuild
  • In-Memory Database: In-Memory OLTP
  • In-Memory Database: hybrid buffer pool
  • In-Memory Database: memory-optimized tempdb metadata
  • In-Memory Database: persistent memory support
  • Stretch database
  • Multi-instance support
  • Table and index partitioning
  • Data compression
  • Resource governor
  • Partitioned table parallelism
  • Multiple filestream containers
  • NUMA aware and large page memory and buffer array allocation
  • Buffer pool extension
  • IO resource governance
  • Read-ahead
  • Advanced scanning
  • Delayed durability
  • Intelligent Database: automatic tuning
  • Bulk insert improvements
See also IQP Features

Security features

Security features include
  • Row-level security
  • Always Encrypted
  • Always Encrypted with Secure Enclaves
  • Dynamic data masking
  • Server audit
  • Database audit
  • Transparent database encryption
  • Extensible key management
  • User-defined roles
  • Contained databases
  • Encryption for backups
  • Data classification and auditing

Replication features

Replication features include
  • Heterogeneous subscribers
  • Merge replication
  • Oracle publishing
  • Peer to peer transactional replication
  • Snapshot replication
  • SQL Server change tracking
  • Transactional replication
  • Transactional replication to Azure
  • Transactional replication updateable subscription

Management tools

Management tools include
  • SQL Management Objects (SMO)
  • SQL Assessment API
  • SQL Vulnerability Assessment
  • SQL Configuration Manager
  • SQL CMD (Command Prompt tool)
  • Distributed Replay - Admin Tool
  • Distribute Replay - Client
  • Distributed Replay - Controller
  • SQL Profiler
  • SQL Server Agent
  • Microsoft System Center Operations Manager Management Pack
  • Database Tuning Advisor (DTA)

RDBMS manageability

RDBMS manageability features include
  • User instances
  • LocalDB
  • Dedicated admin connection
  • SysPrep support 1
  • PowerShell scripting support2
  • Support for data-tier application component operations - extract, deploy, upgrade, delete
  • Policy automation (check on schedule and change)
  • Performance data collector
  • Able to enroll as a managed instance in multi-instance management
  • Standard performance reports
  • Plan guides and plan freezing for plan guides
  • Direct query of indexed views (using NOEXPAND hint)
  • Direct query SQL Server Analysis Services
  • Automatic indexed views maintenance
  • Distributed partitioned views
  • Parallel indexed operations
  • Automatic use of indexed view by query optimizer
  • Parallel consistency check
  • SQL Server Utility Control Point
  • Buffer pool extension
  • Master instance for big data cluster
  • Compatibility certification

Development tools

Development tools include

See also

SQL Server: SQL
authentication
administration
T-SQL: functions, stored procedures.
helpers
Transactions
cliconfg.exe is a tool for configuring SQL Server aliases.
sqlps.exe starts a PowerShell session with the SQL Server PowerShell provider.
The registry keys
SQL Server versions (see also Determine database versions).
The provider for SQL Server is SQLOLEDB.
SQL Server editions
Services:
System.Data.SqlClient is the .NET namespace for the Data Provider for SQL Server.
The point in time that SQL Server was last started is recorded in the column sqlserver_start_time of the sys.dm_os_sys_info view.
Azure SQL is a family of Azure services in the Azure cloud that are built on the SQL Server database engine.
Azure SQL Managed Instance is almost 100% compatible with the Enterprise Edition of SQL Server and allows to lift and shift existing SQL Server applications to the cloud.

Links

Developer edition of SQL Server 2016. Free for development and testing.

Index