Results 1 to 13 of 13

Thread: please help me with this query

  1. #1

    Thread Starter
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    please help me with this query

    Hello,
    Can someone help me with this query...I want to update the leadactivities column based on a value from another table.Please help.
    Code:
    UPDATE    tblLeadActivities SET f_activityName =  (SELECT     f_activityName
      FROM          tblStandardActivities) WHERE     (f_leadId = '0602HOUS3')
    I am using SQL EXPRESS 2005 with Asp.Net 2.0..the latest one.Please help me,Thanks so much muchhhhh.
    Godwin

    Help someone else with what someone helped you!

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: please help me with this query

    I believe you would need to preface your field names with their tablenames.

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: please help me with this query

    You must ensure the sub-query returns only one row, so add a where clause. If there are still multiple records use the Top clause.

    Code:
    UPDATE tblLeadActivities 
    SET f_activityName =  
                (SELECT f_activityName FROM tblStandardActivities Where ...) 
    WHERE (f_leadId = '0602HOUS3')

  4. #4
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: please help me with this query

    This seem similar...
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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

    Re: please help me with this query

    In MS SQL - UPDATES can use JOIN'd tables and alias's as long as you alter the syntax slightly...

    Code:
    Update tblLeadActivities Set f_activityname=ts.f_activityname
       From tblLeadActivities ta
       Left Join tblStandardActivities ts on ts.keycol=ta.keycol
       Where ta.checkcol={whatever} or ts.checkcol={whatever}
    The BOLD part looks just like a standard SELECT/FROM/JOIN - all the same possibilties...

    Because of that altered syntax, you can use the alias ts. on the right-side of the equals in the UPDATE/SET list.

    Hope this helps...

    *** 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
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    Re: please help me with this query

    Hello,
    Thanks for all your responses
    But,the solution for me was this...
    Code:
    declare
    	@LeadID		varchar(15),
    	@rec_count	int,
    	@idx		int
    
    select	@LeadID		= '001'
    select	@rec_count = count(*)
    	from	tblLeadActivities
    	where	f_leadId = @LeadID
    
    select	@idx = 1
    set rowcount 1	
    while @idx <= @rec_count
    begin
    	update	u
    		set	u.f_activityName = (select top 1 x.f_activityName 
    						from 	tblStandardActivities x
    						where	not exists (select * from tblLeadActivities y
    									where	y.f_activityName 	is not null
    									and	y.f_activityName 	= x.f_activityName
    									and	y.f_leadId 		= u.f_leadId)
    						order by newid()
    					    )
    	from	tblLeadActivities u
    	where	u.f_leadId		= @LeadID
    	and	u.f_activityName	is null
    	select	@idx = @idx + 1
    end
    set rowcount 0
    
    select * from tblLeadActivities
    Godwin

    Help someone else with what someone helped you!

  7. #7

    Thread Starter
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    Re: please help me with this query

    Actually that code copies the contents of one column in a table to another table...based on certain conditions.
    It was given to me by a friend.
    But,Can someone help me in mdifying that code to copy in the same order of the source table?
    Thanks
    Godwin

    Help someone else with what someone helped you!

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: please help me with this query

    So, is this problem resolved?

    If so, could you please pull down the Thread Tools menu and click the Mark Thread Resolved button. That will let everyone know that you have your answer.

    Thank you.

  9. #9

    Thread Starter
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    Re: please help me with this query

    Yes I will do that soon sir but please help me with that one last problem related to the same issue.
    Thanks
    Godwin

    Help someone else with what someone helped you!

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

    Re: please help me with this query

    Did you see my post (#5) with the enhanced syntax for an UPDATE statement with JOIN possibilities?

    *** 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

  11. #11

    Thread Starter
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    Re: please help me with this query

    Yes I did but the problem is I dont have a key linking both tables...
    Godwin

    Help someone else with what someone helped you!

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

    Re: please help me with this query

    You do not need a key to link - you can link on whatever you want...

    Isn't this your where clause?

    Code:
    where	y.f_activityName is not null
    and	y.f_activityName = x.f_activityName
    and	y.f_leadId = u.f_leadId
    That could easily be changed to an ON clause in a join.

    Regardless of whether you use a sub-query in the UPDATE or a JOIN - you still need to "related" the rows together on some condition.

    If you can show me some real data from these two tables I can work up a better example.

    *** 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

  13. #13

    Thread Starter
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    Re: please help me with this query

    Dear szlamany,
    The whole thing I actually wanted in quite complicated...It was probably my fault of not explaining the question properly.Its kind of hard to explain it through text here,but I will try.
    I have 3 tables.
    tblLeads
    tblLeadActivities
    tblStandardActivities.

    tblLeads and tblLeadactivities have a key called serial number.
    tblLeadactivities and tblStandardactivities have no key.

    Now,everytime I enter a new record into the tblLeadactivities,It is entered as 7 copies leaving the activityname field as null.
    Then,I need to select the list of activity names from the tblStandardActivities table and fill them up each for each record in the activitiyname column the tblLeadActivities table.

    I hope you got the picture now.

    Thanks for your time again
    Godwin

    Help someone else with what someone helped you!

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