存储过程意外结果

时间:2014-03-04 22:19:37

标签: sql sql-server stored-procedures

我在这个存储过程中遇到了大量编码问题。我让它工作到这一点,它将字母打印到表中的每个客户,但我需要它是状态敏感的,这样当进入程序时它只返回那些客户。我试图改变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

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

我想出了你的评论在以后的程序中有所帮助的问题,谢谢你这里是成品

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