简体   繁体   English

存储过程意外结果

[英]Stored Procedure unexpected results

I am having a little trouble with a large bit of coding in this Stored procedure. 我在此存储过程中进行了大量编码时遇到了一些麻烦。 i have it working to the point that it prints the letters to every customer in the table but i need it to be state sensitive so that when entered into the procedure it returns only those customers. 我的工作要点是它会向表中的每个客户打印字母,但我需要它对状态敏感,以便在进入该过程时仅返回那些客户。 i tried altering the while clause but i am having some unexpected results with it erasing the name of the customers. 我尝试更改while子句,但是由于擦除了客户的名字,我遇到了一些意外的结果。

Create Proc spNewsLetter
@StateID varchar(2)
as



DECLARE @CustCount INT
DECLARE @CustomerName varchar(10)
DECLARE @LastName varchar(20)
DECLARE @FirstName varchar(15) 
DECLARE @SalesOfficer varchar(20)
DECLARE @Date as varchar(12) 
DECLARE @FutureDate as varChar(12)
DECLARE @JobDescription as varchar (20)

SET @CustomerName = '       '

Set @futureDate = DATEADD(dd, 14, getdate())

SET @Date = convert(char(12),getDate(), 107)

SELECT @SalesOfficer = FirstName + ' ' + LastName, @JobDescription = JobTitle
FROM Employee WHERE JobTitle = 'Chief Sales Officer'

SELECT @CustCount = count(*) FROM Customer  

SELECT @StateID = State
FROM Customer

IF EXISTS (Select @StateID from Customer)
BEGIN
WHILE @CustCount > 0  
BEGIN

 SELECT @CustomerName = MIN(CustomerID)
   FROM Customer WHERE CustomerID > @CustomerName
 SELECT @LastName = CustLastName,
   @FirstName=CustFirstName 
   FROM Customer WHERE  CustomerID = @CustomerName
 PRINT ' '
 PRINT ' '
 PRINT ' '
 PRINT ' '
 PRINT ' Date:' + @Date  
 PRINT ' '               
 PRINT ' Dear ' + @FirstName + ' ' + @LastName + ','
 PRINT ' '
 PRINT ' Eagle is please to offer you a 20% discount on any purchase you make prior   to 11:55pm '+@FutureDate+'.'                 
 PRINT ' This limited time offer is our best offer of the year!  You can view our entire product line '
 PRINT ' and place your order at Eagle20Deal.com.  Make sure to place your order by ' +@FutureDate
 PRINT ' because this offer expires at 11:55pm on that day.'
 PRINT ' '
 PRINT ' Sincerely, ' + @SalesOfficer 
 PRINT '            '+@JobDescription           
 PRINT '            Eagle Corporation' 
 PRINT '*****************************************' 

 SET @CustCount = @CustCount - 1 

END  -- *** end of while block
END -- *** end of if
ELSE 
BEGIN
 Print 'Please enter a valid state ID'
END  -- *** end of else block

any help would be very much appreciated 任何帮助将不胜感激

I figured our the problem your comments did help some later on in the program thank you here is the finished product 我想出了我们的问题,您的评论在程序的后面对您有所帮助,谢谢。这里是成品

Create Proc spNewsLetter
@StateID varchar(2)
as

DECLARE @CustCount varchar (20)
DECLARE @CustomerName varchar(10)
DECLARE @LastName varchar(20)
DECLARE @FirstName varchar(15) 
DECLARE @SalesOfficer varchar(20)
DECLARE @Date as varchar(12) 
DECLARE @FutureDate as varChar(12)
DECLARE @JobDescription as varchar (20)
DECLARE @CompanyName Varchar(20)


SET @CustomerName = '       '

Set @futureDate = DATEADD(dd, 14, getdate())


SET @Date = convert(char(12),getDate(), 107)

SELECT @SalesOfficer = FirstName + ' ' + LastName, @JobDescription = JobTitle
FROM Employee WHERE JobTitle = 'Chief Sales Officer'

SELECT @CustCount = Count(CustomerID) FROM Customer  
WHERE @StateID = State


if @CustCount > 0   
BEGIN
 WHILE @CustCount > 0   
   BEGIN

     SELECT @CustomerName = MIN(CustomerID)
       FROM Customer WHERE CustomerID > @CustomerName and @StateID = State
     SELECT @LastName = CustLastName,
       @FirstName=CustFirstName, @CompanyName = Coalesce(CompanyName,'')
       FROM Customer WHERE  CustomerID = @CustomerName and @StateID = State
     PRINT ' '
     PRINT ' '
     PRINT ' '
     PRINT ' '
     PRINT ' Date:' + @Date  
     PRINT ' '               
     PRINT ' Dear ' + @FirstName + ' ' + @LastName + ', '+@CompanyName 
     PRINT ' '
     PRINT ' Eagle is please to offer you a 20% discount on any purchase you make prior to 11:55pm '+@FutureDate+'.'
     PRINT ' This limited time offer is our best offer of the year!  You can view our entire product line '
     PRINT ' and place your order at Eagle20Deal.com.  Make sure to place your order by ' +@FutureDate
     PRINT ' because this offer expires at 11:55pm on that day.'
     PRINT ' '
     PRINT ' Sincerely, ' + @SalesOfficer 
     PRINT '            '+@JobDescription           
     PRINT '            Eagle Corporation' 
     PRINT '*****************************************' 

     SET @CustCount = @CustCount - 1 


   END
END
ELSE 
BEGIN
     Print 'Please enter a valid state ID'
END 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM