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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Making a Specific Stored Procedure For Search Keyword

Making a Specific Stored Procedure For Search Keyword

Scheduled Pinned Locked Moved Database
databasehelpsharepointquestion
4 Posts 4 Posters 1 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.
  • I Offline
    I Offline
    ivadak
    wrote on last edited by
    #1

    Hi ! I want to create a stored procedure for Search keyword with UNION, Sub query. My DataBase includes same types of Board Tables. (Such as board_a , board_b ...) I tried to make as below.

    CREATE PROCEDURE sp_Search
    @page int = 1,
    @pageSize int = 10,
    @keyword varchar(50)

    AS

    Declare @Sql varchar(3000)
    Set @Sql = "Select Top " + Str(@pageSize) + " no, subject, content from  "
    Set @Sql = @Sql + " ( "
    Set @Sql = @Sql + "			Select tb, no, subject, content from ( "
    Set @Sql = @Sql + "				Select 'board\_a' as tb, no,subject,content from board\_a "
    Set @Sql = @Sql + "				UNION "
    Set @Sql = @Sql + "				select 'board\_b' as tb, no,subject,content from board\_b "
    Set @Sql = @Sql + "			) As A "
    Set @Sql = @Sql + " )   where no NOT IN "
    Set @Sql = @Sql + " ( "
    Set @Sql = @Sql + "		Select Top " + Str(@pageSize \* (@page-1)) + " no from ( "
    Set @Sql = @Sql + "			Select tb, no, subject, content from ( "
    Set @Sql = @Sql + "				Select 'board\_a' as tb, no,subject,content from board\_a "
    Set @Sql = @Sql + "				UNION "
    Set @Sql = @Sql + "				select 'board\_b' as tb, no,subject,content from board\_b "
    Set @Sql = @Sql + "			) As A "
    Set @Sql = @Sql + "     )  "
    Set @Sql = @Sql + " ) where subject Like '%"+ @keyword +"%' or content Like '%"+ @keyword +"%' order by no ASC " 
    Execute(@Sql)
    

    But I got a Error Message. I am having extreme difficulty in working time. Please can someone help me? ;)

    H A A 3 Replies Last reply
    0
    • I ivadak

      Hi ! I want to create a stored procedure for Search keyword with UNION, Sub query. My DataBase includes same types of Board Tables. (Such as board_a , board_b ...) I tried to make as below.

      CREATE PROCEDURE sp_Search
      @page int = 1,
      @pageSize int = 10,
      @keyword varchar(50)

      AS

      Declare @Sql varchar(3000)
      Set @Sql = "Select Top " + Str(@pageSize) + " no, subject, content from  "
      Set @Sql = @Sql + " ( "
      Set @Sql = @Sql + "			Select tb, no, subject, content from ( "
      Set @Sql = @Sql + "				Select 'board\_a' as tb, no,subject,content from board\_a "
      Set @Sql = @Sql + "				UNION "
      Set @Sql = @Sql + "				select 'board\_b' as tb, no,subject,content from board\_b "
      Set @Sql = @Sql + "			) As A "
      Set @Sql = @Sql + " )   where no NOT IN "
      Set @Sql = @Sql + " ( "
      Set @Sql = @Sql + "		Select Top " + Str(@pageSize \* (@page-1)) + " no from ( "
      Set @Sql = @Sql + "			Select tb, no, subject, content from ( "
      Set @Sql = @Sql + "				Select 'board\_a' as tb, no,subject,content from board\_a "
      Set @Sql = @Sql + "				UNION "
      Set @Sql = @Sql + "				select 'board\_b' as tb, no,subject,content from board\_b "
      Set @Sql = @Sql + "			) As A "
      Set @Sql = @Sql + "     )  "
      Set @Sql = @Sql + " ) where subject Like '%"+ @keyword +"%' or content Like '%"+ @keyword +"%' order by no ASC " 
      Execute(@Sql)
      

      But I got a Error Message. I am having extreme difficulty in working time. Please can someone help me? ;)

      H Offline
      H Offline
      Henry Minute
      wrote on last edited by
      #2

      I do not have the skills to help you, but it might help others to do so if you edit your post to include the full error message, rather than just say that you get one. Cut and paste if you can, to avoid typos. :)

      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

      1 Reply Last reply
      0
      • I ivadak

        Hi ! I want to create a stored procedure for Search keyword with UNION, Sub query. My DataBase includes same types of Board Tables. (Such as board_a , board_b ...) I tried to make as below.

        CREATE PROCEDURE sp_Search
        @page int = 1,
        @pageSize int = 10,
        @keyword varchar(50)

        AS

        Declare @Sql varchar(3000)
        Set @Sql = "Select Top " + Str(@pageSize) + " no, subject, content from  "
        Set @Sql = @Sql + " ( "
        Set @Sql = @Sql + "			Select tb, no, subject, content from ( "
        Set @Sql = @Sql + "				Select 'board\_a' as tb, no,subject,content from board\_a "
        Set @Sql = @Sql + "				UNION "
        Set @Sql = @Sql + "				select 'board\_b' as tb, no,subject,content from board\_b "
        Set @Sql = @Sql + "			) As A "
        Set @Sql = @Sql + " )   where no NOT IN "
        Set @Sql = @Sql + " ( "
        Set @Sql = @Sql + "		Select Top " + Str(@pageSize \* (@page-1)) + " no from ( "
        Set @Sql = @Sql + "			Select tb, no, subject, content from ( "
        Set @Sql = @Sql + "				Select 'board\_a' as tb, no,subject,content from board\_a "
        Set @Sql = @Sql + "				UNION "
        Set @Sql = @Sql + "				select 'board\_b' as tb, no,subject,content from board\_b "
        Set @Sql = @Sql + "			) As A "
        Set @Sql = @Sql + "     )  "
        Set @Sql = @Sql + " ) where subject Like '%"+ @keyword +"%' or content Like '%"+ @keyword +"%' order by no ASC " 
        Execute(@Sql)
        

        But I got a Error Message. I am having extreme difficulty in working time. Please can someone help me? ;)

        A Offline
        A Offline
        Abhishek Sur
        wrote on last edited by
        #3

        what is the error message ?? :confused:

        Abhishek Sur


        My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

        **Don't forget to click "Good Answer" if you like to.

        1 Reply Last reply
        0
        • I ivadak

          Hi ! I want to create a stored procedure for Search keyword with UNION, Sub query. My DataBase includes same types of Board Tables. (Such as board_a , board_b ...) I tried to make as below.

          CREATE PROCEDURE sp_Search
          @page int = 1,
          @pageSize int = 10,
          @keyword varchar(50)

          AS

          Declare @Sql varchar(3000)
          Set @Sql = "Select Top " + Str(@pageSize) + " no, subject, content from  "
          Set @Sql = @Sql + " ( "
          Set @Sql = @Sql + "			Select tb, no, subject, content from ( "
          Set @Sql = @Sql + "				Select 'board\_a' as tb, no,subject,content from board\_a "
          Set @Sql = @Sql + "				UNION "
          Set @Sql = @Sql + "				select 'board\_b' as tb, no,subject,content from board\_b "
          Set @Sql = @Sql + "			) As A "
          Set @Sql = @Sql + " )   where no NOT IN "
          Set @Sql = @Sql + " ( "
          Set @Sql = @Sql + "		Select Top " + Str(@pageSize \* (@page-1)) + " no from ( "
          Set @Sql = @Sql + "			Select tb, no, subject, content from ( "
          Set @Sql = @Sql + "				Select 'board\_a' as tb, no,subject,content from board\_a "
          Set @Sql = @Sql + "				UNION "
          Set @Sql = @Sql + "				select 'board\_b' as tb, no,subject,content from board\_b "
          Set @Sql = @Sql + "			) As A "
          Set @Sql = @Sql + "     )  "
          Set @Sql = @Sql + " ) where subject Like '%"+ @keyword +"%' or content Like '%"+ @keyword +"%' order by no ASC " 
          Execute(@Sql)
          

          But I got a Error Message. I am having extreme difficulty in working time. Please can someone help me? ;)

          A Offline
          A Offline
          Ashfield
          wrote on last edited by
          #4

          Try printing your sql (replace Execute(@Sql) with print @SQL), then paste it into query analyser and try running it from there to get more information on the error.

          Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

          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