Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Building an SQL Statement in a stored procedure

Building an SQL Statement in a stored procedure

Scheduled Pinned Locked Moved Database
databasehelp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paul McGann
    wrote on last edited by
    #1

    I have the following code SET @sql = 'SELECT * FROM tbl_Employee WHERE tbl_Employee.Department_ID = ' + @Department_ID + AND tbl_Employee.Surname LIKE %' + @Surname + '%' If i remove the Like part it works fine but with it added it's casuing some problems. I think I'm concatenating the sql wrong. If someone can help it would be much appreciated.

    ASP all the way

    L M 3 Replies Last reply
    0
    • P Paul McGann

      I have the following code SET @sql = 'SELECT * FROM tbl_Employee WHERE tbl_Employee.Department_ID = ' + @Department_ID + AND tbl_Employee.Surname LIKE %' + @Surname + '%' If i remove the Like part it works fine but with it added it's casuing some problems. I think I'm concatenating the sql wrong. If someone can help it would be much appreciated.

      ASP all the way

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Do this and check your SQL:

      print @sql

      1 Reply Last reply
      0
      • P Paul McGann

        I have the following code SET @sql = 'SELECT * FROM tbl_Employee WHERE tbl_Employee.Department_ID = ' + @Department_ID + AND tbl_Employee.Surname LIKE %' + @Surname + '%' If i remove the Like part it works fine but with it added it's casuing some problems. I think I'm concatenating the sql wrong. If someone can help it would be much appreciated.

        ASP all the way

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        The single quote before the AND keyword is missing, it should be:

        SET @sql = 'SELECT * FROM tbl_Employee
        WHERE tbl_Employee.Department_ID = ' + @Department_ID + ' AND tbl_Employee.Surname LIKE %' + @Surname + '%'

        1 Reply Last reply
        0
        • P Paul McGann

          I have the following code SET @sql = 'SELECT * FROM tbl_Employee WHERE tbl_Employee.Department_ID = ' + @Department_ID + AND tbl_Employee.Surname LIKE %' + @Surname + '%' If i remove the Like part it works fine but with it added it's casuing some problems. I think I'm concatenating the sql wrong. If someone can help it would be much appreciated.

          ASP all the way

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          I presume there is a good reason why you are using dynamic SQL, the select statement does not require it! This will work without dynamic SQL

          Set @Surname = @Surname + '%'

          SELECT *
          FROM tbl_Employee
          WHERE tbl_Employee.Department_ID = @Department_ID
          AND tbl_Employee.Surname LIKE @Surname

          As Shameel suggested print the @SQL, copy the result back and try to run it, then fix it so it runs and make the changes to your dynamic sql construct.

          P 1 Reply Last reply
          0
          • M Mycroft Holmes

            I presume there is a good reason why you are using dynamic SQL, the select statement does not require it! This will work without dynamic SQL

            Set @Surname = @Surname + '%'

            SELECT *
            FROM tbl_Employee
            WHERE tbl_Employee.Department_ID = @Department_ID
            AND tbl_Employee.Surname LIKE @Surname

            As Shameel suggested print the @SQL, copy the result back and try to run it, then fix it so it runs and make the changes to your dynamic sql construct.

            P Offline
            P Offline
            Paul McGann
            wrote on last edited by
            #5

            Thanks guys. The reason for dynamic sql is because this is part of a much larger section of code for custom paging. Again thanks

            ASP all the way

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups