In SQL Server, we can drop DEFAULT
constraints by using the ALTER TABLE
statement with the DROP CONSTRAINT
argument.
Tag: mssql
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.
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.
3 Ways to Delete Duplicate Rows in SQL Server while Ignoring the Primary Key
The following examples use T-SQL to delete duplicate rows in SQL Server while ignoring the primary key or unique identifier column.
More specifically, the examples delete duplicate rows but keep one. So, given two identical rows, one is deleted and the other remains. This is often referred to as “de-duping” the table, “deduplication” of the table, etc.
Continue readingVALUES Clause in SQL Server
In SQL Server, VALUES
is a table value constructor that specifies a set of row value expressions to be constructed into a table.
The VALUES
clause is often used with INSERT
statements to insert data, but it can also be used as a derived table in either the USING
clause of the MERGE
statement or the FROM
clause.
Example of a Simple Merge Statement in SQL Server
The following example uses T-SQL to merge two tables in SQL Server. It uses the VALUES
table value constructor as the source table.
SQL Server SHOW TABLES Equivalent
Every now and then I find myself typing SHOW TABLES
in SQL Server, expecting to get a list of tables.
That would make perfect sense if I was using MySQL or MariaDB. But SQL Server/T-SQL doesn’t have a SHOW TABLES
statement like MySQL or MariaDB, so it never works. And I keep forgetting. But fortunately, SQL Server does have alternatives.
Here are five options for getting a list of tables in SQL Server. These can be used whenever you’re trying to find that elusive SHOW TABLES
statement in SQL Server.
Fix Msg 512 “Subquery returned more than 1 value” in SQL Server
If you get error Msg 512 that reads “Subquery returned more than 1 value…” in SQL Server, it’s because you’re using a subquery that returns more than one value in a scenario where this is not allowed.
Continue reading