• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

sql server question

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    sql server question

    sql server 2005

    I have two tables

    table A has columns id, stepvalue, status
    table B has many columns id, 'F' & stepvalue ,,,,,,,,,


    I want to put A.status into B.F??


    I tried this using two SP's , a wrapper with a cursor plus one that executed each stepvalue but it was way too slow


    A has 6k rows
    B has 100
    (\__/)
    (>'.'<)
    ("")("") Born to Drink. Forced to Work

    #2
    I'm no SQL Server expert but can you try an inner join:

    Code:
    UPDATE b SET f=a.status FROM b INNER JOIN a ON b.id = a.id

    Comment


      #3
      This will only work if your IDs are not duplicated

      Code:
      update B 
      inner join
      (select A.status, A.id
      from A inner join B
      on A.id = B.id) derivedT
      on derivedT.id = B.id
      set F = derviedT.status
      Last edited by MrRobin; 11 March 2008, 13:34. Reason: slightly wrong order
      It's about time I changed this sig...

      Comment


        #4
        finally figured out what you meant...

        you want the F column in table B to hold the concatenated statuses from the A table matched by ID and Step value
        ie F = status1|status2|status3......

        before i go into any SQL.... why the funk do you want to do this? it goes against every single RDBMS principle!
        Coffee's for closers

        Comment


          #5
          Originally posted by MrRobin View Post
          This will only work if your IDs are not duplicated

          Code:
          update B 
          inner join
          (select A.status, A.id
          from A inner join B
          on A.id = B.id) derivedT
          on derivedT.id = B.id
          set F = derviedT.status
          the whole point is that I dont know f

          f is determined by one of the columns in table A
          (\__/)
          (>'.'<)
          ("")("") Born to Drink. Forced to Work

          Comment


            #6
            Originally posted by EternalOptimist View Post
            the whole point is that I dont know f

            f is determined by one of the columns in table A


            Yes... 'F' is determined by 'Status' in the other table, right? So you select all the statuses in A that also have an ID in table B and then match them up.
            It's about time I changed this sig...

            Comment


              #7
              Originally posted by MrRobin View Post


              Yes... 'F' is determined by 'Status' in the other table, right? So you select all the statuses in A that also have an ID in table B and then match them up.

              no, f is the column name
              (\__/)
              (>'.'<)
              ("")("") Born to Drink. Forced to Work

              Comment


                #8
                Yes, I know it is.

                You're not making much sense I'm afraid...
                It's about time I changed this sig...

                Comment


                  #9
                  Originally posted by Spacecadet View Post
                  finally figured out what you meant...

                  you want the F column in table B to hold the concatenated statuses from the A table matched by ID and Step value
                  ie F = status1|status2|status3......

                  before i go into any SQL.... why the funk do you want to do this? it goes against every single RDBMS principle!
                  I know its all screwy. Its a legacy system that has a pivot table, where the end table (b) has column names that correspond to a step value in table a.column.stepvalue

                  eg

                  table A

                  ID stepvalue status

                  1, 1, y,
                  1, 3, y,
                  1 , 4 , y,
                  2, 1, n,
                  2, 3, y,


                  table b

                  ID F01 F02 F03 F04===>
                  1, y, y, , y,
                  2, n , , y, ,



                  the problem is, I cant guarantee that the columns are there
                  plus using a wrapper and a cursor is way too slow

                  I am working on pivoting A right now, to get the b@astards to line up....unless anyone has a slicker idea





                  (\__/)
                  (>'.'<)
                  ("")("") Born to Drink. Forced to Work

                  Comment


                    #10
                    which SQL Server version are you using?

                    run this if you're not sure what you've connected to:
                    SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY ('productlevel'), SERVERPROPERTY ('edition')

                    If you're using 2005 then there are pivot functions that can help
                    Coffee's for closers

                    Comment

                    Working...
                    X