–Compare a table shema from two different tables.

Eine immer wiederkehrende Aufgabe ist der Import oder Export von Daten zweier Tabellen aus meist verschiedemen Datenbanken. Der SQL Code vergleicht die ähnlichen Feldnamen und deren Datentypen.

English:
A recurring task is the import or export of data from two tables from mostly different databases. The SQL code compares the similar field names and their data types (table shema).
Here is a great contribution from mssqltips.com:

https://www.mssqltips.com/sqlservertip/4824/easy-way-to-compare-sql-server-table-schemas/

[pastacode lang=“sql“ manual=“–https%3A%2F%2Fwww.mssqltips.com%2Fsqlservertip%2F4824%2Feasy-way-to-compare-sql-server-table-schemas%2F%0A–By%3A%20Matteo%20Lorini%0A–A%20mapping%20of%20table%20names%20has%20been%20added%2C%20I.Hill%0A%0ADECLARE%20%40TableFromLink%20%20VARCHAR(MAX)%20%3D%20’%5BsourceServer%5D.%5Bdatabase%5D.%5Bdbo%5D.%5BsourceTable%5D’%0ADECLARE%20%40TableToLink%20%20VARCHAR(MAX)%20%3D%20’%5BImportToServer%5D.%5Bdatabase%5D.%5Bdbo%5D.%5BImportToTable%5D’%0ADECLARE%20%40LinkMapping%20%20VARCHAR(MAX)%20%3D%20’%5BTest%5D.%5Bdbo%5D.%5BTempMappingTable%5D’%0A%0ADECLARE%20%40DynSql%20nvarchar(max)%20%3D%20“%0ADECLARE%20%40NewMappingTable%20BIT%20%3D%201%20%0A%0A%0AIF%20%40NewMappingTable%20%3D%201%0ABEGIN%0A%09%09———————————————————————————————————-%0A%09%09–Create%20a%20mapping%20table%20and%20save%20the%20field%20data%0A%09%09IF%20OBJECT_ID(%40LinkMapping%20%2C%20’U‘)%20is%20not%20null%09BEGIN%20SET%20%40DynSql%20%3D%20’DROP%20TABLE%20’%20%2B%20%40LinkMapping%3B%20EXEC(%40DynSql)%3B%20END%0A%09%09SET%20%40DynSql%20%3D%20%0A%09%09’%0A%09%09CREATE%20TABLE%20’%20%2B%20%40LinkMapping%20%2B%20′(%20%0A%09%09%09ColumnName%20VARCHAR(MAX)%20NOT%20NULL%2C%20%0A%09%09%09ColumnNameMapping%20VARCHAR(MAX)%20DEFAULT%20““%2C%0A%09%09%09Import%20BIT%20DEFAULT%201%2C%0A%09%09%09Datatype%20VARCHAR(MAX)%20NOT%20NULL%2C%20%0A%09%09%09IsNullable%20BIT%2C%0A%09%09%09IsIdentity%20BIT%2C%0A%09%09%09MaxLength%20INT%2C%0A%09%09%09collation_name%20VARCHAR(MAX)%20DEFAULT%20““%2C%0A%09%09%09ConvertStr%20VARCHAR(MAX)%20DEFAULT%20““%09%0A%09%09%09)%0A%0A%09%09INSERT%20INTO%20’%20%2B%20%40LinkMapping%20%2B%20’%20(ColumnName%2C%20ColumnNameMapping%2C%20Datatype%2C%20IsNullable%2C%20IsIdentity%2C%20collation_name%20%2CMaxLength)%0A%09%09SELECT%20%0A%09%09%09toTbl.name%20as%20toTbl_ColumnName%2C%20%09%20%0A%09%09%09toTbl.name%20as%20toTbl_ColumnName%2C%0A%09%09%09toTbl.system_type_name%20as%20toTbl_Datatype%2C%20%09%0A%09%09%09toTbl.is_nullable%20as%20toTbl_is_nullable%2C%20%09%09%0A%09%09%09toTbl.is_identity_column%20as%20toTbl_is_identity%2C%0A%09%09%09CASE%20WHEN%20toTbl.collation_name%20IS%20NOT%20NULL%20THEN%20toTbl.collation_name%20ELSE%20““%20END%2C%0A%09%09%09toTbl.max_length%0A%09%09FROM%20sys.dm_exec_describe_first_result_set%20(N“Select%20*%20From%20’%20%2B%20%40TableFromLink%20%2B%20“’%2C%20NULL%2C%200)%20AS%20toTbl%09%09%0A%09%09———————————————————————————————————-%0A%09%09———————————————————————————————————-%0A%09%09’%0A%09%09PRINT%20%40DynSql%0A%09%09EXEC(%40DynSql)%0AEND%0A%0AIF%20OBJECT_ID(‚tempdb..%23%23MappingTable’%2C%20’U‘)%20is%20not%20null%09DROP%20TABLE%20%23%23MappingTable%0ASET%20%40DynSql%20%3D%20’SELECT%20*%20INTO%20%23%23MappingTable%20FROM%20’%20%2B%20%40LinkMapping%3B%20EXEC(%40DynSql)%3B%0A%0A%0A%0ASELECT%20%0A%09%09%0A%09%0A%09Source.ColumnName%20as%20Source_ColumnName%2C%20%09%0A%09Source.ColumnNameMapping%20AS%20SourceMapping%2C%0A%09toTbl.name%20as%20toTbl_ColumnName%2C%0A%09%0A%09CASE%20WHEN%20Source.ColumnNameMapping%20!%3D%20toTbl.name%20THEN%20’X’%20ELSE%20“%20END%20AS%20nameDIff%2C%09%20%0A%09Source.IsNullable%20as%20Source_is_nullable%2C%0A%09toTbl.is_nullable%20as%20toTbl_is_nullable%2C%0A%09CASE%20WHEN%20toTbl.is_nullable%20!%3D%20Source.IsNullable%20THEN%20’X’%20ELSE%20“%20END%20AS%20nullDIff%2C%0A%09Source.Datatype%20as%20Source_Datatype%2C%0A%09toTbl.system_type_name%20as%20toTbl_Datatype%2C%20%09%20%09%0A%09CASE%20WHEN%20Source.Datatype%20%20!%3D%20toTbl.system_type_name%20THEN%20’X’%20ELSE%20“%20END%20AS%20typeDIff%2C%0A%09Source.MaxLength%20AS%20Source_length%2C%0A%09toTbl.max_length%20AS%20toTbl_length%2C%09%09%09%0A%09CASE%20WHEN%20Source.MaxLength%20%20!%3D%20toTbl.max_length%20THEN%20’X’%20ELSE%20“%20END%20AS%20lengthDIff%2C%0A%09Source.IsIdentity%20as%20Source_is_identity%2C%0A%09toTbl.is_identity_column%20as%20toTbl_is_identity%2C%20%09%0A%09CASE%20WHEN%20toTbl.is_identity_column%20!%3D%20Source.IsIdentity%20THEN%20’X’%20ELSE%20“%20END%20AS%20idenDIff%2C%09%0A%09Source.collation_name%20AS%20Source_collation_name%2C%20%0A%09CASE%20WHEN%20toTbl.collation_name%20%20IS%20NOT%20NULL%20THEN%20toTbl.collation_name%20%20ELSE%20“%20END%20AS%20toTbl_collation_name%2C%0A%09CASE%20WHEN%20toTbl.collation_name%20!%3D%20Source.collation_name%20THEN%20’X’%20ELSE%20“%20END%20AS%20collationDIff%0A%0AFROM%20sys.dm_exec_describe_first_result_set%20(N’Select%20*%20From%20’%20%2B%20%40TableToLink%2C%20NULL%2C%200)%20AS%20toTbl%20%0AFULL%20OUTER%20JOIN%20%20%23%23MappingTable%20AS%20Source%20%0AON%20UPPER(toTbl.name)%20%3D%20UPPER(Source.ColumnNameMapping)%20%0AWHERE%20%0A%09%09Source.Import%20%3D%201%20%0A%09%09–AND%0A%09%09–Source.ColumnName%20IS%20NOT%20NULL%20AND%20%0A%09%09–(%0A%09%09–toTbl.name%20IS%20NULL%20OR%0A%09%09–Source.ColumnNameMapping%20IS%20NULL%20OR%0A%09%09–toTbl.name%20!%3D%20Source.ColumnNameMapping%20OR%0A%09%09–toTbl.is_nullable%20!%3D%20Source.IsNullable%20OR%0A%09%09–toTbl.system_type_name%20!%3D%20Source.Datatype%20OR%0A%09%09–toTbl.max_length%20!%3D%20Source.MaxLength%20OR%0A%09%09–toTbl.is_identity_column%20!%3D%20Source.IsIdentity%20OR%0A%09%09–toTbl.collation_name%20!%3D%20Source.collation_name%0A%09%09–)“ message=“compare a table shema in t-sql“ highlight=““ provider=“manual“/]

