concatenate where clause

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

    concatenate where clause

    I am beggining to learn stored procedures and I am trying to
    concatenate the where clause below but I keep getting an error:

    declare @sqlwhere varchar(15)

    set @sqlwhere = 'parentid=2'

    select * from categories where @sqlwhere

    This is the error I am getting

    Server: Msg 170, Level 15, State 1, Line 3
    Line 3: Incorrect syntax near '@sqlwhere'.

    What am I doing wrong?

    Thanks

    Rod

  • Simon Hayes

    #2
    Re: concatenate where clause


    "Rodusa" <rclwebdesign@y ahoo.com> wrote in message
    news:1114622611 .063773.17130@z 14g2000cwz.goog legroups.com...[color=blue]
    >I am beggining to learn stored procedures and I am trying to
    > concatenate the where clause below but I keep getting an error:
    >
    > declare @sqlwhere varchar(15)
    >
    > set @sqlwhere = 'parentid=2'
    >
    > select * from categories where @sqlwhere
    >
    > This is the error I am getting
    >
    > Server: Msg 170, Level 15, State 1, Line 3
    > Line 3: Incorrect syntax near '@sqlwhere'.
    >
    > What am I doing wrong?
    >
    > Thanks
    >
    > Rod
    >[/color]

    You can't use a variable in place of arbitrary parts of a query - you would
    need to use dynamic SQL instead. However, that's often a bad idea and there
    are lots of reasons why you shouldn't; check out this article for examples
    and more details of the issues with using dynamic SQL:



    Simon


    Comment

    • --CELKO--

      #3
      Re: concatenate where clause

      SQL is a compiled language. not some version of BASIC.

      SELECT * FROM Categories WHERE parent_id = 2;

      Comment

      • Rodusa

        #4
        Re: concatenate where clause

        Thanks guys. I appreciate your help. Let me explain what I want to do
        and maybe there is a different solution for what I am trying to do.

        I am building a stored procedure which contains a lot of repetitive sql
        statements specially in the where clause. I was trying to clean it a
        little bit by storing the recurring code so that it would be easier to
        read. In C# or VB.net this is easy to do like I showed you. But it
        seems that I will have to live with it unless I use dynamic SQL.

        Rod

        Simon Hayes wrote:[color=blue]
        > "Rodusa" <rclwebdesign@y ahoo.com> wrote in message
        > news:1114622611 .063773.17130@z 14g2000cwz.goog legroups.com...[color=green]
        > >I am beggining to learn stored procedures and I am trying to
        > > concatenate the where clause below but I keep getting an error:
        > >
        > > declare @sqlwhere varchar(15)
        > >
        > > set @sqlwhere = 'parentid=2'
        > >
        > > select * from categories where @sqlwhere
        > >
        > > This is the error I am getting
        > >
        > > Server: Msg 170, Level 15, State 1, Line 3
        > > Line 3: Incorrect syntax near '@sqlwhere'.
        > >
        > > What am I doing wrong?
        > >
        > > Thanks
        > >
        > > Rod
        > >[/color]
        >
        > You can't use a variable in place of arbitrary parts of a query - you[/color]
        would[color=blue]
        > need to use dynamic SQL instead. However, that's often a bad idea and[/color]
        there[color=blue]
        > are lots of reasons why you shouldn't; check out this article for[/color]
        examples[color=blue]
        > and more details of the issues with using dynamic SQL:
        >
        > http://www.sommarskog.se/dynamic_sql.html
        >
        > Simon[/color]

        Comment

        • Simon Hayes

          #5
          Re: concatenate where clause

          In that case, you might be able to use views and/or functions to
          simplify your queries - you can create views for your most common joins
          and queries, then select from the views in your proc code. That would
          help you simplify your code while avoiding the dynamic SQL solution.

          Simon

          Comment

          • Rodusa

            #6
            Re: concatenate where clause

            Simon, first of all thanks for you prompt answers. I liked the function
            idea is nice.

            Rod

            Simon Hayes wrote:[color=blue]
            > In that case, you might be able to use views and/or functions to
            > simplify your queries - you can create views for your most common[/color]
            joins[color=blue]
            > and queries, then select from the views in your proc code. That would
            > help you simplify your code while avoiding the dynamic SQL solution.
            >
            > Simon[/color]

            Comment

            • --CELKO--

              #7
              Re: concatenate where clause

              >> I am building a stored procedure which contains a lot of repetitive
              sql statements specially in the where clause.<<

              Statements change things in the schema (UPDATE, INSERT, DELETE) so you
              cannot put them int he WHERE clause; did you mean search conditions?

              Build VIEWs and not functions or dynamic SQL for the repetitive search
              conditions. SQL is a declarative langauge, not a procedural language.
              It is also a compiled language. so dynamic SQL is a kludge.

              Also, if this is one procedure as you said, post the code. Assuming
              that it has the proper cohesion and coupling, there is a good chance
              that we can improve it and remove the repetition. Since we got the
              CASE expression, a lot of things that programmers used to write with
              procedural or dynamic code can be done with compiled code.

              Comment

              Working...