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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.