Code on github
Code on GitHub

-Umzug der Anmeldedaten auf einen neuen SQL Server / Moving the credentials to a new SQL Server (Microsoft TSQL)

Das Skript speichert alle Anmeldedaten in einer (lokalen) Datenbank. In einem zweiten Schritt können diese Daten wieder in einen neuen Server eingelesen werden. Die Anmeldedaten aus dem alten Server können auch über eine Linked-Serververbindung in den neuen Server eingelesen werden.

English:
The script stores all credentials in a (local) database. In a second step, this data can be read into a new server again. The credentials from the old server can also be read into the new server via a linked server connection.

Save the credentials:

[pastacode lang=“sql“ manual=“DECLARE%20%40UserDb%20VARCHAR(200)%20%3D%20’databaseForTheLogindata‘–Database%20in%20which%20the%20data%20is%20stored%0ADECLARE%20%40SaveToUserDb%20BIT%20%3D%201%09%09%09%09%09%09%09–%201-%3E%20save%20the%20data%20in%20the%20local%20database%20%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09–%200-%3E%20Reading%20the%20data%20from%20a%20local%20database%20and%20storing%20in%20the%20server%0A%0A–If%20no%20value%20was%20entered%2C%20then%20the%20data%20are%20stored%20in%20the%20local%20database%0ADECLARE%09%40PartnerServer%20sysname%20%3D%20“%0A–When%20debug%20is%20turned%20on%2C%20no%20data%20are%20written%20to%20the%20server%20but%20only%20as%20output%20text.%0ADECLARE%20%40Debug%20bit%20%3D%201″ message=“Save the credentials in the database“ highlight=““ provider=“manual“/]

