MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 24th, 2004, 03:56 PM
ndn_24_7 ndn_24_7 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 6 ndn_24_7 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Smile Stored procedure asstance

Hello all,
I’m requesting some assistance with a stored procedure. There are total of 5 tables that interact with the stored procedure. The tables are listed below. The syntax might be a little off but the basic structure is a follows:

Tables

CDS_Player
Player_ID int (4) not null
FirstName varchar (30) not null
LastName varchar (30) not null

CDS_Account
Primary_ID int (4) not null [This is the same as player_ID in CDS_Player, which is joined to get the players address]
Address1A varchar (50) not null,
City1 varchar (30) null,
State1 varchar (30) null,
Zip1 varchar (30) null

DrawingVoucherNumbers
PromoID int (4)
PlayerID int (4)
VoucherNumber int (4),
Issued char (1),
IssueDate datetime,
UserID int (4)

DrawingVouchers
PromoID int 4
PlayerID int 4
TotalPoints int 4
VouchersEarned int 4
VouchersIssued int 4

VoucherConfig
PromoID int 4
PromotionName varchar 50
StartDate datetime 8
EndDate datetime 8
PtsPerVoucher int 4







Stored Procedure

CREATE PROCEDURE dbo.Report_IssueDrawingVoucher
@@PlayerId INT,
@@PromoID INT,
@@UserID INT
AS

Declare @@StartDate Datetime,
@@EndDate Datetime,
@@PtsPerVoucher INT,
@@IssueDate DateTime

select @@Startdate = Startdate from VoucherConfig where PromoID=@@PromoID
select @@EndDate = EndDate from VoucherConfig where PromoID=@@PromoID
select @@PtsPerVoucher = PtsPerVoucher from VoucherConfig where PromoID=@@PromoID
select @@IssueDate = getdate()


if not exists(select PlayerID from DrawingVouchers
where PromoID = @@PromoID
and PlayerID = @@PlayerID)

insert into DrawingVouchers
values(@@PromoID, @@PlayerID, 0, 0, 0)

Update DrawingVouchers
set TotalPoints = (select sum(PtsEarned) from CDS_StatDetail
where IDType = 'P'
and Meta_Id = @@PlayerID
and Gamingdate between @@StartDate and @@EndDate)
where PlayerID = @@PlayerID


Update DrawingVouchers
set VouchersEarned = TotalPoints/@@PtsPerVoucher
where PlayerID = @@PlayerID

while exists (select 1 from DrawingVouchers
where Playerid = @@PlayerID
and VouchersIssued < VouchersEarned)
BEGIN
insert into DrawingVoucherNumbers (PromoID, PlayerID, UserID, IssueDate)
(select PromoID, Playerid, @@UserID, @@IssueDate from DrawingVouchers
where PlayerID = @@PlayerID
and PromoId = @@PromoID)

update DrawingVouchers
set VouchersIssued = VouchersIssued + 1
where PlayerID = @@PlayerID
and PromoId = @@PromoID

END

CREATE TABLE ##Voucher (
PromotionName varchar (50) NOT NULL ,
Player_ID int NOT NULL ,
FirstName varchar (30) NOT NULL ,
LastName varchar (30) NOT NULL ,
Address1A varchar (50) NULL ,
City1 varchar (30) NULL ,
State1 varchar (30) NULL ,
Zip1 varchar (30) NULL ,
VoucherNumber int NOT NULL ,
UserID int NOT NULL ,
IssueDate datetime,
IssueTime varchar(10)
) ON [PRIMARY]


while exists (select 1 from DrawingVoucherNumbers
where Playerid = @@PlayerID
and Issued = 'N')
BEGIN
Insert into ##Voucher
Select n.PromotionName, p.Player_ID, p.FirstName, p.LastName, a.Address1A, a.City1, a.State1, a.Zip1, d.VoucherNumber, d.UserID, d.Issuedate,
cast(datepart(hh, IssueDate)as varchar) + ':' + cast(datepart(mi, IssueDate)as varchar) + ':' + cast(datepart(ss, IssueDate)as varchar)
from CDS_Player p, CDS_Account a, DrawingVoucherNumbers d, VoucherConfig n
where p.Player_ID = a.Primary_ID
and p.Player_ID = d.PlayerID
and d.PromoID = n.PromoID
and p.Player_ID = @@PlayerID
and d.Issued = 'N'

Update DrawingVoucherNumbers
set Issued = 'Y'
where PlayerID = @@PlayerID
END

select PromotionName, Player_ID, FirstName, LastName, Address1A, City1, State1, Zip1, VoucherNumber, UserID, IssueDate, IssueTime from ##Voucher

drop table ##Voucher


RETURN (0)
GO

This program prints one drawing voucher for every 300 points a player has earned within a specific date range. This date range is located in the VoucherConfig Table. The contents of the table are as follows:

PromoID PromotionName StartDate EndDate PtsPerVoucher
1 THE GREAT CASH GIVEAWAY IN JULY 6/15/2004 8/31/2004 300
2 THE GREAT FALL CASH GIVEAWAY 9/1/2004 10/30/2004 300
3 CRAZY CHRISTMAS CASH 11/1/2004 12/24/2004 300

This stored procedure interfaces with a Crystal Report to print the tickets. The problem I’m having is that some of the data in VouchersIssued in the DrawingVouchers table exceeds VouchersEarned. VouchersIssued should never exceed VouchersEarned. In some of the rows, I’m showing VouchersIssued being and outrageous number like 21988 when VouchersEarned is 20. I can’t figure out where the VouchersIssued is getting this outrageous number. Everything in the stored procedure looks right, but I haven’t had that much experience in writing stored procedure.
Any assistance will be greatly appreciated. If theres any more information needed, please let me know, and I will get it

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Stored procedure asstance


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT