Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 2nd, 2004, 11:06 AM
Authorized User
 
Join Date: Sep 2003
Posts: 93
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to flyin
Default Stored Procedure help

This is very interesting,, but might not work in all cases for me and let me explain why.

I'm sorry i did not include this in the beginning.

The tb_Points table will store points and rank for a number of stats.

There also exists a table:

tb_PointsType
Fields:
PointsTypeID
Description

Lets re-create the tb_Points so that it is more clear
Table 2: tb_Points
AccountID
PointsTypeID
Points
Rank



Again with some dummy data.

Table: tb_PointsType
1 ArticlePoints
2 ReplyPoints
3 RatingPoints

Table: tb_Points
AccountID PointsTypeID Points Rank
1 1 3000 (trying to populate this)
1 3 234 " " "
34 1 4500
34 2 1234

So i can't drop the whole table all the time because the way this is going to work is that
I will be taking 1 PointsTypeID at a time, calculating the points from it's related table, like tb_Articles For instance,
then i will be doing an update or insert int tb_Points with my new calculations.

So for instance,

I will take tb_PointsType table, and grab the appropriate table, calculate points for all who belong to that table(tb_Articles for instance) then
with that i want to store the points, rank, PointsTypeID, and AccountID in tb_Points.

SampleCode:

SQL = "SELECT * FROM tb_PointsType Order By PointsTypeID"
dr = DAL.ExecuteQUERY(SQL)

Do while dr.read()
    Select Case dr("PointsTypeID")
        case ePointsType.ArticlePoints
            SQL = "sp_BatchUpdateArticlePonts " & dr("AccountID")
        case ePointsType.ReplyPoints
            SQL = "sp_BatchUpdateReplyPonts " & dr("AccountID")
        case ePointsType.RatingPoints
        SQL = "sp_BatchUpdateRatingPonts " & dr("AccountID")
        .
        .

        cmd.ExecuteNonQuery(SQL)
LOOP


Create Procedure sp_BatchUpdateArticlePonts(@AccountID)

Select Sum(DailyPoints + TotalPoints) as Sum Points, AccountID, 1 As PointsTypeID FROM tb_Accounts
Group By AccountID
Order by Points DESC

i hope i'm being more clear. if not please let me know! thanks all!
 
Old August 2nd, 2004, 01:29 PM
Authorized User
 
Join Date: Jul 2004
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Set RANK to bigint and set it as identity yes, seed 1. When you do your insert it will increase it by one for each new record. this will work for a one time shot, do you plan on clearing the tbPoints table and repopulating it? if so you would have to drop and recreate the table each time you run the stored procedure

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tbPoints]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[tbPoints]
GO

CREATE TABLE [dbo].[tbPoints] (
    [RANK] [bigint] IDENTITY (1, 1) NOT NULL ,
    [POINTS] [bigint] NOT NULL ,
    [ACCOUNTID] [bigint] NOT NULL
) ON [PRIMARY]
GO
 
Old August 2nd, 2004, 02:57 PM
Authorized User
 
Join Date: Sep 2003
Posts: 93
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to flyin
Default

This is very interesting,, but might not work in all cases for me and let me explain why.

I'm sorry i did not include this in the beginning.

The tb_Points table will store points and rank for a number of stats.

There also exists a table:

tb_PointsType
Fields:
PointsTypeID
Description

Lets re-create the tb_Points so that it is more clear
Table 2: tb_Points
AccountID
PointsTypeID
Points
Rank



Again with some dummy data.

Table: tb_PointsType
1 ArticlePoints
2 ReplyPoints
3 RatingPoints

Table: tb_Points
AccountID PointsTypeID Points Rank
1 1 3000 (trying to populate this)
1 3 234 " " "
34 1 4500
34 2 1234

So i can't drop the whole table all the time because the way this is going to work is that
I will be taking 1 PointsTypeID at a time, calculating the points from it's related table, like tb_Articles For instance,
then i will be doing an update or insert into tb_Points with my new calculations.

So for instance,

I will take tb_POintsType, and grab each table the appropriate table, calculate points for all who belong to that table(tb_Articles for instance) then
with that i want to store the points, rank, PointsTypeID, and AccountID in tb_Points.

SampleCode:

SQL = "SELECT * FROM tb_PointsType Order By PointsTypeID"
dr = DAL.ExecuteQUERY(SQL)

Do while dr.read()
    Select Case dr("PointsTypeID")
        case ePointsType.ArticlePoints
            SQL = "sp_BatchUpdateArticlePonts " & dr("AccountID")
        case ePointsType.ReplyPoints
            SQL = "sp_BatchUpdateArticlePonts " & dr("AccountID")
        case ePointsType.RatingPoints
        SQL = "sp_BatchUpdateArticlePonts " & dr("AccountID")
        .
        .

        cmd.ExecuteNonQuery(SQL)
LOOP


i hope i'm being more clear. if not please let me know! thanks all!
 
Old August 3rd, 2004, 03:09 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Usually this is how I would do this. You'll need to check the syntax as I'm not sitting in front of a SQL console to verify:

Create Procedure sp_BatchUpdateArticlePonts(@AccountID)
Select Sum(DailyPoints + TotalPoints) as Sum Points, AccountID
INTO #Rank
FROM tb_Accounts
Group By AccountID
Order by Points DESC

MODIFY TABLE #Rank ADD Rank INT IDENTITY

UPDATE tb_Accounts SET Rank=tmp.Rank
FROM #Rank WHERE tb_Accounts.AccountID=tmp.AccountID

DROP TABLE #Rank

Try that and let us know if it works.
 
Old August 3rd, 2004, 07:37 AM
Authorized User
 
Join Date: Sep 2003
Posts: 93
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to flyin
Default

that worked great with some tweaks! thanks a bunch!!






Similar Threads
Thread Thread Starter Forum Replies Last Post
stored procedure prashant_telkar SQL Server 2000 1 July 9th, 2007 07:57 AM
Stored Procedure jezywrap SQL Server ASP 1 January 3rd, 2007 12:29 AM
stored procedure kdm260 SQL Server 2000 2 June 19th, 2006 04:45 PM
Stored Procedure rajanikrishna SQL Server 2000 0 July 18th, 2005 05:01 AM
Help About Stored Procedure zhuge6 BOOK: ASP.NET Website Programming Problem-Design-Solution 3 May 20th, 2005 09:27 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.