Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Insert Multiple Data

Author  Topic 

DonDen
Starting Member

4 Posts

Posted - 2011-09-30 : 14:44:38
Some what new to SQL and I am trying to update a table based on the userid and areaid from two other tables, User table and Area table. The table I am updating is userarea and gives permisison to each are by user role. Here is what I have at this time along with the error I am getting.

INSERT INTO userarea ( userid, areaID )
VALUES((select userID from [user]where RoleID = 4294967304)
,(SELECT AreaID FROM area WHERE AreaID = 10488310136835))

Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.

Also wantedd to mention that if there is just one user in the role than the obove statement works.


Here are the reults of the individual slect statements.

userID

12884902519
12884902521
4582730104834
8349416423427
8349416423429
8349416423432
8349416423434
8349416423436
8349416423438
8349416423440
8349416423442
8349416423444
8349416423446
8349416423448
8349416423452
8349416423457
10475425234947

AreaID

10488310136835

This is the end result of the userarea table I am looking for.

userID areaID
12884902519 10488310136835
12884902521 10488310136835
4582730104834 10488310136835
8349416423427 10488310136835
8349416423429 10488310136835
8349416423432 10488310136835
8349416423434 10488310136835
8349416423436 10488310136835
8349416423438 10488310136835
8349416423440 10488310136835
8349416423442 10488310136835
8349416423444 10488310136835
8349416423446 10488310136835
8349416423448 10488310136835
8349416423452 10488310136835
8349416423457 10488310136835
10475425234947 10488310136835

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-09-30 : 15:02:12
Show us the data that both of those select statements return.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-09-30 : 16:03:44
INSERT INTO userarea ( userid, areaID )
select userID, 10488310136835
from [user]
where RoleID = 4294967304

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

DonDen
Starting Member

4 Posts

Posted - 2011-09-30 : 16:41:33
Thanks Tara, that did the trick.

To expand slightly would there be away to do this for 65 areaids at once?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-09-30 : 17:00:23
Yes, but it depends how you want to match up the userids to the areaids. If you want a cartesian product, then you can use a cross join.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

DonDen
Starting Member

4 Posts

Posted - 2011-09-30 : 17:14:23
Each userid would need to match up with each areaid for instance:
userid1 areaid1
userid1 areaid2
userid1 areaid3

DonDen
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-09-30 : 17:16:20
Yeah a cartesian product:

select userID, areaID
from [user]
cross join area

If that gives you the correct output, then add the INSERT above it.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

DonDen
Starting Member

4 Posts

Posted - 2011-09-30 : 17:56:25
tara, thanks for your help. This will get me what I need.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-09-30 : 18:18:20
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -