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. Web Development
  3. ASP.NET
  4. How to use if condition in stored procedures

How to use if condition in stored procedures

Scheduled Pinned Locked Moved ASP.NET
databasetutorialsharepoint
3 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.
  • N Offline
    N Offline
    Nekkantidivya
    wrote on last edited by
    #1

    Hi, I want to develop a stored procedure in which I have to apply different modifications to the Query. Finally after undergoing all the changes to the query I have to execute the final query and get the resultant table.For example: Create Procedure [dbo].[Test] @Flg1 bit, @Flg2 bit, @Flg3 bit, AS BEGIN SET NOCOUNT ON; Declare @Query varchar(max) if(@Flg1<>0) Begin Set @Query='Select * from emplayee where empId=1" End if(@Flg2<>0) Begin Set @Query=@Query+'and empId=2' End if(@Flg3<>0) Begin Set @Query=@Query+'and empId=3' End Exec sp_executesql(@Query); End As shown above my Query conditions will change according to the parameters. But here I am not able to get the resultant table. Please reply me if anyone know how to get the resultant Table. Also please tell me whether the above procedure is correct or not. Thanks in advance.

    D H 2 Replies Last reply
    0
    • N Nekkantidivya

      Hi, I want to develop a stored procedure in which I have to apply different modifications to the Query. Finally after undergoing all the changes to the query I have to execute the final query and get the resultant table.For example: Create Procedure [dbo].[Test] @Flg1 bit, @Flg2 bit, @Flg3 bit, AS BEGIN SET NOCOUNT ON; Declare @Query varchar(max) if(@Flg1<>0) Begin Set @Query='Select * from emplayee where empId=1" End if(@Flg2<>0) Begin Set @Query=@Query+'and empId=2' End if(@Flg3<>0) Begin Set @Query=@Query+'and empId=3' End Exec sp_executesql(@Query); End As shown above my Query conditions will change according to the parameters. But here I am not able to get the resultant table. Please reply me if anyone know how to get the resultant Table. Also please tell me whether the above procedure is correct or not. Thanks in advance.

      D Offline
      D Offline
      Dev S
      wrote on last edited by
      #2

      Hi, You have mistakes ...

      Nekkantidivya wrote:

      Set @Query='Select * from emplayee where empId=1"

      You have ended up with double quotes.

      Nekkantidivya wrote:

      Declare @Query varchar(max)

      Replace 'max' with some number like 200 as your string cant go over this. Correct it n then check.

      modified on Saturday, April 18, 2009 7:24 AM

      1 Reply Last reply
      0
      • N Nekkantidivya

        Hi, I want to develop a stored procedure in which I have to apply different modifications to the Query. Finally after undergoing all the changes to the query I have to execute the final query and get the resultant table.For example: Create Procedure [dbo].[Test] @Flg1 bit, @Flg2 bit, @Flg3 bit, AS BEGIN SET NOCOUNT ON; Declare @Query varchar(max) if(@Flg1<>0) Begin Set @Query='Select * from emplayee where empId=1" End if(@Flg2<>0) Begin Set @Query=@Query+'and empId=2' End if(@Flg3<>0) Begin Set @Query=@Query+'and empId=3' End Exec sp_executesql(@Query); End As shown above my Query conditions will change according to the parameters. But here I am not able to get the resultant table. Please reply me if anyone know how to get the resultant Table. Also please tell me whether the above procedure is correct or not. Thanks in advance.

        H Offline
        H Offline
        Herman T Instance
        wrote on last edited by
        #3

        Create Procedure [dbo].[Test] @EmpID Int as AS BEGIN SET NOCOUNT ON Select * from emplayee where empId <= @EmpID END You better ask these question in the SQL part of this forum. They'll give you more proper answers.

        In Word you can only store 2 bytes. That is why I use Writer.

        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