In SQL Server we can run a query against the sys.computed_columns
system catalog view to return all computed columns and their definitions.
Tag: t-sql
How to Drop a DEFAULT Constraint in SQL Server
In SQL Server, we can drop DEFAULT
constraints by using the ALTER TABLE
statement with the DROP CONSTRAINT
argument.
Fix “The requested range for sequence object ‘…’ exceeds the maximum or minimum limit. Retry with a smaller range.” (Error 11732) in SQL Server
If you’re getting an error that reads something like “The requested range for sequence object ‘Sequence1’ exceeds the maximum or minimum limit. Retry with a smaller range.” in SQL Server, it’s probably because you’re trying to return a value that’s outside the range of the sequence object.
This can happen when you’re using the sp_sequence_get_range
stored procedure to return a range from a sequence object that exceeds the minimum or maximum value for that sequence object, and the sequence object does not have CYCLE
defined. It’s a similar error to error 11728, which can happen when using NEXT VALUE FOR
to generate a new value from a sequence.
Fix Error Msg 11728 “The sequence object ‘…’ has reached its minimum or maximum value.” in SQL Server
If you’re getting an error that reads something like “The sequence object ‘…’ has reached its minimum or maximum value. Restart the sequence object to allow new values to be generated.“, it’s probably because you’re using a sequence that’s reached the minimum or maximum value specified in its definition.
Continue readingFix “NEXT VALUE FOR function cannot be used directly in a statement that contains an ORDER BY clause unless the OVER clause is specified.” in SQL Server (Error Msg 11723)
If you’re getting an error that reads “NEXT VALUE FOR function cannot be used directly in a statement that contains an ORDER BY clause unless the OVER clause is specified” in SQL Server, it’s probably because you’re trying to sort the results of a query that uses the NEXT VALUE FOR
function without using the OVER
clause.
How to Reset a Sequence in SQL Server
In SQL Server, we can use sequences to generate sequence numbers that increment by a specified amount. This means that any new number generated by the sequence will be the next sequential increment as specified in the sequence’s definition.
Normally, this is exactly what we want. We want each number to adhere to the increment that we specified when defining the sequence.
But what if we want to reset the sequence, so that the numbering starts all over again? In other words, we want to restart the sequence from the beginning. Or what if we want to reset the sequence so that it increments in a different range?
Fortunately, we can reset a sequence with the ALTER SEQUENCE
statement.
SQL Server CASE Statement
The SQL Server CASE
statement evaluates a list of conditions and returns one of multiple possible result expressions.
Although it’s commonly known as the “CASE
statement”, the SQL Server documentation lists it as an expression. Plus it’s referred to as the “CASE
expression” in the SQL standard. Some other DBMSs distinguish between the CASE
statement and the CASE
expression. Either way, here’s a quick rundown on how the “CASE
statement” (or CASE
expression) works in SQL Server.
Fix “At least one of the arguments to COALESCE must be an expression that is not the NULL constant” in SQL Server
In SQL Server, if you’re getting error Msg 4127 that reads “At least one of the arguments to COALESCE must be an expression that is not the NULL constant”, it’s probably because all of your arguments to the COALESCE()
expression are the NULL
constant.
To fix this issue, make sure at least one argument is not the NULL
constant.
SQL Server COALESCE() Explained
In SQL Server, the COALESCE()
expression returns its first non-null argument.
The way it works is, we pass a list of arguments to the expression, it evaluates the arguments in order and returns the current value of the first expression that initially doesn’t evaluate to NULL
.
How to Convert a Unix Timestamp to a Date/Time Value in SQL Server
In SQL Server, we can use the following method to return a date and time based on a given Unix timestamp.
The Unix timestamp (also known as Unix Epoch time, Unix time, or POSIX time) is the number of seconds that have elapsed since 00:00:00 Thursday, 1 January 1970, Coordinated Universal Time (UTC).
Continue reading