Query Variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yota

    Query Variables

    Hi,

    I'm new to SQL Server, but an experienced .Net developer. I'm trying
    to accomplish a query the most efficient way possible. My question is
    if you can define a temporary variable within a query to store tables
    or fields. (Like the LET clause of LINQ) My query makes use of
    subqueries which filter my table (WHEREs, not SELECTs) in the same
    exact way. I'd like to have a subquery at the beginning of my query
    to filter the table(s) once, and then SELECT off it of later in the
    query.

    Here is an (utterly poor) example. No, this is not from my project.
    My filter is a little more complex than 'c=@p'.
    ('c' is a column/field, 't' is a table', '@p' is a parameter)

    SELECT *
    FROM (SELECT COUNT(c) FROM t WHERE c=@p GROUP BY c)
    CROSS JOIN (SELECT c FROM t WHERE c=@p)

    Bottom line, would something like the following be possible?

    @v = (SELECT c FROM t WHERE a=@p)
    SELECT *
    FROM (SELECT COUNT(c) FROM @v GROUP BY c)
    CROSS JOIN (SELECT c FROM @v)

    I'd like to know if this is possible within a query, but I can move to
    a Stored Procedure if I must. (I'll still need help then.)

    Thank you all

  • steve

    #2
    Re: Query Variables

    >My question is if you can define a temporary variable within a query to >store tablesor fields. (Like the LET clause of LINQ).

    The answer is of course! Jump in anywhere and you will see what makes
    sense:) You can start here:
    Everything you work with in D4, every object you define, is a variable. And since everything is a variable it must typed. Understanding vari...


    best,.


    Comment

    • Ed Murphy

      #3
      Re: Query Variables

      steve wrote:
      >My question is if you can define a temporary variable within a query to >store tablesor fields. (Like the LET clause of LINQ).
      >
      The answer is of course! Jump in anywhere and you will see what makes
      sense:) You can start here:
      Everything you work with in D4, every object you define, is a variable. And since everything is a variable it must typed. Understanding vari...

      >
      best,.
      www.beyondsql.blogspot.com
      Posting only to discuss one's own product is one of the ten early
      warning signs of crankery.

      Comment

      • steve

        #4
        Re: Query Variables

        On Oct 17, 11:51 am, Ed Murphy <emurph...@soca l.rr.comwrote:
        steve wrote:
        My question is if you can define a temporary variable within a query to >store tablesor fields. (Like the LET clause of LINQ).
        >
        The answer is of course! Jump in anywhere and you will see what makes
        sense:) You can start here:
        http://beyondsql.blogspot.com/2007/0...es-are-typed-v...
        >>
        Posting only to discuss one's own product is one of the ten early
        warning signs of crankery.
        If you had bothered to read the link you'd see it was exactly what the
        op was asking for. I guess you can be an sql cop. I'm not so sure
        about a detective :)

        Comment

        Working...