Category Archives: SQL Server

Home / SQL Server
48 Posts

 

When we make manual backups we typically do so as insurance before applying updates, or to get a copy of a database for testing. We aren’t making backups to contribute to the backup regime, as that is the purvey of the automated backup jobs. The recommendation is to make out-of-band backups as described in the PDF document. An out-of-band backup can be deleted without causing problems and should be deleted when no longer needed.

We don’t (want to) know the particulars of our self-hosted customers’ backup regime, so it’s best that we not affect it, so out-of-band is the way to go.

Creating-an-out-of-band-SQL-database-backup.pdf

Note: The [Order] field is not unique in the database. The [Namespace] field is for multi-tenancy support.

Using setvar in T-SQL

 

When a VM is created from a template machine that has SQL Server installed, that SQL instance must be told that the machine name has changed.

If a “foo” machine is created from a template named “template”, the T-SQL statement run against the default SQL instance select @@SERVERNAME returns “template” instead of the desired “foo”.

The solution is to execute the following T-SQL and then restart the SQL Server process.

Here’s a C# version of the rename and the SQL Server process restart using PowerShell and the SqlDataClient.

A configuration for SQL Server to prevent outgoing access to networked (off-machine) SQL Server instances from cloud VMs, while allowing management machines to access these instances.

SQL Server configuration for customer VMs

  • All SQL instances to be the default SQL instance on each machine (no named instances)
  • All SQL instances to use only TCP protocol (disable named pipes, and shared memory)
  • All SQL instances use port 1433 (already the case with template VMs)
  • All SQL instances do not run the SQL Browser Service (already the case with template VMs)
  • Management machines with SQL Server would not implement any special firewall rules aside from their current rule to allow incoming SQL requests.

Windows firewall configuration for VMs

Windows firewall properties for Domain, Private, and Public profiles
  • Inbound connections : block by default
  • Outbound connections allow by default
Create the following Windows firewall rules to block outgoing SQL requests:
  • Management machines with SQL Server would not implement these rules
  • DENY TCP remote port 1433,1434, any local port, any network profile, any program
  • DENY UDP remote port 1433,1434, any local port, any network profile, any program
Create the following Windows firewall rules to allow incoming SQL requests:
  • ALLOW TCP local port 1433 any remote port, any network profile, any program
  • ALLOW UDP local port 1433, any remote port, any network profile, any program
Use group policy to prevent users other than domain admins from changing firewall settings.

An alternate solution is to put a single firewall on the network between all machines and configure rules accordingly. This would make central management easier, but complicate the configuration.

 

Implementation #1

#1 Results

Implementation #2

#2 Results

A system to store property values as strings that are coerced and formatted according a data type. Supported data types are defined in the [services].[ValueType] table.

This is an alternative to using multiple typed columns or a single sql_variant type column in the database. All have their benefits. This is just another way handle user-defined typed data.

Partial Schema

Base SQL Functions

Core Coercion Objects

Stored procedures for updating concatenated lists of property values

Query and Update Property Values Manually

C#

Convert string array to string

result = HelloDoctorNameContinueYesterdayTomorrow
result2 = Hello, Doctor, Name, Continue, Yesterday, Tomorrow

Format Decimal as currency

result = $32.95

Format Decimal as string with N places

Optionally Running Debug Code

SQL

Generate Sequential Guids
arguably not the most commonly used capability, but here it is…

Print immediately to the Messages window when running queries in SQL Management Studio

Delete temp table if exists

SQL Server database stuck in “Restoring” state

Disable / Enable triggers and constraints for the database

Clear the cache without re-starting

Check if file exists using T-SQL
Note: The identity under which the SQL Server process is running will require permissions to read the file system. Right-click the folder/file from file explorer, from the “Security” tab, assign appropriate permissions to “NetworkService” (or whatever credential SQL Server is using). Don’t forget to remember not to forget to remember that the file system is seen from the perspective of the SQL Server!

Close all database connections
Using setvar in T-SQL

Other

Excel 2013 Cell Limit
Cell XFD1048576 is the max cell in Excel. It’s “right down” there.
That’s 16,383 columns x 1,048,576 rows.
excel-specifications-and-limits

Excel Guid Formula
=CONCATENATE(DEC2HEX(RANDBETWEEN(0,4294967295),8),”-“,DEC2HEX(RANDBETWEEN(0,65535),4),”-“,DEC2HEX(RANDBETWEEN(16384,20479),4),”-“,DEC2HEX(RANDBETWEEN(32768,49150),4),”-“,DEC2HEX(RANDBETWEEN(0,65535),4),DEC2HEX(RANDBETWEEN(0,4294967295),8))

Remove hiberfil.sys from a server
If it’s a Citrix VM, hibernation must first be enabled in the “firmware” by issuing the following command, and re-start the VM.

xe vm-param-add uuid=<your VM> param-name=platform acpi_s4=true

Run the command interpreter “Run as Administrator” and enter:
powercfg -h off

The chart of accounts (COA) is different for each organization. The COA code segments are expressed as individual columns in the table structure, making it difficult to query based on code values without resorting to dynamic SQL.

A possible solution is, for each table that contains the COA codes, to create a single calculated-persisted column that consolidates the multi-column values into a single delimited column. In this case, the values are consolidated with a caret (^) character. This example determines the formula for the new calculated-persisted field by examining the COA definition in the tblSegmentInfo table, then it creates the new [CodeSequence] column for each target table. The schema is checked to see if the column and its associated index already exist, and if so, it drops them before (re-)creating them. The tables involved only need to be the ones that the integrating application cares about, in this particular case, they are…

  • tblBLTrans – budget detail table
  • tblENTrans – encumbrance detail table
  • tblDLTrans – expense detail table
  • tblGLBLBalance – transaction summary table
  • tblDistCodeDetail – distribution codes, which are named sequences of codes without the GL (0) segment

COA

There are probably way more elegant ways to do this… but “Mission Accomplished”.

The gotcha in all of this is the biweekly (26 payments per year) schedule, because you need to know which week in a given month should be the chosen one. You can’t just assume it’s always the second week of a given month because the second week in that month may land on the first week of a month down the line. The SQL function below starts from a reference date with which it calibrates the start of the bi-weekly pay schedule and calculates the rest of the pay dates through the end date. Most organizations have a payroll system that calculates new pay dates from the last pay date of record (that’s the reference date here). The bottom line is an actual pay date of record must be known from which future pay dates can be calculated. The weekly (52 payments per year) and semi-monthly (24 payments per year) don’t have this problem, though the latter can have pay days that land on weekend days for which special handling is required… an aspect which is included in the T-SQL below.