How to validate a column using stored procedure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rahulae
    New Member
    • Jul 2008
    • 29

    How to validate a column using stored procedure

    I have a column name Room_type ,the valid room types are bedroom,dining room,living room,kitchen,ba throom.

    I have to validate the room_type column such that only valid room types are allowed.
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    The problem is that invalid data has been entered.I am assuming at the front end.
    This can be rendered impossible by this design
    Code:
    I would have a seperate room_types table
    room_types
    type_id   description
    1            bedroom
    2            dining
    etc
    Then your Room_type column can only contain a valid id.
    You can easily migrate to this design and in the process filter out the invalid data.
    Or maybe not. Let me know

    Comment

    • ck9663
      Recognized Expert Specialist
      • Jun 2007
      • 2878

      #3
      Then create a relationship (FK - to - PK) between the two table.

      -- CK

      Comment

      • rahulae
        New Member
        • Jul 2008
        • 29

        #4
        its not like that i have to write a procedure where in i should be able to insert a room and while inserting i should see that room type is valid.

        valid types i have listed already.

        I cannot use one more table

        Comment

        • ck9663
          Recognized Expert Specialist
          • Jun 2007
          • 2878

          #5
          It's better if you create a CONSTRAINT.

          If some inserted a row on your table without using your SP, it might still have invalid values.

          -- CK

          Comment

          • rahulae
            New Member
            • Jul 2008
            • 29

            #6
            can you tell me how to create constraint for that because the valid room types are around 4

            Comment

            • rahulae
              New Member
              • Jul 2008
              • 29

              #7
              Is there any way to validate that column without using different table and constraint

              Comment

              • ck9663
                Recognized Expert Specialist
                • Jun 2007
                • 2878

                #8
                Here are some readings.

                -- CK

                Comment

                • rahulae
                  New Member
                  • Jul 2008
                  • 29

                  #9
                  Validation

                  How does that concept of constraints help solve my problem

                  see my problem is whenever i try to insert a room_type into a table it shouldn't allow any other types other than that i have listed.

                  what can i do to solve this.

                  will using a constraint solve this out.

                  if yes what type of constraint and give me an example
                  Last edited by rahulae; Aug 15 '08, 07:04 AM. Reason: updations

                  Comment

                  • ck9663
                    Recognized Expert Specialist
                    • Jun 2007
                    • 2878

                    #10
                    A CHECK CONSTRAINT can be created for a field to ensure that the condition has to be fulfilled first before SQL SERVER allows insert/update on the table. So if you have a constraint on your room_type column that says all valid values should be IN ('BEDROOM','DIN NING'), then only these two values will be inserted on your column, otherwise it will not allow the insert or update of the entire record.

                    -- CK

                    Comment

                    • rahulae
                      New Member
                      • Jul 2008
                      • 29

                      #11
                      plz tell me how to write it

                      i mean syntax

                      Comment

                      • rahulae
                        New Member
                        • Jul 2008
                        • 29

                        #12
                        how to write a check constraint expression to solve my problem

                        Comment

                        Working...