|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > Stored procedure asstance |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|