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

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.

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
...

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

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.

Related

Case expression to check if column exists throws invalid column name error

I'm building a Stored Procedure meant to be run daily. Due to how the data is provided me, some days some of the columns necessary for the output are not in the file that's imported to a temporary table.
For the output, the value can be null/empty, but the actual column has to be there.
I've tried the following code:
select
case
when exists(
select * from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = 'tableName' and COLUMN_NAME = 'ABC'
) then ABC
else ''
end
as 'XYZ' from tableName
I know for a fact that in the tests I'm running the column DOES NOT exist, so I'm expecting the SELECT statment to simply return an empty string for column XYZ.
However when running such SELECT statement, I get the following error:
Invalid column name 'ABC'.
Since I know from the start that the column ABC does not exist, I was expecting that the EXISTS(...) would evaluate to FALSE and jump straight to the ELSE statement. However it seems that the column name is still evaluated.
How can I get around this?
I believe i have solved my troubles thanks to the workaround provided by #MartinSmith
(https://dba.stackexchange.com/questions/66741/why-cant-i-use-a-case-statement-to-see-if-a-column-exists-and-not-select-from-i/66755#66755)
I ended up using an alternative version of the workaround provided, linked by the poster of #MartinSmith's solution, but the results seem to be what i was expecting.
Regarding the Only one expression can be specified in the select list when the subquery is not introduced with EXISTS error, it was being caused by me trying to SELECT two columns in the subquery. Solution came from here: https://stackoverflow.com/a/7684626/18191554
Now, my code is as follows:
select
.
.
.
.
(select (SELECT [ABC]
from dbo.tableName
where ID = temp.ID])
from (select '' as [ABC]) as dummy)
as 'XYZ',
.
.
.
from tableName
where ABC is the column that may or may not exist.
Thanks to everyone involved

ERROR: syntax error at or near "." LINE 4: ON like.takerId = frame.likeId;