In the second step the credentials are transferred from the database to the new server:

[pastacode lang=“sql“ manual=“DECLARE%20%40UserDb%20VARCHAR(200)%20%3D%20’databaseForTheLogindata‘–Database%20in%20which%20the%20data%20is%20stored%0ADECLARE%20%40SaveToUserDb%20BIT%20%3D%200%09%09%09%09%09%09%09–%201-%3E%20save%20the%20data%20in%20the%20local%20database%20%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09–%200-%3E%20Reading%20the%20data%20from%20a%20local%20database%20and%20storing%20in%20the%20server%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09%0A–If%20no%20value%20was%20entered%2C%20then%20the%20data%20are%20stored%20in%20the%20local%20database%0ADECLARE%09%40PartnerServer%20sysname%20%3D%20’NameOfTheLinkedServer’%0A–When%20debug%20is%20turned%20on%2C%20no%20data%20are%20written%20to%20the%20server%20but%20only%20as%20output%20text.%0ADECLARE%20%40Debug%20bit%20%3D%201″ message=“Transfer credentials to the new server“ highlight=““ provider=“manual“/]

Code on github
Code on GitHub

–Bitwise operators to store multiple values in one number (bit flags)

Das folgende Beispiel zeigt, wie in T-SQL, ein Bit in einem Smallint Datentype je Position geschaltet werden kann. Beispiel:

Pos.:F|E|D|C|B|A|9|8|7|6|5|4|3|2|1|0
Wert:0|0|0|0|0|0|0|0|0|0|1|0|0|0|0|1   =    33 (Decimal)
Wert:0|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1   = 32767 (Decimal)

