Skip to content
Advertisement

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

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.

Advertisement

Answer

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
...
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement