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)
 Update table based on Select statement

Author  Topic 

RoLYroLLs
Constraint Violating Yak Guru

255 Posts

Posted - 2009-06-19 : 13:25:29
Hi All,

I was told I would need to make a temp table in order to make this work. I have not yet had the need for a temp table, and in the past I've heard of performace issues, so I always tried to stay away.

I'll describe what I have and what results I need. My main question is how can this be done with sprocs and/or functions. I hope I don't need to use temp tables, but if I have to, I will.

BTW: I'm using MS SQL Server 2008

Table:


create table [dbo].[users]
[userid] [int] identity(1,1) not null,
[username] [varchar](255) not null,
constraint [pk_users] primary key clustered


create table [dbo].[links]
[linkid] [int] identity(1,1) not null,
[linkname] [varchar](255) not null,
constraint [pk_links] primary key clustered


create table [dbo].[userlinks]
[userid] [int] not null,
[linkid] [int] not null,
[repeat] [int] not null


create procedure [dbo].[sp_GetLinks]
@userid int
as
begin
set nocount on;
--here i need to be able to add one to the repeat or add to the table if doesn't exist
--plus i don't know how to add one to repeat for each row selected
select top 10 *
from links
where linkid not in
(
select top 10 ul.linkid
from userlinks ul
where ul.userid = @userid
order by ul.repeat desc
)
order by (newid())
end



Users holds a list of registered users, while links are a list of about 1000 links we add. The idea is that the sproc should return 10 random links. These random links must not be any that have been return yet, and inserted into the userlinks table with the userid, linkid and a "repeat = 1". Obviously at some point all the links would have been returned, so then the goal is to return any 10 links and update the userlinks table and one to the repeat.

I am open to redesigning the db design if there is a better way to accomplish this.

Thank you in advance.


- RoLY roLLs
http://www.buysellmotors.com
http://www.tikiloveroom.com
http://www.rolyrolls.com
http://iphone.rolyrolls.com

RoLYroLLs
Constraint Violating Yak Guru

255 Posts

Posted - 2009-06-19 : 16:09:24
Opps...this is 2005...altho i don't think i would matter

ref: [url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=127918[/url]

- RoLY roLLs

http://www.buysellmotors.com
http://www.tikiloveroom.com
http://www.rolyrolls.com
http://iphone.rolyrolls.com
Go to Top of Page
   

- Advertisement -