Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server query erroring with 'An object or column name is missing or empty'

Tags:

sql

sql-server

I have the following query in a stored procedure in SQL server:

SELECT  TLI.LESNumber
    ,COUNT(TLT.PL)
INTO #PWCM
FROM #tmpLESImport TLI
INNER JOIN tbl_LES L 
    on TLI.LESNumber=L.NUMB
WHERE ISNULL(L.DELT_FLAG,0)=0
    AND L.SCHL_PK=@SCHL_PK
    AND TLI.PL IS NOT NULL
    AND LEN(TLI.PL)>0
GROUP BY LESNumber 
HAVING COUNT(PL)>1

When the query is run I get the following error:

An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.

Can anyone tell me why? #PWCM does not appear anywhere until this query.

like image 839
Art F Avatar asked Jul 28 '14 15:07

Art F


People also ask

How do you fix an ambiguous error in SQL?

You may see an error that says something like Column 'id' in field list is ambiguous . This error means that there is a field name that is present in more than one table, so it needs to be scoped with the table name to avoid ambiguity: using orders.id instead of just id will resolve the issue.

Is blank function in SQL Server?

Description. The IS NULL condition is used in SQL to test for a NULL value. It returns TRUE if a NULL value is found, otherwise it returns FALSE. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.

Why do it say invalid column name in SQL Server?

An invalid column name error in SQL means that the column name violates the conditions of the column name. If you reference an object that does not exist in the table or the name exists, but you did not reference it correctly, you will get this error.

What are the errors in SQL?

There are two types of errors in SQL Server: system errors and custom errors. System errors can be viewed in the sys. messages system view and are defined by SQL server. Therefore, when a system error occurs, SQL Server will log a system error and may take actions to fix the error.


1 Answers

When you SELECT INTO a table, it creates the table (in this case, a temp table). In order to create a table, each column needs a name, which your count column does not. You just need to give it a name:

SELECT  TLI.LESNumber,COUNT(TLT.PL) [NumRecords]
INTO #PWCM
FROM #tmpLESImport TLI
...
like image 105
Joe Enos Avatar answered Sep 30 '22 07:09

Joe Enos