Fix “Violation of PRIMARY KEY constraint” in SQL Server (Error 2627)

If you’re getting an error that reads something like “Violation of PRIMARY KEY constraint ‘PK_CatId’. Cannot insert duplicate key in object ‘dbo.Cats’. The duplicate key value is (1)” in SQL Server, it’s because you’re trying to insert a duplicate value into a primary key column.

A primary key cannot contain duplicate values.

To fix this issue, you’ll need to change the value you’re trying to insert into the primary key column.

Continue reading

Fix Error 137 “Must declare the scalar variable” in SQL Server

If you’re getting SQL Server error 137 that goes something like “Must declare the scalar variable…“, it’s probably because you’re referring to a variable that hasn’t been declared.

If the variable has been declared, it’s possible you’re referring to it incorrectly in the code.

When we use a variable in SQL Server, we must declare the variable first.

To fix this issue, declare the variable. Also be sure to use the right syntax when using it.

Continue reading

Fixing Error 1066 When Using the HANDLER Statement in MySQL

If you’re getting MySQL error 1066 which reads something like “1066 (42000): Not unique table/alias: ‘products’” when using the HANDLER statement in MySQL, it could be that you’re trying to open a table that’s already open.

If this is the case, be sure to close the table before trying to open it again. Or simply continue working without opening the table again.

Continue reading

Fix “Column … in field list is ambiguous” in MySQL (Error 1052)

If you’re getting an error that reads something like “1052 (23000): Column ‘name’ in field list is ambiguous” in MySQL, it looks like you could be referencing a column name in a query without qualifying it with the table name.

This can happen when you perform a join between tables that use the same name for one or more columns.

To fix this issue, be sure to qualify column names with the table names when performing joins across tables.

Continue reading

Fix “Syntax ‘*’ is not allowed in schema-bound objects” in SQL Server (Error 1054)

If you’re getting an error that reads “Syntax ‘*’ is not allowed in schema-bound objects” in SQL Server, it appears that you’re using the asterisk wildcard (*) to select all columns when trying to define a schema-bound object (like a schema-bound view).

As the error message states, this is not allowed.

To fix this issue, explicitly name each column.

Continue reading

Fix “Not unique table/alias” in MySQL (Error 1066)

If you’re getting an error that reads something like “ERROR 1066 (42000): Not unique table/alias: ‘d’” in MySQL, it could be that you’re trying to assign a duplicate alias to a table. Or it could be that you’re doing a self-join without assigning table aliases.

Table names and aliases must be unique when doing queries in MySQL.

The error can also happen if you use HANDLER to open a table, but then try to open it again before closing it.

To fix this issue, be sure to use unique table aliases in your query. And if you’re using HANDLER, either close the table or continue working with it (without trying to open it again).

Continue reading

Fixing Error 4121 When Using a CTE with FOR XML in SQL Server

If you’re trying to use a common table expression (CTE) that produces XML in SQL Server, but you’re getting error 4121, which reads something like “Cannot find either column “EmployeeData” or the user-defined function or aggregate “EmployeeData.query”, or the name is ambiguous” it might be that you’re trying to query the XML in the outer query, but your CTE isn’t outputting the XML as an actual xml type.

The FOR XML result must be an xml type in order to process the result on the server.

To fix this issue, make sure the CTE outputs the result using the xml data type.

Continue reading

Fix “Unknown table … in HANDLER” in MySQL (Error 1109)

If you’re getting MySQL error 1109 which reads something like “1109 (42S02): Unknown table ‘products’ in HANDLER” in MySQL, it appears that you’re trying to reference a table that’s not currently open when using MySQL’s HANDLER statement.

This can happen when you try to read from a table that you haven’t yet opened. It can also happen when you’ve assigned an alias to the table but you try to reference it without the alias. And it can happen if you try to close a table that isn’t actually open.

Continue reading

Fix “Column names in each table must be unique…” in SQL Server (Error 2705)

If you’re getting an error that reads something like “Column names in each table must be unique. Column name ‘c1’ in table ‘t1’ is specified more than once” in SQL Server, it appears that you’re trying to create a table with duplicate column names, or you’re trying to add a column to a table that already has a column of that name.

Column names must be unique within each table.

Continue reading

Possible Reason You’re Getting Error 156 When Running a Subquery in SQL Server

Error 156 in SQL Server is a generic error that doesn’t tell us much, other than the fact that we’re using the wrong syntax.

But if you’re running a subquery, and you’re getting error 156, which reads “Incorrect syntax near the keyword ‘WHERE’.” or whatever keyword it’s near, it could be that you haven’t declared an alias for the subquery.

Continue reading