If you need to do some inserts and updates in MySQL, one option is to run separate INSERT
and UPDATE
statements. Another option is to use the INSERT ... ON DUPLICATE KEY UPDATE
statement. Depending on the scenario, this can be a handy option.
Tag: how to
Fix “An aggregate may not appear in the OUTPUT clause” in SQL Server (Error 158)
If you’re getting an error that reads “An aggregate may not appear in the OUTPUT clause” in SQL Server, it appears that you’re using an aggregate function in the OUTPUT
clause.
Aggregate functions are not allowed in the OUTPUT
clause.
To fix this issue, remove any aggregate functions from the OUTPUT
clause and try applying them to a table or table variable.
Example of PostgreSQL Automatically Creating a Nested Composite Type
Whenever we create a table in PostgreSQL, a composite type is automatically created behind the scenes. This composite type is based on the table that we created. Each column in the table becomes a field in the composite type.
If the table already uses a composite type, then the composite type that PostgreSQL creates will include that type in its definition, thereby creating a situation where we effectively have a nested composite type.
Continue readingFix “Cannot insert the value NULL into column” in SQL Server (Error 515)
If you’re getting an error that reads something like “Cannot insert the value NULL into column ‘CatId’, table ‘demo.dbo.Cats’; column does not allow nulls. INSERT fails” in SQL Server, it’s because you’re trying to insert a NULL value into non-nullable column (i.e. a column with a NOT NULL
constraint).
We can’t insert NULL values into non-nullable columns.
To fix this issue, insert a non-NULL value.
Continue readingUsing INSERT INTO with a CTE in SQL Server
Common Table Expressions (CTEs) are a useful way to simplify complex queries in SQL Server (and other SQL databases). More often than not, we’ll see CTEs that are used by SELECT
statements, but this isn’t necessarily required. CTEs can be followed by SELECT
, INSERT
, UPDATE
, DELETE
, or MERGE
statements.
This article provides a simple example of using INSERT INTO
with a CTE in SQL Server.
How to DROP a Primary Key in SQL Server
Dropping a primary key in any RDBMS is a critical operation that requires careful consideration, especially when there are related foreign keys in other tables.
The decision to drop a primary key is not to be taken lightly.
That said, this article walks through the process of how to drop a primary key in SQL Server, while highlighting important considerations.
Continue readingFix Error “No column name was specified for column…” in SQL Server (Error 8155)
If you’re getting an error that reads something like “No column name was specified for column 2 of ‘SalaryTotals’” in SQL Server, it could be that you’re running a subquery or CTE, but you’re not providing a column name for an aggregate function, or for the XML or JSON result of the CTE.
To fix this issue, use an alias to define a column name for all aggregate functions and XML/JSON outputs in the subquery or CTE. Or if it’s a CTE, you can specify the column name using the CTE syntax.
Continue readingFix “Invalid column name ‘$action'” When Using the OUTPUT Clause in SQL Server (Error 207)
If you’re getting an error that reads “Invalid column name ‘$action’” in SQL Server, it could be that you’re using the $action
argument of the OUTPUT
clause for an operation that doesn’t support this argument.
Or it could be that the operation does support the argument, but you’re referencing it with the wrong syntax.
The $action
argument can only be used with MERGE
operations, and it must be done correctly.
To fix this issue, be sure to only use the $action
argument in MERGE
operations and to reference it with the correct syntax.
Fix “Column, parameter, or variable #1: Cannot find data type” in SQL Server (Error 2715)
If you’re getting SQL Server error 2715, which reads something like “Column, parameter, or variable #1: Cannot find data type SERIAL“, it appears that you’re trying to define a column to have a data type that’s not supported in SQL Server.
To fix this issue, be sure to use a supported data type for all columns.
Continue readingFix “A MERGE statement must be terminated by a semi-colon (;)” in SQL Server (Error 10713)
If you’re getting SQL Server error 10713 that reads “A MERGE statement must be terminated by a semi-colon (;)“, it’s because you’re running a MERGE
statement without terminating it with a semi-colon.
The MERGE
statement requires a semi-colon at the end of it in order to correctly terminate the statement.
To fix this issue, put a semi-colon at the end of your MERGE
statement.