Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to select distinct Customer records ? Error on SELECT 1

Status
Not open for further replies.

GarHeard

Programmer
May 3, 2005
28
US
In the following Stored Procedure my objective was to select unique Customer Records. I select all the field in the Customer table. However, I can't simply use a SELECT DISCTINT statement because I also SELECT 2 fields from the Product table. There is a 1 to many relationship between the Customer table and the Products table.

When I execute the following Stored Procdure via the Query Analyzer of SQL Server, I get the following message:

Server: Msg 8152, Level 16, State 6, Procedure procFlLNUNIQUE, Line 11
String or binary data would be truncated.
The statement has been terminated.

Line 11 is as follows:
Insert into tblFlLN ([First Name], [MI], [Last Name], [Address 1], [Address 2], [Address 3], [Address 4], City,
----------------------------------------------------------
I checked the data type of the columns in tblCustomer and tblproducts as follows:

INSERT INTO tblFlLN ([First Name], [MI], [Last Name], [Address 1], [Address 2], [Address 3], [Address 4], City,
State, Zip, SSN, [Account Number],PropertyType,DateLost,IRACode,DateOfBirth )

First Name nvarchar 25
MI nvarchar 1
Last Name nvarchar 25
Address 1 nvarchar 35
Address 2 nvarchar 35
Address 3 nvarchar 35
Address 4 nvarchar 35
City nvarchar 25
State nvarchar 2
Zip nvarchar 10
SSN nvarchar 11

[Account Number] = C.OfficeNumber,3) + ' ' + C.CustomerNumber
OfficeNumber nvarchar 4
CustomerNumber nvarchar 6

PropertyType nvarchar 12
DateLost datetime 8
IRACode nvarchar 3
DateOfBirth datetime 8

-----------------------------------------------------------
CREATE PROCEDURE dbo.procFlLNUNIQUE
@RptYear int
AS
If Exists(SELECT * FROM dbo.SYSOBJECTS WHERE NAME = 'tblFlLN' AND TYPE = 'U')
DROP TABLE tblFlLN
CREATE TABLE tblFlLN ([First Name] nvarchar, [MI] nvarchar, [Last Name] nvarchar, [Address 1] nvarchar, [Address 2] nvarchar, [Address 3] nvarchar, [Address 4] nvarchar, City nvarchar,
State nvarchar, Zip nvarchar, SSN nvarchar, [Account Number] nvarchar,PropertyType nvarchar,DateLost datetime,IRACode nvarchar,DateOfBirth datetime)

Insert into tblFlLN ([First Name], [MI], [Last Name], [Address 1], [Address 2], [Address 3], [Address 4], City,
State, Zip, SSN, [Account Number],PropertyType,DateLost,IRACode,DateOfBirth )
Select distinct
UPPER (C.FirstName) AS [First Name], UPPER (C.MiddleInitial) AS [MI],
UPPER (C.LastName) AS [Last Name], UPPER(C.Address1) AS [Address 1], UPPER (C.Address2) AS [Address 2],
UPPER(C.Address3) AS [Address 3], UPPER(C.Address4) AS [Address 4], UPPER(C.City) AS City,
UPPER(C.State) AS State, C.Zip AS Zip, C.SSN,
Right(C.OfficeNumber,3) + C.CustomerNumber AS [Account Number],P.PropertyType,C.DateLost,P.IRACode,C.DateOfBirth

FROM tblStatesAll As S INNER JOIN (tblCustomers As C INNER JOIN tblProducts As P ON C.CustomerNumber = P.CustomerNumber)
ON S.StateFS = C.State
WHERE (C.RedFlag = 'N') AND
((P.PropertyType='OTHER' AND C.DateLost <= CAST((@RptYear - S.MutualFS) AS VARCHAR) + '-06-30' AND LEN(P .IRACode) = 0 ) OR
(C.DateOfBirth <= CAST(((@RptYear - S.IRAFS)-71) AS VARCHAR) + '-12-31' AND LEN(P .IRACode) > 0 ) OR
(LEN(C.DateOfBirth) = 0))
GROUP BY (C.FirstName), (C.MiddleInitial),
(C.LastName), (C.Address1),(C.Address2),
(C.Address3),(C.Address4),(C.City) ,
(C.State), C.Zip, C.SSN,
C.OfficeNumber, C.CustomerNumber,C.DateLost,C.DateOfBirth,P.PropertyType,P.IRACode
ORDER BY C.State
GO
 
