SQL Query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sonia.sardana
    New Member
    • Jul 2006
    • 95

    SQL Query

    Write an update query to join the mail table with a table called mail2 where the email addresses match.Set Email field equal to 'X'.

    create table mail(email varchar(50))
    insert into mail values('sonia.s ardana@yahoo.co .in')
    insert into mail values('a.xzy@g mmail.com')


    create table mail2(email varchar(50))
    insert into mail2 values('a.xzy@g mmail.com')
    insert into mail2 values('sardana .sonia@gmail.co m')
    select * from mail2

    Final Query--
    Update mail.email set email='X' where mail.email=mail 2.email
    Error is coming Invalid object name 'mail.email'. I want to update the entries in Mail Table.
  • deric
    New Member
    • Dec 2007
    • 92

    #2
    Hi..
    You need not specify the table's field in the Update statement.
    http://msdn2.microsoft .com/en-us/library/aa260662(SQL.80 ).aspx

    Code:
    Update [B]mail[/B] set email='X' where mail.email=mail2.email

    Comment

    • Delerna
      Recognized Expert Top Contributor
      • Jan 2008
      • 1134

      #3
      Yes the basic syntax for update is
      [code=sql]
      UPDATE NameOfTableToUp date
      SET NameOfField1ToU pdate=VaueForFi eld1,
      NameOfField2ToU pdate=ValueForF ield2,
      .......etc
      [/code]


      so your error is coming from the mail.email in

      Update mail.email set email='X' .........

      it should be
      Update mail set email='X' ........

      as pointed out by the previous post

      Also the where clause is not correct because the mail2 table is not setup to be used by the query

      Comment

      • sonia.sardana
        New Member
        • Jul 2006
        • 95

        #4
        Delerna can u tell me what to write in the where clause.
        If I write the foll.Query-
        Update mail set email='X' where mail.email=mail 2.email

        Error is there-
        The multi-part identifier "mail2.emai l" could not be bound.

        Comment

        • Delerna
          Recognized Expert Top Contributor
          • Jan 2008
          • 1134

          #5
          I am unsure what your intent is here.

          What data is in table Mail2 ?
          Are you intending that Mail2 has a list of emails that you want to update within the main Mail table.

          In other words, out of all the records in Mail only update the ones that exist in Mail2.

          Or are you intending to update the email field in the mail table to the email field in the mail2 table.

          or is it something else.
          The way to write it is dependent on your intent

          Comment

          • sonia.sardana
            New Member
            • Jul 2006
            • 95

            #6
            Two tables are there Mail & Mail2. I want that if email matches in both the tables then update that all that entries in Mail Table to 'X'.

            Comment

            • Delerna
              Recognized Expert Top Contributor
              • Jan 2008
              • 1134

              #7
              [code=sql]
              UPDATE Mail
              SET Email='X'
              FROM Mail,Mail2
              WHERE Mail.Email=Mail 2.Email
              [/code]

              Comment

              • sonia.sardana
                New Member
                • Jul 2006
                • 95

                #8
                Thx,I
                t worked out.

                Comment

                Working...