SQL Server: temporary tables and constraint names

| category: Programming | author: st

Update. Since the version 2016 SQL Server supports anonymous constraint declaration:

CREATE TABLE #temp_table (
   id1 int NOT NULL,
   id2 int NOT NULL,
   name1 nvarchar(100),
   name2 nvarchar(100),
   PRIMARY KEY (id1, id2),
   UNIQUE (name1, name2)

For earlier versions the solution is below.

Local temporary tables are isolated in the scope …

SQL Server: fractional part of number

| category: Programming | author: st

Unfortunately, SQL Server doesn't have a function like frac to extract the fractional part of number. The method is to subtract the integer part from the origin value.

DECLARE @n1 float, @n2 float, @n3 float;
SELECT @n1 = 123.456, @n2 = 234.567, @n3 = 456.789;
SELECT @n1 - round(@n1, 0 …

SQL: generate random character string

| category: Programming | author: st

Collection of methods to generate random string in a single SQL statement

M1: Recursive string concatenation

On SQL Server the method is limited by 100 characters because of CTE recursion level limitation. The method has the performance issues when using in loops/joins because the table always have 100 rows …

SQL Server: sync two tables with MERGE statement

| category: Programming | author: st
Tags: ,

This small example shows how to synchronize the data of two tables using the MERGE statement

CREATE TABLE table1 (id int NOT NULL PRIMARY KEY, str_value nvarchar(50));
CREATE TABLE table2 (id int NOT NULL PRIMARY KEY, str_value nvarchar(50));
INSERT INTO table1 (id, str_value) VALUES (1, 'Value 1 …

Count LOC with Notepad++

| category: Programming | author: st

Notepad++ can count LOC (lines of code) of source files and give you the estimation of code size.

Press Ctrl+Shift+F shortcut to start "Find in files" dialog. Select "regular expression" search mode and start it.

Notepad++ LOC count

Some regular expressions:

  • \S+\s*$ -- all non-empty lines
  • ^\s*$ -- only empty (white-space) lines …

Using Delphi library with C++ Builder

| category: Programming | author: st
Tags: ,

Since early XE versions C++ Builder supports Delphi units directly added in C++ project and compiles them as well as C/C++ source. However, this approach has several drawbacks:

  • Usually, Delphi files are stored in separated folders of other applications and packages. So your C++ project will point to many …

Interface implementations and multiple inheritance in C++

| category: Programming | author: st

Multiple inheritance of interfaces which are pure abstract classes indeed is not a problem until you need to use the interface implementation in other classes.

Suppose there are two interfaces, the implementation of which will be used in other classes.

class IA
    virtual void m1() = 0;

class IB : public …