Sometimes there is a need to delete a temporarily created table during a procedure, which may or may not exist. Simply writing, for example, drop table #myTempTable is not enough, because if the table does not exist, the query will not execute and will return an error. The example below shows how to correctly delete temporary tables in such cases:
Window functions are functions that calculate a scalar value based on a window (set of records). This value is determined using the OVER instruction. Example: select regioncode, aolevel, offname, row_number() over(partit…
The following problem arose, in the case of the absence of data in the table, the result of the maximum value query in the field will output a value of null, of course, you can handle the result in the program, but it is significantly easier to assign this functionality to a script.
Adding Records to a Table Ignoring IDENTITY: -- Enable SET IDENTITY_INSERT tableA ON -- You must specify the full list of columns for INSERT: INSERT Into tableA ([id], [c2], [c3], [c4], [c5] ) SELECT [id], [c2], [c3], [c4], [c5] FROM tableB...
Команды манипулирования данными SELECT , UPDATE , DELETE работают сразу с группами строк. Эти группы, вплоть до отдельных строк, можно выбрать с помощью опции WHERE. А если надо перебрать строки некоторой таблицы последовательно, одну за др…
A useful example of changing the encoding (COLLATION) on an already created database. In this example, the Russian encoding is set: Cyrillic_General_CI_AS USE master; GO -- Switching the DB to single-user mode ALTER DATABASE [database_name] SET…
The correct way to delete a temporary table: IF OBJECT_ID('tempdb.dbo.#tmp', 'U') IS NOT NULL DROP TABLE #tmp;
Here is an example of how you can make a script in MSSQL work correctly with dates regardless of regional settings and date format. I personally implemented it in the following way: In the script, write the date as follows: Where data is a string of the following type: - This is the format of the date indicated by the number 120. Therefore, the server will not confuse the month with the day, etc.
Example of creating a SEQUENCE in MSSQL: CREATE SEQUENCE [dbo].[CardNum] AS [bigint] START WITH 1 INCREMENT BY 1 MINVALUE -9223372036854775808 MAXVALUE 9223372036854775807 CACHE GO This script will create a SEQUENCE…
Restarting MSSQL Server on Linux (Ubuntu). Microsoft recommends restarting the server as follows: -- Get service status sudo systemctl status mssql-server -- Stop service sudo systemctl stop mssql-server -- Start service...