How to select result with empty set - sql

I have sql query:
select * from table where id in (1,2)
1,2 are parameters which I add there dynamically. But what If I have empty set:
select * from table where id in ()
then this query call exception:
ERROR at line 1:
ORA-00936: missing expression
how should I create sql with empty set

You can always add null to your set, so when the real set is empty, you get no syntax error:
select * from table where id in (null,1,2)
vs
select * from table where id in (null)
Your generator would be simpler, too, because you can print a , in front of every item that you add, without checking if it's the first one or not.
Of course since you are generating your SQL, the standard precautions against SQL Injection apply: do not let user input anywhere near the SQL that you generate.

Try this
select * from table where id in (null)

Related

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

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

How to assign returned result to variable when using "Except" in ms msql?

I have 2 temporary table that I am comparing. I would like to have a variable to hold either the count of differences between these 2 tables or just varchar of differences that comes from except.
I have below query so far
Declare #comparison AS NVARCHAR(2000);
SET #comparison=(select * from #dboPerson1
except
select * from #dboPerson2);
Select #comparison
I get following error:
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
I tried adding count(*) before select and the comparison variable becomes the number of records that are different between two temporary tables.
But it was printing select * except result and as well as count.
I would like to assign the differences two variable without showing it on console unless I chose to select the variable name.
Hope it makes sense. I am using sql server 2012 btw.
Thanks.
I would like to have a variable to hold either the count of differences [...]
DECLARE #comparison INT;
SET #comparison=
(
SELECT COUNT(*)
FROM (select * from #dboPerson1 except select * from #dboPerson2) x
);

Ambiguous Column Error In SQL Select Statement

SELECT IB,*
FROM SaleOrder
WHERE IB IS NOT NULL
ORDER BY IB
Error :
Msg 209, Level 16, State 1, Line 1
Ambiguous column name 'IB'.
Can somebody please explain why am I getting error while executing above SQL statement in SQL Server 2012 whereas same runs fine in SQL Server 2008?
My guess is your SQL Server 2008 database is in SQL Server 2000 compatibility mode, because normally it should return the same error as your 2012 instance.
Try fully qualifying the table name in your query and running it in SQL Server 2008 in the context of a database with the default compatibility level (e.g. in the context of tempdb), and you will likely see the error.
The difference in behaviour is by design and is documented in this Technet article as follows (emphasis added):
Compatibility-level setting of 80
…
When binding the column references in the ORDER BY list to the columns defined in the SELECT list, column ambiguities are ignored and column prefixes are sometimes ignored. This can cause the result set to return in an unexpected order.
Your table already contains column IBID so in ORDER BY clause sql server is not able to decide which column to access and sort by , IBID from table obtained in * or IBIS column specified at beginning in SELECT list
Select IBID,s.* from SaleHeader s WHERE IBID IS NOT NULL Order BY s.IBID
specify an alias to table as shown above
Select * from SaleHeader WHERE IBID IS NOT NULL Order BY IBID
That is because you are selecting IBID column twice.
Try:
Select * from SaleHeader WHERE IBID IS NOT NULL Order BY IBID
That is because the column IBID occurs twice in your results now.
You should either add an alias or remove the column.
So either this:
Select * from SaleHeader WHERE IBID IS NOT NULL Order BY IBID
Or:
Select s.IBID colX,* from SaleHeader s WHERE s.IBID IS NOT NULL Order BY s.IBID
About the 'why':
Is the query exact the same? Maybe the interpreter is different on both platforms. Is there a difference in your use of this query? (for example, do you run this separately or as a view definition. that differs a lot in how it is analyzed)
your table have already that column. if you use 2 times column in one query than sql server need to specify which table column want to access. use tablename.column name in order by.
SELECT IBID,*
FROM SaleHeader
WHERE IBID IS NOT NULL
ORDER BY SaleHeader.IBID

Mysterious variable in sql query

I'm looking at some sql code with the following structure:
set #var =
(
select count(1) from
(
select * from table where field = 1
)
someVariable
)
It won't seem to run unless "someVariable" is in the statement. My question is, what does this "someVariable" represent, and why is it in the query? I don't understand why I can't set #var to the select count statement outright, so the "someVariable" is really throwing me off.
Derived tables need to have aliases. someVariable is functioning as an alias in this case.
When I run a similar query on MySQL, I got:
ERROR 1248 (42000): Every derived table must have its own alias
The inner select actually gives rise to a derived table, and someVariable is it's alias.