Update record of two table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ranjana1980
    New Member
    • Jul 2008
    • 15

    Update record of two table

    How to update record of two tables when in Table A RegNo is primary key
    and in Table B RegNo is foreign Key and i like to update where A.RegNo=B.RegNo
  • Delerna
    Recognized Expert Top Contributor
    • Jan 2008
    • 1134

    #2
    This is a bit too vague for me to determine what code to post from hundreds of possibilities

    Can you post some sample data for the two tables as well as an example of what it is you want to update.

    Comment

    • ranjana1980
      New Member
      • Jul 2008
      • 15

      #3
      Originally posted by Delerna
      This is a bit too vague for me to determine what code to post from hundreds of possibilities

      Can you post some sample data for the two tables as well as an example of what it is you want to update.
      update Student as s,AcademicQuali fication as a set s.FName='Maya', s.LName='Sharma ',a.InstituteNm ='St Joseph',a.Unive rsity='Barkatul lah' where s.RegNo==a.Regn o

      Comment

      • amitpatel66
        Recognized Expert Top Contributor
        • Mar 2007
        • 2358

        #4
        Originally posted by ranjana1980
        update Student as s,AcademicQuali fication as a set s.FName='Maya', s.LName='Sharma ',a.InstituteNm ='St Joseph',a.Unive rsity='Barkatul lah' where s.RegNo==a.Regn o

        And what did you get after running the above query??

        Comment

        • ranjana1980
          New Member
          • Jul 2008
          • 15

          #5
          Originally posted by amitpatel66
          And what did you get after running the above query??
          I have an error where i using as clause

          Comment

          • Delerna
            Recognized Expert Top Contributor
            • Jan 2008
            • 1134

            #6
            where s.RegNo==a.Regn o


            Why the double equals?

            should be
            where s.RegNo=a.Regno

            Comment

            • ranjana1980
              New Member
              • Jul 2008
              • 15

              #7
              Originally posted by Delerna
              where s.RegNo==a.Regn o


              Why the double equals?

              should be
              where s.RegNo=a.Regno
              Its my mistake i wrote double equal actually I will try with single equal to and i face same problem

              Comment

              • amitpatel66
                Recognized Expert Top Contributor
                • Mar 2007
                • 2358

                #8
                Originally posted by ranjana1980
                Its my mistake i wrote double equal actually I will try with single equal to and i face same problem
                Do you still face any problem?..Pleas e post the error that your query displays for our reference?

                Comment

                • Delerna
                  Recognized Expert Top Contributor
                  • Jan 2008
                  • 1134

                  #9
                  A number of problems
                  1) you cannot give the table you are updating an alias (as far as I know).
                  2) You can't give update a list of tables to update like that. You just get the error "Line1: syntax error near ,"
                  3) Even if that query did work it would update every record in both tables to the same values. Or is what you want to do?


                  I suggest that you simplify it and use two seperate update queries in a stored proc or udf
                  [code=sql]
                  create proc UpdStudentAcade micQual @RegoNo as bigint,@FName as varchar(50),@LN ame as varchar(50),@In stituteNm as varchar(50),@Un iversity as varchar(50)
                  as
                  update Student
                  set FName=@FName,
                  LName=@LName,
                  where RegNo=@RegoNo

                  update AcademicQualifi cation
                  set InstituteNm=@In stituteNm,
                  University=@Uni versity
                  where Regno=@RegoNo
                  [/code]

                  Comment

                  • ranjana1980
                    New Member
                    • Jul 2008
                    • 15

                    #10
                    Originally posted by Delerna
                    A number of problems
                    1) you cannot give the table you are updating an alias (as far as I know).
                    2) You can't give update a list of tables to update like that. You just get the error "Line1: syntax error near ,"
                    3) Even if that query did work it would update every record in both tables to the same values. Or is what you want to do?


                    I suggest that you simplify it and use two seperate update queries in a stored proc or udf
                    [code=sql]
                    create proc UpdStudentAcade micQual @RegoNo as bigint,@FName as varchar(50),@LN ame as varchar(50),@In stituteNm as varchar(50),@Un iversity as varchar(50)
                    as
                    update Student
                    set FName=@FName,
                    LName=@LName,
                    where RegNo=@RegoNo

                    update AcademicQualifi cation
                    set InstituteNm=@In stituteNm,
                    University=@Uni versity
                    where Regno=@RegoNo
                    [/code]
                    Hi,
                    Thanks For this Solution.Your Previous three point I have clear regarding error.
                    I like to update different values of both the tables having same regno of both tables.
                    Can I Solve this using trigger but I don't know how to update two tables with different values with having same RegNo using trigger

                    Comment

                    • Delerna
                      Recognized Expert Top Contributor
                      • Jan 2008
                      • 1134

                      #11
                      Not sure I understand your question.

                      A trigger is a stored proc that is called when a particular event occurs on a table.
                      So you should be able to do something similar to the solution I gave you.
                      That solution updates two different tables to different values for the same RegoNo. Thats why Im unsure about your question

                      You could also put the solution I gave into a UDF

                      Comment

                      • ranjana1980
                        New Member
                        • Jul 2008
                        • 15

                        #12
                        Originally posted by Delerna
                        Not sure I understand your question.

                        A trigger is a stored proc that is called when a particular event occurs on a table.
                        So you should be able to do something similar to the solution I gave you.
                        That solution updates two different tables to different values for the same RegoNo. Thats why Im unsure about your question

                        You could also put the solution I gave into a UDF
                        I use your stored procedure code to update two tables but i get error in where clause

                        Comment

                        • Delerna
                          Recognized Expert Top Contributor
                          • Jan 2008
                          • 1134

                          #13
                          what is the error that you get

                          Comment

                          Working...