Results 1 to 26 of 26

Thread: [RESOLVED] Update record on SQL

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    97

    Resolved [RESOLVED] Update record on SQL

    I am using MS SQL SERVER 2000 with a PERL SCRIPT.

    Code:
    UPDATE [CustSat].Customers_T
    	SET [CustSat].[Customers_T].LastEmailSent = GETDATE(),[CustSat].[Customers_T].LastTicketSent = TicketAssignment_T.TicketNumber
    	SELECT TOP $survey_total TicketAssignment_T.Customer_ID, TicketNumber, LanID, EmailAddress, ShortDescription
    	FROM TicketAssignment_T
    	INNER JOIN Customers_T
    	ON (TicketAssignment_T.Customer_ID = Customers_T.Customer_ID)
    	WHERE((DATEDIFF(day,Customers_T.LastEmailSent, GETDATE()) > $customer_days) OR (Customers_T.LastTicketSent IS NULL))
    	ORDER BY NEWID()
    Where ever you see a $ is a variable from a perl script.


    Anyways, what the code is supposed to do is just update the TOP #, which are selected randomly by used ORDER BY NEWID(), and update those values with the current date. Instead, it is updating every row in the table with the current date. How could I change this?
    Last edited by mlosso; Jul 10th, 2006 at 07:51 AM.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    97

    Re: Update record on SQL

    I really need help on this? Can you do UPDATES on INNER JOINS?

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Update record on SQL

    UPDATE/FROM is the syntax you are looking for...

    Code:
    UPDATE Customers_T
    	SET	 LastEmailSent = GETDATE()
                              ,LastTicketSent = T1.TicketNumber
    
    	From Customers_T C1
    	Left Join TicketAssignment_T T1 ON (T1.Customer_ID
    						= C1.Customer_ID)
    	WHERE((DATEDIFF(day,C1.LastEmailSent, GETDATE()) > (...SUBQUERY GOES HERE...)
    		OR (C1.LastTicketSent IS NULL))
    Something like that should work. The UPDATE/SET should not have aliases (like the C1) - they are part of the FROM/JOIN select. The RIGHT-SIDE of the = sign in the SET's can have ALIAS's.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    97

    Re: Update record on SQL

    You are awesome, only a few things:

    1. After the WHERE Clause, it will not allow me to do: ORDER BY NEWID(). That is really important, becuase this needs to be a random selection

    2. At this line of code:

    Code:
    SET	 LastEmailSent = GETDATE()
                              ,LastTicketSent = T1.TicketNumber
    It said T1 is not apart of the Customer_T table, therefore I cant reference it.

    Also the DATEDIFF comparison is to a variable value that I send in.
    Last edited by mlosso; Jul 10th, 2006 at 11:18 AM.

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Update record on SQL

    I don't understand the need for a random update - you cannot put an ORDER BY on an UPDATE - so please clarify your reason.

    The T1 error makes no sense - we are joining the "T1" alias table to our "C1" table - that makes those aliases available on the right-side of the equal sign.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    97

    Re: Update record on SQL

    The reason for the random update is because, I am selecting a specified amount of rows to send survey's too. Those who are sent surveys are supposed to be chosen at random, to the amount specified as long as the fit the date criteria. It really isnt a random update, more of a random selection and then out of those randomly selected, they are updated with the new date.

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Update record on SQL

    Well - since this is MS SQL 2000 I would make a STORED PROCEDURE to do this job...

    Seems like you would need to create either a TABLE VARIABLE (if not too many rows) or a TEMP TABLE with the "selected" records to choose. Just the primary keys is enough...

    Then the SPROC would update from that "table" of primary keys...

    Or on second thought maybe you could:

    WHERE PRIKEY IN (SELECT TOP 100 PRIKEY FROM ... ORDER BY NEWID())

    That might work...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    97

    Re: Update record on SQL

    I wanted to do a stored procedure, but cannot because I am using PERL to send out these emails.

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Update record on SQL

    Quote Originally Posted by mlosso
    I wanted to do a stored procedure, but cannot because I am using PERL to send out these emails.
    I have never used PERL...

    Maybe my "second thought" suggestion will work...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    97

    Re: Update record on SQL

    Trying it now...I dont really understand that that is doing though..

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    97

    Re: Update record on SQL

    Ok I tried this:

    Code:
    UPDATE Customers_T
    	SET	 LastEmailSent = GETDATE()
    
    	From Customers_T 
    	Left Join TicketAssignment_T
    		ON (TicketAssignment_T.Customer_ID = Customers_T.Customer_ID)
    		WHERE Customers_T.Customer_ID IN 
    			(SELECT TOP 40 TicketAssignment_T.Customer_ID, TicketNumber, LanID, EmailAddress, ShortDescription
    					FROM TicketAssignment_T
    					INNER JOIN Customers_T
    					ON (TicketAssignment_T.Customer_ID = Customers_T.Customer_ID)
    					WHERE DATEDIFF(day,Customers_T.LastEmailSent, GETDATE()) > 115
    					ORDER BY NEWID()
    			 )
    I get the following error from query analyzer:

    Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    97

    Re: Update record on SQL

    Ok, after I removed everything in the select list except for the first thing, it now works the way I want it too, except I need to figure out how I can get those other column headings

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    97

    Re: Update record on SQL

    Soooo close

  14. #14
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Update record on SQL

    The "WHERE...IN..." only wants one single column - it's a silly trick to populate a list of id's to select from.

    But this is important:

    Code:
    UPDATE Customers_T
    	SET	 LastEmailSent = GETDATE()
    
    	From Customers_T C1
    	Left Join TicketAssignment_T T1
    Those ALIAS assignments allow you to reference the fields from the C1 or T1 tables - realize that there are really three tables being hit in this type of UPDATE - the CUSTOMERS_T (without alias) is the UPDATE table - that's the ANSI standard syntax allowed for UPDATE. Having an UPDATE with a FROM clause like this brings in two other tables - CUSTOMERS_T again (with ALIAS) and TICKETASSIGNMENT_T (with ALIAS). Those C1.ColumnName or T1.ColumnName values should be available on the right-side of the = sign in the SET part of the UPDATE.

    It's bothering me a little that you decided to go with ALIAS in the sub-query. SQL can sometimes pull the wrong table (you have like 5 tables in this query now) when you do not use an ALIAS with ambiguous references like this. You should have C2 and T2 aliases in that sub-query...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    97

    Re: Update record on SQL

    Ok, I did the recommended changes for aliases, but I really need to figure out how get those other colums to return the row columns that I need.

  16. #16
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Update record on SQL

    Are you saying it won't let you add this line????

    ,LastTicketSent = T1.TicketNumber

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    97

    Re: Update record on SQL

    No,

    on the last select, i need to retrieve these column headings:

    SELECT TOP 40 TicketAssignment_T.Customer_ID, TicketNumber, LanID, EmailAddress, ShortDescription

    But I cannot because of the WHERE IN clause before. How can I get around this?

  18. #18
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Update record on SQL

    Why do you need those other columns?

    Are they all part of the "selection" of the record to be updated?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    97

    Re: Update record on SQL

    Yes, this data will be used when I send out the emails. They must be retrieved.

  20. #20
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Update record on SQL

    Quote Originally Posted by mlosso
    Yes, this data will be used when I send out the emails. They must be retrieved.
    I'm not seeing how that makes any sense...

    This is an UPDATE statement - it's not returning a recordset - right?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  21. #21

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    97

    Re: Update record on SQL

    I am trying to do both in one statment. I figured this had to be done because of the random selection, ie had to update while I was selecting...

  22. #22

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    97

    Re: Update record on SQL

    The idea is basically, while I am randomly selecting the rows based on the criteria, I am also updating their timestamp...

  23. #23
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Update record on SQL

    I'm thinking that's not possible - unless someone else has an idea...

    I know that MS SQL 2005 allows a SELECT output from an UPDATE - that's in the sticky at the top of the DB section of this forum - but that's new syntax...

    All your updated rows will have the same GETDATE() in that spot - can't you use that for selection?

    I don't know PERL and how it works - so I'm not going to be of much more help...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  24. #24

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    97

    Re: Update record on SQL

    All your updated rows will have the same GETDATE() in that spot - can't you use that for selection?
    Perl is just the language I am using the execute these statments..nothing more. But you are a genius, thats what I need to do.... I think I tried it before, I just didnt do it right. I wish I could execute a stored prodcedure in perl, cause I would just save that GETDATE and use a selection. Thanks, I think I will try to go about it that way.

  25. #25
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Update record on SQL

    You should be able to:

    Code:
    SELECT * FROM ... WHERE LASTEMAILSENT
                    = (SELECT MAX(LASTEMAILSENT) FROM ...)
    Good luck!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  26. #26

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    97

    Re: [RESOLVED] Update record on SQL

    Marking this as resolved for now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width