1

hiya i have a massive job trying to sort thousands of records all is going okay, but i have a table of business and a table of users, now all the users need to have there business id applied. I have done this:

select b.id as businessid, u.id as userid
from business b, [trade_tools].[dbo].[user] u
where pcode = u.pword AND u.parent_id is null AND u.usertype_id = 5 AND u.display = 1 AND b.display = 1

this works great but i want to wrap this inside say:

update [trade_tools].[dbo].[user]
set parent_id = (select businessid FROM (select b.id as businessid, u.id as userid
from business b, [trade_tools].[dbo].[user] u
where pcode = u.pword AND u.parent_id is null AND u.usertype_id = 5 AND u.display = 1 AND b.display = 1) where userid =       currentIDWeoOnNow)

now i know this is possible i am just not working it the right way, could any SQL Pro's just lend a bit of a hand here so i can get this data to flow.

i did try running an update with an inner select however this did not work because the innrer select created more than 2 results when you are only allowed one, hence going down this route to try and get around that issue.

thanks p.s Server is MSSQL server and i am using SQL Server Management Studio to run my queries.

2
  • 1
    Could you give us the table definitions and say a word about usertype_id and display? I have a feeling that display at least is redundant. Commented Feb 16, 2011 at 10:54
  • usertype_id is business, admin, area manager etc in a seperate table, the display is to set the venue to active or not active neither of these effect what i am trying to do i am just limiting my results to specific users and businesses Commented Feb 16, 2011 at 11:02

3 Answers 3

1
  1. you have a one-to-many relationship from business to user (i.e. each business has many users but each user only belongs to one business)
  2. you are using user.pword and business.pcode to join users to their business

In that case, doing simply this might work:

update [trade_tools].[dbo].[user]
set parent_id = businessid  
from business b, [trade_tools].[dbo].[user] u
where pcode = u.pword AND u.parent_id is null AND u.usertype_id = 5 AND u.display = 1 AND b.display = 1 
Sign up to request clarification or add additional context in comments.

2 Comments

worked i think to an extent i still have 314 users that dont have a parent_id ( i.e Business ) but that could be a seperate issue and it has at least done a TON more, thankx for your help
are these 314 results returned by your original SELECT? Otherwise try relaxing the constraints in the "where" clause of your select one at the time (i.e. try removing the b.display=1 and see if the select now returns your 314 users, if it doesn't try removing the u.display=1 and so on...)
1

I think this might work this time::

update [trade_tools].[dbo].[user] set parent_id = b.id

from business b, [trade_tools].[dbo].[user] u where pcode = u.pword AND u.parent_id is null AND u.usertype_id = 5 AND u.display = 1 AND b.display = 1 and u.userid =currentIDWeoOnNow

1 Comment

How do you consider this condition in the right answer?'userid =currentIDWeoOnNow'
0

Try this:

update [trade_tools].[dbo].[user] 
set parent_id = b.id

from 
business b, [trade_tools].[dbo].[user] u 
where pcode = u.pword 
AND u.parent_id is null 
AND u.usertype_id = 5 
AND u.display = 1 
AND b.display = 1 

where userid =currentIDWeoOnNow

1 Comment

Also, correct syntax or not, try to use the code block for any code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.