[pastacode lang=“sql“ manual=“%2F*%0AT-SQL%20Bitwise%20operators%20to%20store%20multiple%20values%20in%20one%20number%20(bit%20flags)%0AI.Hill%2C%202015%2C%20mail%40werferstein.org%0A*%2F%0A%0A%0A%0ADECLARE%20%40Pos%20%20tinyint%20%20%3D%2013%09%09%09%09–Bit%20position%20to%20be%20read%20or%20written%09%0ADECLARE%20%40Flag%20bit%20%3D%200%09%09%09%09%09–Flag%20to%20be%20read%20or%20written%0A%0A%0A%0ADECLARE%20%40Set%20bit%20%3D%201%09%09%09%09%09–if%200%20The%20flag%20is%20read%20from%20the%20specified%20bit%20position%20of%20the%20value%0A%09%09%09%09%09%09%09%09%09%09–if%201%20The%20flag%20is%20stored%20at%20the%20specified%20bit%20position%20of%20the%20value%0ADECLARE%20%40optionValue%20smallint%20%20%3D%2032767%09–Value%20in%20which%20the%20bits%20are%20stored%20(only%2015%20bits%2C%20one%20bit%20is%20the%20sign%20bit%20–%3E%20pos.%2016)%0A%0A%0A%0A%0A——————————————————————————–%0A——————————————————————————–%0ADECLARE%20%40Expo%20%20smallint%20%3D%200%0ADECLARE%20%40MinusFlag%20smallint%20%3D%20-32768%0A%0A–In%20range%20%3F%0Aif%20(%40Pos%20%3E%200%20OR%20%40Pos%20%3C%2016)%20%20AND%20(%40optionValue%20%3E%3D%200%20OR%20%40optionValue%20%3C%3D%2032767)%0ABEGIN%09%0A%09%0A%09SET%20%40Expo%20%3D%20POWER%20(%202.0%2C%20%40Pos-1)%0A%0A%09IF%20%40Set%20%3D%200%0A%09–Value%20to%20flag%0A%09BEGIN%0A%09%09–Bit%20auf%20Pos.%20abfragen%0A%09%09SET%20%40Flag%20%3D%20%20%40optionValue%26%20%40Expo%20%0A%09END%0A%09ELSE%0A%09–Flag%20to%20value%0A%09BEGIN%0A%09%09–Turn%20off%20a%20bit%0A%09%09IF%20%40Flag%20%3D%200%20SET%20%20%40optionValue%20%26%3D%20~%40Expo%20%5E%20%40MinusFlag%20%0A%09%09–Turn%20on%20a%20bit%0A%09%09ELSE%20SET%20%40optionValue%20%7C%3D%20%40Expo%09%09%0A%09END%0AEND%0A%0A%0A————————————————————————————————%0A–Output%20of%20the%20values%20as%20a%20binary%0A————————————————————————————————%0ADECLARE%20%40WPOS%20INT%20%3D%2015%0ADECLARE%20%40BIN%20VARCHAR(15)%20%3D%20“%0AWHILE%20%40WPOS%20%3E%200%0ABegin%0A%09SET%20%40Expo%20%3D%20POWER%20(%202.0%2C%20%40WPOS-1)%20%0A%09%0A%09IF%20%40optionValue%20%26%20%40Expo%20%3E%200%20SET%20%40BIN%20%2B%3D%20’1’%0A%09ELSE%20SET%20%40BIN%20%2B%3D%20’0’%0ASET%20%40WPOS%20-%3D%201%0AEND%20%0A————————————————————————————————%0A————————————————————————————————%0APRINT%20’Pos%3A’%0APRINT%20%40Pos%0APRINT%20’Expo’%0APrint%20%40Expo%0APRINT%20’Flag’%0APrint%20%40Flag%0APRINT%20’Value’%0APRINT%20%40optionValue%0APRINT%20%40BIN%0A————————————————————————————————%0A————————————————————————————————%0A%0A%2F*%0A%26%3D%20F%C3%BChrt%20eine%20bitweise%20AND-Operation%20aus%20und%20legt%20den%20urspr%C3%BCnglichen%20Wert%20auf%20das%20Ergebnis%20fest.%09%0A%5E%3D%20F%C3%BChrt%20eine%20bitweise%20exklusive%20OR-Operation%20aus%20und%20legt%20den%20urspr%C3%BCnglichen%20Wert%20auf%20das%20Ergebnis%20fest.%0A%7C%3D%20F%C3%BChrt%20eine%20bitweise%20OR-Operation%20aus%20und%20legt%20den%20urspr%C3%BCnglichen%20Wert%20auf%20das%20Ergebnis%20fest.%20%0A*%2F%0A%0A%20″ message=“Bitweise Operatoren zum Speichern mehrerer Werte in einer Zahl (Bit-Flags)“ highlight=““ provider=“manual“/]