'WHERE 1 =1' in SQL Stored Procedure [duplicate] - sql

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Why would someone use WHERE 1=1 AND <conditions> in a SQL clause?
I've been tasked with reviewing some SQL stored procedures and have seen many that look like the following:
SELECT
X, Y, Z
FROM
Table
WHERE
1 = 1
ORDER BY
X
Why would someone use '1 = 1' for the where clause?
Thanks!

It's common in dynamic SQL, in order to append additional criteria to a WHERE clause. Otherwise, it's useless and it is ignored by the optimizer.

possibly to dynamically add conditions to the where clause.

Related

Access SQL to Firebird SQL [duplicate]

This question already has answers here:
How to select data from non-table in Firebird?
(1 answer)
Use Alias in Select Query
(3 answers)
Use column alias in same select [duplicate]
(2 answers)
Closed 11 months ago.
I have in my Access .mdb
SELECT a * b AS c, c * d AS e
I must translate this clause to Firebird.
Any hint?
Using SELECT without a FROM clause is non-standard in the SQL language. A few implementations allow it (e.g. Microsoft Access, SQL Server, and MySQL), but others do not.
Firebird is one of those that implement standard SQL, so you must provide a FROM clause, and reference a table with at least one row, or else you get no result.
http://www.firebirdfaq.org/faq30/ says:
You can use the RDB$DATABASE which is a single-row table, and part of each database's metadata:
select current_timestamp from RDB$DATABASE;
Any other table with at least one row could be used, but the point is that RDB$DATABASE is sure to be present.

SQL Select AS not being recognized as column [duplicate]

This question already has answers here:
Referring to a Column Alias in a WHERE Clause
(9 answers)
Closed 1 year ago.
I am using a simple select statement and creating a column using a CONCAT function and labeling the column as Filter.
Why is the new column not being recognized?
Error message states
Invalid Column Name - Filter
You can't refer to column aliases in the where clause as the where is processed before the alias is materialised.
There are several workarounds, and assuming SQL Server you can do
select *
from table t
cross apply (values(concat(columna,columnb)))c([filter])
where [filter]='something'
Also note that the performance won't be great on large data sets since this criteria is non-sargable

How to use NOT IN sql query in django [duplicate]

This question already has answers here:
Django equivalent of SQL not in
(4 answers)
Closed 3 years ago.
Im new to Django. Anyone help me to write "Not In" sql query in django.
here i used raw() query set without raw() query set how to write this query in django.
query = 'SELECT basic_uom FROM uom_master WHERE id="'+ id +'" and basic_uom not in (SELECT next_uom from uom_master WHERE id="'+ id +'") and basic_uom not in(SELECT std_uom FROM std_uom_master WHERE id"'+ id +'")ORDER BY next_uom ASC'
data = uom_master.objects.raw(query)
If [1,2,3] is your list then you can do something like this, you modify according to your table names
uom_master.objects.filter(id=some_id).exclude(id__in=[1,2,3]).order_by("next_uom")
To get list of ids you can do like
list_of_ids = std_uom_master.objects.filter().values_list("id",flat=True)
filter is your filtering criteria and exclude is records to be omitted.
See filters like id__in and guide about making queries

List aggregation in Oracle 10G [duplicate]

This question already has answers here:
Concatenate results from a SQL query in Oracle
(7 answers)
Closed 6 years ago.
Lets say, I have following table(i.e: Reference Table). I want to display my results as 'Expected Table'. How may I get this result? Any help will be highly appreciated. I am using Oracle 10g.
Expected:
SELECT Collateral_Id,
LISTAGG(Commitment_Id, ',')
WITHIN GROUP (ORDER BY Commitment_Id) "Commitment_Id"
FROM yourTable
GROUP BY Collateral_Id

SQL ORDER BY (sequence) [duplicate]

This question already has answers here:
sql ORDER BY multiple values in specific order?
(12 answers)
Closed 9 years ago.
I have a sql statement which I would want to ORDER BY a specific sequence.
SELECT * FROM UserDB ORDER BY [Role]
How can I make it such that the data brought to my GridView table is listed from Admin on the top, follow by User and Guests?
So you want to order by Admin/User/Guest?
Try something like :
SELECT *
FROM UserDB
ORDER BY
CASE Role WHEN 'Admin' THEN 0
WHEN 'User' THEN 1
WHEN 'Guest' THEN 2
END
Does that work for you?
Another option would be to have (or add) a column Sequence to your Role table so you could define the sequence in the table itself - and then just to an ORDER BY Role.Sequence.
This is substantively identical to the question " sql ORDER BY multiple values in specific order? " and I strongly recommend you look at the solutions presented there.