Re: What are these queries used for?



On Wed, 17 Oct 2007 06:44:57 -0700, Mukesh_Singh_Nick@xxxxxxxxx wrote:

I've come accross queries like this one previously but never got the
time to carefully study them form some book.

SELECT 1 WHERE 1 = 1


1) What do queries like these mean? From common sense, I can deduce
that it is some kind of a test for a boolean value, but the result is
already deterministic in the above case (true).

What use is such a query for?

Hi Mukesh_Singh_Nick,

A query exactly like the one above is rather pointless.

A query without FROM clause is sometimes used to return a single row,
based on values that are not from a table - for instance calculations,
variables, etc.

WHERE 1 = 1 is redundant - it means select rows from the source (in this
case the single row consisting of the constant value 1) only if 1 is
equal to 1 - which is of course always true. The only "good" reason for
using WHERE 1 = 1 is if queries are generated dynamically - if you start
with 1 = 1, you can use AND in front of all other tests; without it, you
have to choose WHERE for the first and AND for the rest. Note, though,
that dynamically generating SQL is not something a beginning SQL coder
should ever do - there are way too many risks involved!


2) What dialect of SQL (ANSI/T-SQL) does MS-SQL Server use?

Transact-SQL, often shortened to T-SQL. Many language elements from
T-SQL are also defined in ANSI, but there are some features that T-SQL
has added in addition to the ANSI standard, and there are also some
features that are defined in the standard but not (yet???) implemented
in T-SQL.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
.



Relevant Pages

  • Re: Update or Delete querys with Joins
    ... This is were I was missing the boat Steve. ... update multiple columns in the ANSI with the same right-side of query. ... that time as Microsoft deems it suitable for inclusion into T-SQL. ... > but SQL Server doesn't implement it. ...
    (microsoft.public.sqlserver.programming)
  • Re: Subquery Help?
    ... Hve not used T-SQL in a while. ... may be in SQL Server 2005. ... DateSerial function to create a date based on the YearField, MonthField, ... The second query would have to be written in the SQL view, ...
    (microsoft.public.access.queries)
  • Re: Slow Query
    ... It looks like you're using SQL Server so, you'd use T-SQL syntax & language to create the query. ... In the ODBC Connect Str property put the correct ODBC connection ...
    (microsoft.public.access.queries)
  • Re: DTS Global Variables + SQL query
    ... they query I'm trying to run isn't as straight forward as I ... > would do in T-SQL. ... > Global Variables and SQL statements in DTS ... > Darren Green (SQL Server MVP) ...
    (microsoft.public.sqlserver.dts)
  • Re: Query working finse on Sql Server 6.5 and not working on Sql Server 2000
    ... ANSI standard syntax... ... from titles right outer join publishers on titles.pub_id = ... I support the Professional Association of SQL Server and it's community of SQL Server professionals. ... > I have a problem with the following query. ...
    (microsoft.public.sqlserver.programming)