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. Problem with piece of code

Problem with piece of code

Scheduled Pinned Locked Moved Database
databasehelpquestiontutorial
2 Posts 2 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.
  • C Offline
    C Offline
    ChrisFarrugia
    wrote on last edited by
    #1

    I have this stored procedure on which I would like to apply if statements at the where clause. But it seems that sql is finding errors associated with the if statements. Can anyone please guide me on what is wrong. Maybe it is not possible to use if statemnts in where clauses? @category int,@brand int ---> parameters AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; select equipment_id,equipment_modelNumber,equipment_description,category,brand,link,equipment_small_image,equipment_largr_image FROM Gen_Equipment INNER JOIN Gen_Equipment_Categories INNER JOIN Gen_Equipment_Brand INNER JOIN Gen_Equipment_Link ON (Gen_Equipment.equipment_category=Gen_Equipment_Categories.category_id) ON (Gen_Equipment.equipment_brand=Gen_Equipment_Brand.brand) ON (Gen_Equipment.equipment_link=Gen_Equipment_Link.link_id) WHERE if (NOT(@category=0) AND @brand=0) THEN BEGIN Gen_Equipment.equipment_category=@category order by Gen_Equipment.equipment_category END ELSE IF (@category=0 AND NOT(@brand=0)) THEN BEGIN equipment_brand=@brand END END Thank you very much for your help. Chris

    C 1 Reply Last reply
    0
    • C ChrisFarrugia

      I have this stored procedure on which I would like to apply if statements at the where clause. But it seems that sql is finding errors associated with the if statements. Can anyone please guide me on what is wrong. Maybe it is not possible to use if statemnts in where clauses? @category int,@brand int ---> parameters AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; select equipment_id,equipment_modelNumber,equipment_description,category,brand,link,equipment_small_image,equipment_largr_image FROM Gen_Equipment INNER JOIN Gen_Equipment_Categories INNER JOIN Gen_Equipment_Brand INNER JOIN Gen_Equipment_Link ON (Gen_Equipment.equipment_category=Gen_Equipment_Categories.category_id) ON (Gen_Equipment.equipment_brand=Gen_Equipment_Brand.brand) ON (Gen_Equipment.equipment_link=Gen_Equipment_Link.link_id) WHERE if (NOT(@category=0) AND @brand=0) THEN BEGIN Gen_Equipment.equipment_category=@category order by Gen_Equipment.equipment_category END ELSE IF (@category=0 AND NOT(@brand=0)) THEN BEGIN equipment_brand=@brand END END Thank you very much for your help. Chris

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      ChrisFarrugia wrote:

      I have this stored procedure on which I would like to apply if statements at the where clause. But it seems that sql is finding errors associated with the if statements. Can anyone please guide me on what is wrong. Maybe it is not possible to use if statemnts in where clauses?

      You can't use IF statements in WHERE clauses. IF statements stand alone and do not form part of another statement (in this case the SELECT statement) Your best bet is to put the IF statement around the SELECT

      IF ((@category<>0) AND @brand=0)
      BEGIN
      SELECT equipment_id, equipment_modelNumber, equipment_description, category, brand,
      link, equipment_small_image, equipment_largr_image
      FROM Gen_Equipment
      INNER JOIN Gen_Equipment_Categories
      INNER JOIN Gen_Equipment_Brand
      INNER JOIN Gen_Equipment_Link
      ON (Gen_Equipment.equipment_category=Gen_Equipment_Categories.category_id)
      ON (Gen_Equipment.equipment_brand=Gen_Equipment_Brand.brand)
      ON (Gen_Equipment.equipment_link=Gen_Equipment_Link.link_id)
      WHERE Gen_Equipment.equipment_category=@category
      ORDER BY Gen_Equipment.equipment_category
      END
      ELSE
      BEGIN
      SELECT equipment_id, equipment_modelNumber, equipment_description, category, brand,
      link, equipment_small_image, equipment_largr_image
      FROM Gen_Equipment
      INNER JOIN Gen_Equipment_Categories
      INNER JOIN Gen_Equipment_Brand
      INNER JOIN Gen_Equipment_Link
      ON (Gen_Equipment.equipment_category=Gen_Equipment_Categories.category_id)
      ON (Gen_Equipment.equipment_brand=Gen_Equipment_Brand.brand)
      ON (Gen_Equipment.equipment_link=Gen_Equipment_Link.link_id)
      WHERE equipment_brand=@brand
      END


      -- Always write code as if the maintenance programmer were an axe murderer who knows where you live. Upcoming FREE developer events: * Glasgow: Agile in the Enterprise Vs. ISVs, Mock Objects, SQL Server CLR Integration, Reporting Services, db4o ... * Reading: SQL Bits My website

      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