SQL Server Error 111: “…must be the first statement in a query batch”

In SQL Server, a batch is a group of one or more T-SQL statements sent at the same time from an application to SQL Server for execution.

If you encounter an error like this:

Msg 111, Level 15, State 1, Line 2
'CREATE VIEW' must be the first statement in a query batch.

It’s probably because you’re combining the statement with other statements in the same batch, which is not allowed in batches.

The first part of the error message will depend on the actual statement that you’re using in your batch. In my case it’s CREATE VIEW, but it could just as easily be CREATE PROCEDURE, CREATE FUNCTION, etc if those are the statements you’re using.

Continue reading

SQL Server Error 110: There are fewer columns in the INSERT statement than values specified in the VALUES clause.

Error message 110 is a commonly encountered error in SQL Server when inserting data into a table. The full error looks like this:

Msg 110, Level 15, State 1, Line 1
There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.

This happens when you specify less columns in the INSERT statement than the number of values that you’re trying to insert with the VALUES clause.

This will occur if you accidentally omit one or more columns from the INSERT statement.

You’d get a similar (but technically different) error if you tried to do the opposite – specify more columns in the INSERT statement than you try to insert.

Continue reading

SQL Server Error 213: Column name or number of supplied values does not match table definition.

Error message 213 is a common error that happens when you try to insert values into a table without explicitly specifying the column names.

The error looks like this:

Msg 213, Level 16, State 1, Line 1
Column name or number of supplied values does not match table definition.

It occurs when you specify the wrong number of values for that table. In other words, the number of values you provide doesn’t match the number of columns in the table.

Continue reading

“Query not allowed in Waitfor” Error 101 in SQL Server

If you’re using the WAITFOR statement in SQL Server, and you get the following error, it’s probably because you’re providing a query as its argument.

Msg 101, Level 15, State 1, Line 1
Query not allowed in Waitfor.

The WAITFOR statement doesn’t accept queries for its “wait for” period. It only accepts a specific time or an interval.

Actually, it does accept RECEIVE statements, but this is only applicable to Service Broker messages, so if you’re not using Service Broker messages, the above error is quite self-explanatory.

Continue reading

SQL Server Error 4104: The multi-part identifier could not be bound.

If you get an error telling you that the “The multi-part identifier could not be bound.”, it usually means that you’re prefixing one or more columns with either a table that isn’t included in your query, or an alias that you haven’t actually assigned to a table.

Fortunately, the error message shows you which multi-part identifier is causing the problem.

Continue reading

SQL Server Error 109: There are more columns in the INSERT statement than values specified in the VALUES clause

This is a commonly encountered error in SQL Server when inserting data into a table. The full error goes like this:

Msg 109, Level 15, State 1, Line 1
There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.

This happens when you specify more columns in the INSERT statement than the number of values that you’re trying to insert with the VALUES clause.

This will occur if you accidentally omit one or more values from the VALUES clause.

You’d get a similar (but technically different) error if you tried to do the opposite – specify fewer columns in the INSERT statement than you try to insert.

Continue reading

How to Fix the “datediff function resulted in an overflow” Error in SQL Server

This article provides a solution to a problem you may occasionally encounter while using the DATEDIFF() function in SQL Server.

If you encounter the following error:

The datediff function resulted in an overflow. The number of dateparts separating two date/time instances is too large. Try to use datediff with a less precise datepart.

It’s because the return value is too large. The DATEDIFF() function returns its result as an int data type. The reason you got this message is that the return value is too big for the int data type. Fortunately there’s an easy way to fix this.

Continue reading