Author’s Archive: ЈЦЅГЇП €ΘΘΚ

Home / ЈЦЅГЇП €ΘΘΚ
48 Posts

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

Note that this doesn’t work so well with method name obfuscation!

Results
[Operations]
Foo.DoSomething : something 1
Foo.DoSomethingElse : something else 1
Foo.DoSomething : something 2

WCF throws System.ServiceModel.FaultExceptions (FE) between the client and server. The FE is a System.Exception so the client does not need to explicitly catch FaultException, but the FE is missing something. It does not take an InnerException in its constructor, so the server-side chain of exceptions and stack trace data are not available to the client. The typical response to this is “That’s the way it’s supposed to work!”, but if you own/control the consumers of your web service and you want the exception chain and stack trace data, then you’ll want to use FaultException.

The first thing is to decorate your service operations with the FaultContract attribute:

By default, the service implementation will throw all unhandled exceptions back to the client as a regular FaultException. To avoid this, catch exceptions and re-throw them as FaultException as such…

Great! All that detail is on its way to your client.

FaultException and FaultException<>; are derived from System.Exception, so when you don’t care about the detail, just catch Exception. When you want the detail, catch FaultException and inspect …

An instance of ExceptionDetail stores a copy of the underlying Exception data as well as the Exception’s InnerException data, which is also converted to a ExceptionDetail, thus preserving the data of the Exception chain. This means that custom exception types thrown by the server cannot specifically be caught by the client. It also means that the client does not need to reference these exception types. However, the client can determine the type of exception thrown by the server…

The client wishes to detect the ABC.EmptyGuidException thrown by a library called by the service implementation.

Clunky, but attainable.

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.

 

 

Using setvar in T-SQL
Set the values of the two SQLCMD variables.

In this example, the script will only run the shrink if the ‘foo’ database has at least 100 MB to potentially free.

The script runs a shrink on each file of the database until the reduction value drops to zero.

Note: We use the “Simple” recovery mode for non-production databases, and ‘Full’ for those in production. This may affect how much the transaction log size can be reduced, specifically, the time since the last SQL backup may affect the trans-log’s shrinkability when the database is set to use ‘Full’ recovery mode.

Here’s the same thing as a stored procedure.

  • Store hierachical organization data
  • All tables in a multi-tenant database would have a [OrganizationName] field.
  • The Organization table stores the hierarchical relationships for each organization.
  • SQL Server 2008+ supports the hierarchyID data type (not used here), which would make some of the hierarchical manipulations easier. I needed to support SQL Server 2005 as well, so I didn’t have the pleasure.

[C#] version of [dbo].[spSetContext]