Code:
CREATE TABLE tblFlLN ([First Name] nvarchar, [MI] nvarchar,...
nvarchar how much? 1 character?

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
DO you know why I received an error near the Keyword Group in the following:
This was the result of executing a stored procedure comprised of a SQL string.

CREATE TABLE tblFlLN ([First Name] nvarchar(25), [MI] nvarchar(1), [Last Name] nvarchar(25), [Address 1] nvarchar(35), [Address 2] nvarchar(35), [Address 3] nvarchar(35), [Address 4] nvarchar (35), City nvarchar,
State nvarchar(2), Zip nvarchar(10), SSN nvarchar(11), [Account Number] nvarchar(11),PropertyType nvarchar(12),DateLost datetime,IRACode nvarchar(3),DateOfBirth datetime)

INSERT INTO tblFlLN ([First Name], [MI], [Last Name], [Address 1], [Address 2], [Address 3], [Address 4], City,
State, Zip, SSN, [Account Number],PropertyType,DateLost,IRACode,DateOfBirth )
Select distinct
UPPER (C.FirstName) AS [First Name], UPPER (C.MiddleInitial) AS [MI],
UPPER (C.LastName) AS [Last Name], UPPER(C.Address1) AS [Address 1], UPPER (C.Address2) AS [Address 2],
UPPER (C.Address3) AS [Address 3], UPPER(C.Address4) AS [Address 4], UPPER(C.City) AS City,
UPPER (C.State) AS State, C.Zip AS Zip, C.SSN,
Right(C.OfficeNumber,3) + C.CustomerNumber AS [Account Number],P.PropertyType,C.DateLost,P.IRACode,C.DateOfBirth

FROM tblStatesAll As S INNER JOIN (tblCustomers As C INNER JOIN tblProducts As P ON C.CustomerNumber = P.CustomerNumber)
ON S.StateFS = C.State

WHERE (S.FallCycle= 1 AND C.RedFlag = 'N') AND ((P.PropertyType='OTHER' AND C.DateLost <= CAST((2005 - S.MutualFS) AS VARCHAR) + '-06-30' AND LEN(P.IRACode) = 0 ) OR ((P.PropertyType='MUTUAL FUND' AND C.DateLost <= CAST((2005- S.MutualFS) AS VARCHAR) + '-06-30' AND LEN(P .IRACode) = 0 ) OR (P.PropertyType='CASH' AND C.DateLost <= CAST((2005 - S.CashFS) AS VARCHAR) + '-06-30' AND LEN(P.IRACode) = 0 ) OR (P.PropertyType='EQUITY' AND C.DateLost <= CAST((2005 - S.StocksFS) AS VARCHAR) + '-06-30' AND LEN(P.IRACode) = 0 ) OR (P.PropertyType='FIXED INCOME' AND C.DateLost <= CAST((2005 - S.BondsFS) AS VARCHAR) + '-06-30' AND LEN(P.IRACode) = 0 ) OR (C.DateOfBirth <= CAST(((2005 - S.IRAFS)-71) AS VARCHAR) + '-12-31' AND LEN(P.IRACode) > 0 ) OR (LEN(C.DateOfBirth) = 0))

GROUP BY [First Name], [MI],
[Last Name] , [Address 1], [Address 2],
[Address 3], [Address 4], C.City, C.State, C.Zip, C.SSN,
[Account Number],C.DateLost,C.DateOfBirth,P.PropertyType,C.DateLost,P.IRACode

ORDER BY C.State
Server: Msg 156, Level 15, State 1, Line 16
Incorrect syntax near the keyword 'GROUP'.
 
Check WHERE condition - there is one extra '(' after OR and before 'MUTUAL FUND'

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top