[dba-SQLServer] Strange syntax

David McAfee davidmcafee at gmail.com
Tue Sep 13 14:57:09 CDT 2011


It looks like C or Very Early SQL, which is similar to C (used !=, ==, ++
and so on)


The Declaration of @b and the print of the cast do nothing
A is always going to be 0.
I modified it to use a variable, just so it was easier to change the number.
B is always going to be the test number
C will always be 1 more
D will always be 1 less
E&F will always be 1 more

DECLARE @N AS INT SET @N = 4

SELECT
0              AS A,
@N          AS B,
@N ++  1 AS C, --Think of it as: @N + (+1)
@N +-+ 1 AS D, --Think of it as: @N + (-(+1)) or @N + -1
@N -+-  1 AS E, --Think of it as: @N - (+(-1) ) or @N - -1
@N - -   1 AS F  --Think of it as: @N -(-1) same as E

--Results:
--A           B           C           D           E           F
----------- ----------- ----------- ----------- ----------- -----------
--0           0           1           -1          1           1
--0           1           2           0           2           2
--0           2           3           1           3           3
--0           3           4           2           4           4
--0           4           5           3           5           5


On Tue, Sep 13, 2011 at 4:20 AM, Arthur Fuller <fuller.artful at gmail.com>wrote:

> I just came across some T-SQL syntax that I've never seen before, and I
> have
> no idea what it means or how it works.
>
> <sql>
> DECLARE @b INT
> SELECT 0 [A]
> , B = 1 -- 1
> , 1 ++ 1 AS [C]
> , 1 +-+ 1 AS [D]
> , 1 -+- 1 AS [E]
> , 1 - - 1 AS [F]
>
> PRINT 'Result: ' + CAST(@b AS VARCHAR(10))
> </sql>
>
> Results:
>
> A           B           C           D           E           F
> ----------- ----------- ----------- ----------- ----------- -----------
> 0           1           2           0           2           2
>
> (1 row(s) affected)
>
> Can anyone explain this? I can't think of a use for it offhand, but I'm
> certainly curious as to how it works.
>
>



More information about the dba-SQLServer mailing list