i have a table whose name is like. But whenever i have to select data from like, i was getting this error, i figured it out public.like..but when i try to join two tables
SELECT *
FROM frame
INNER JOIN public.like
ON like.takerId = frame.likeId;
i get this error
ERROR: syntax error at or near "."
LINE 4: ON like.takerId = frame.likeId;
i also use public prefix but it throws
ERROR: column like.takerid does not exist
LINE 4: ON public.like.takerId = frame.likeId;
^
HINT: Perhaps you meant to reference the column "like.takerId".
even if it is saying column like.takerid does not exist , then why it gives me HINT: Perhaps you meant to reference the column "like.takerId". I dont know, i think it is problem with like table name, like is a sql syntax, and it assumes like and a sql syntax and throwing me error. Should I change my table name? Or is there any way to make sql case sensetive or how can i tell sql to ignore like. public.like is not working for joining table.
As like is a reserved keyword, you need to use double quotes for each occurance of it (unless it's prefixed with the schema name as you found out)
SELECT *
FROM frame
JOIN public.like ON "like".takerId = frame.likeId;
Or
SELECT *
FROM frame
JOIN "like" ON "like".takerId = frame.likeId;
Or use an alias
SELECT *
FROM frame f
JOIN "like" l ON l.takerId = f.likeId;
But in the long run you should find a different name for the table that does not require quoting.
You should definitely chose another name for your table. LIKE is a reserved command, and it is considered a bad practice to use it, although possible by using ", e.g.
CREATE TABLE public."like" (id int);
INSERT INTO public."like" VALUES (42);
SELECT * FROM "public.like"
EDIT: As pointed out by #a_horse_with_no_name, specifying a schema in temporary tables won't work (check db<>fiddle), so only the table name should be between double quotes as corrected in the snippet above. For temporary tables just omit the schema:
CREATE TEMPORARY TABLE "like" (id int);
INSERT INTO "like" VALUES (42);
SELECT * FROM "like"
Demo: db<>fiddle

Placing a predefined value in the empty fields of an SQL query result

I need to manipulate the data that a certain SQL query outputs as a result, only by modifying the original query. Since it is s Select-Where-From query, as a novice in SQL I assume I can simply nest it inside another query of this type, resulting in a structure similar to: Select-Where-(Select-Where-From).
The data manipulation simply requires the replacement of all empty fields in a certain column (that was taken from the result of the original query) with a specific predefined value. Here are the two attempts I've made - based on findings from this website - which failed:
select NAME_OF_COLUMN, COALESCE(NULLIF(NAME_OF_COLUMN,''), 'Value_to_insert')
from
(THIS IS WHERE THE ORIGINAL SELECT QUERY GOES)
This one doesn't throw an error, but nonetheless produces empty fields instead of populating them with the value above, as if only the original query was run.
The 2nd:
Select *, NAME_OF_COLUMN=
CASE WHEN NAME_OF_COLUMN IS NULL THEN 'Value_to_insert'
WHEN NAME_OF_COLUMN='' THEN 'Value_to_insert'
ELSE NAME_OF_COLUMN
END
from
(THIS IS WHERE THE ORIGINAL SELECT QUERY GOES)
This one throws the following error (forgive me for the messy presentation, but it was not up to me):
java.sql.SQLSyntaxErrorException: ORA-00923: FROM keyword not found where expected
, org.eclipse.birt.report.engine.api.EngineException: Invalid bound column name: CREATOR_USER_NAME., org.eclipse.birt.report.engine.api.EngineException: Cannot get the result set metadata.
org.eclipse.birt.report.data.oda.jdbc.JDBCException: SQL statement does not return a ResultSet object.
SQL error #1:ORA-00923: FROM keyword not found where expected;
Can you please assist me and tell me what am I doing wrong? Perhaps I need to select a specific column and/or use the 'as' command?
Edit: I have attempted replacing the original select which was:
select table.column as NAME_OF_COLUMN
with this:
select nvl(table.column, 'Value_to_insert') as NAME_OF_COLUMN
Unfortunately, just like the first attempt, the output is identical to the output of the original query..
NAME_OF_COLUMN=CASE ... END is invalid in Oracle. You can't assign a column value in (standard) SQL like that.
If you are trying to come up with a column alias: that needs to go after the expression:
CASE
WHEN name_of_column IS NULL THEN 'Value_to_insert'
WHEN name_of_column = '' THEN 'Value_to_insert'
ELSE name_of_column
END as name_of_column
In Oracle an empty String '' is converted to NULL when you store the value. So the second condition of your CASE expression will never be true. The whole thing can be simplified to:
coalesce(name_of_column, 'Value_to_insert') as name_of_column
Note that you need to get rid of the select * part and explicitly list all other columns excluding name_of_column there, otherwise your query ends up with two columns with the same name.

Error show when update using phpmyadmin

When I update a column using phpmyadmin in database with following query
UPDATE members
SET `refered` = (SELECT COUNT (*)
FROM `user_details`
WHERE `user_details.sponser`=`members.username`
)
It show a error message like this
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '*) FROM `user_details` WHERE `user_details.sponser`=`members.username`)' at line 1
What may be reason?
Error is in
COUNT (*)
-----^
Remove the space between COUNT and (*).
Try the below query
UPDATE members SET refered = (SELECT COUNT(*) FROM user_details
WHERE user_details.sponser=members.username)
Does the Select query returns any result. If so what is the result. Looks like all your query is inside '' single quotes that you are using it should be removed. Single quotes need to be removed for example .
UPDATE members
SET refered = (SELECT COUNT (*)
FROM user_details
WHERE user_details.sponser=members.username
)
-- there is not single quotes in the query above. please remove it from yours.
Part of your problem, or maybe the whole problem, is the WHERE clause. You've used backticks for the table name, which is correct (or, at least, it's optional in this case; it's needed if your database name or table name has a MySQL reserved name or is otherwise ambiguous). The problem, though, is that the dot separating the database from the table needs to be outside the backticks. So your WHERE clause should look like this instead:
WHERE `user_details`.`sponser`=`members`.`username`

Inserting new rows into table-1 based on constraints defined on table-2 and table-3

I want to append new rows to a table-1 d:\dl based on the equality constraint lower(rdl.subdir) = lower(tr.n1), where rdl and tr would be prospective aliases for f:\rdl and f:\tr tables respectively.
I get a function name is missing ). message when running the following command in VFP9:
INSERT INTO d:\dl SELECT * FROM f:\rdl WHERE (select LOWER(subdir)FROM f:\rdl in (select LOWER(n1) FROM f:\tr))
I am using the in syntax, instead of the alias based equality statement lower(rdl.subdir) = lower(tr.n1) because I do not know where to define aliases within this command.
In general, the best way to get something like this working is to first make the query work and give you the results you want, and then use it in INSERT.
In general, in SQL commands you assign aliases by putting them after the table name, with or without the keyword AS. In this case, you don't need aliases because the ones you want are the same as the table names and that's the default.
If what you're showing is your exact code and you're running it in VFP, the first problem is that you're missing the continuation character between lines.
You're definitely doing too much work, too. Try this:
INSERT INTO d:\dl ;
SELECT * ;
FROM f:\rdl ;
JOIN f:\tr ;
ON LOWER(rdl.subdir) = LOWER(tr.n1)