Get the IDENTITY Values that were Generated by an INSERT Statement in SQL Server

The good thing about having IDENTITY columns in SQL Server is that they automatically generate a unique value for every row that’s inserted into a table. This saves us from having to insert our own unique values, and I’m not going to complain about that.

But obvious question you might ask; What if I need to know the value that was generated for each column I just inserted?

Fortunately, the solution may be easier than you think!

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

How to Capture All Rows Deleted by a DELETE Statement in SQL Server

Any time you prepare to delete data from a database, you will probably wonder whether or not to log the deleted data somewhere… just in case.

One way to do this is to construct a SELECT statement to identify the rows you want to delete, and then use it to copy those rows into a separate log table, before doing the actual delete. If you’re using SQL Server, another option is to use the OUTPUT clause. This article discusses the later.

Continue reading

How to Drop a Prepared Statement in MySQL

MySQL provides us with the ability to create multiple prepared statements and run them as many times as we like, while changing the parameter values with each run.

While prepared statements are only available to the session that created them, they are stored in the server. So it’s quite possible that a server could have a build up of lots of prepared statements hanging around once different users have created them.

Continue reading

Identify a Table’s Primary Key’s Index in SQL Server

There are many ways to find the primary key column in SQL Server, but sometimes we might need to identify the name of the primary key’s index.

For example, we may want to identify the index’s name if we need to disable the primary key for some reason (we disable primary keys by disabling their index).

The query below can help if you need to find the name of a primary key’s index for a given table.

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