12

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.

3
  • 7
    You need to give COUNT(TLT.PL) a name
    – T I
    Commented Jul 28, 2014 at 15:25
  • The table #PWCM doesn't appear before this because until statement execute it won't exist.
    – Sean Lange
    Commented Jul 28, 2014 at 15:26
  • Oddly, this error also happens if you have syntax issues like a double-quote at the end of your SQL statement (could be a copy paste mistake)! Commented Mar 13, 2020 at 11:16

4 Answers 4

24

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
...
1
  • I got bit by a different error ... in this case there was a monster subquery in an outer SELECT that threw the same error because I was replacing a string alias with AS syntax. I didn't put the alias in the right place. :) So I spent about 20 min on Google looking for a solution to my own typo ... this did not work well of course. Where there are nested subqueries that need to be re-aliased to AS syntax so they'll upload on Azure ... beware the closing ')' some 3,000 lines after the opening (SELECT ... Commented May 9, 2016 at 19:07
11

I had this error for this query

SELECT 
CASE WHEN COALESCE([dbo].[my-table].[field],"") = '...' THEN 'A' 
     WHEN COALESCE([dbo].[my-table].[field],"") = '...' THEN 'B' 
     ...
     END AS aaa  INTO ##TEMPTABLE
FROM [dbo].[my-table]

Turns out I had to change the "" inside the COALSCE into ''.

Solved it for me

0

I just came across this, and the core reason was actually related to an errant set of brackets in the code which made the engine think there was a missing alias. Something along the lines of:

select *
from SOME_TABLE
where x = 1
[]

A stringified version of the query included a parameter list for logging, but that was being issued as the query instead of the actual query object. Deleting [] at the end resolved it.

0

Showed up here with the same error message, but a different root cause. Mine came from accidentally creating a database that was named "[]". This caused the Use [] statement to throw the above error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.