Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Error

Author  Topic 

Mohamed Faisal
Yak Posting Veteran

51 Posts

Posted - 2012-10-03 : 12:51:12
Hi All,

Am having some problems in adding values to a table.

My table would be:
CREATE TABLE ClientTable
(
clientID char(5) NOT NULL,
clientName Char(30) NOT NULL,
clientAddress char(50) NOT NULL,
clientContact Numeric(8) NOT NULL,
CONSTRAINT ClientTablePK PRIMARY KEY(clientID),
CHECK(clientID like '[A-Z][0-9][0-9][0-9][A-Z]'),
CHECK (clientContact like '[6][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' OR
clientContact like'[0-9][0-9][0-9][0-9][0-9][0-9][0-9]' OR
clientContact like'[9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]')
);

CREATE TABLE CaseTable
(
referenceNum int NOT NULL IDENTITY(100000,1),
startDate DATETIME NOT NULL,
endDate DATETIME NULL,
caseDetail Char(255) NOT NULL,
caseType Char NOT NULL DEFAULT'copyright and trademark',
clientID Char(5) NOT NULL,
CONSTRAINT CaseTablePK PRIMARY KEY(referenceNum),
CONSTRAINT ClientTableFK FOREIGN KEY(clientID)
REFERENCES ClientTable(clientID),
CONSTRAINT caseTypeValues CHECK(caseType LIKE 'intellectual property enforcement' OR
caseType LIKE'copyright and trademark' OR caseType LIKE'patent and industrial design' OR
caseType LIKE'trade secret' OR caseType LIKE 'risk management' OR
caseType LIKE'litigation'),
CONSTRAINT clientIDValues CHECK(clientID LIKE '[A-Z][0-9][0-9][0-9][A-Z]')
);


And when i try to add:
INSERT INTO CaseTable (startDate , caseDetail , caseType, clientID)
VALUES (03/10/2012 , 'To commercialise for cooling system CS1F3', 'intellectual property enforcement', 'C123X')

I get the following error:
Msg 8152, Level 16, State 14, Line 1
String or binary data would be truncated.
The statement has been terminated.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-03 : 12:59:16
reason is you missed specifying length for caseType field so that it resolved to its default length value. it should be


...
CREATE TABLE CaseTable
(
referenceNum int NOT NULL IDENTITY(100000,1),
startDate DATETIME NOT NULL,
endDate DATETIME NULL,
caseDetail Char(255) NOT NULL,
caseType Char(255) NOT NULL DEFAULT'copyright and trademark',
clientID Char(5) NOT NULL,
CONSTRAINT CaseTablePK PRIMARY KEY(referenceNum),
CONSTRAINT ClientTableFK FOREIGN KEY(clientID)
REFERENCES ClientTable(clientID),
CONSTRAINT caseTypeValues CHECK(caseType LIKE 'intellectual property enforcement' OR
caseType LIKE'copyright and trademark' OR caseType LIKE'patent and industrial design' OR
caseType LIKE'trade secret' OR caseType LIKE 'risk management' OR
caseType LIKE'litigation'),
CONSTRAINT clientIDValues CHECK(clientID LIKE '[A-Z][0-9][0-9][0-9][A-Z]')
);

...



make sure you read this

http://visakhm.blogspot.com/2010/02/importance-of-specifying-length-in.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-10-03 : 13:00:19
Make the two changes shown in red
CREATE TABLE ClientTable
(
clientID CHAR(5) NOT NULL,
clientName CHAR(30) NOT NULL,
clientAddress CHAR(50) NOT NULL,
clientContact NUMERIC(8) NOT NULL,
CONSTRAINT ClientTablePK PRIMARY KEY(clientID),
CHECK (clientID LIKE '[A-Z][0-9][0-9][0-9][A-Z]'),
CHECK (
clientContact LIKE '[6][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
OR clientContact LIKE'[0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
OR clientContact LIKE'[9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
)
);

CREATE TABLE CaseTable
(
referenceNum INT NOT NULL IDENTITY(100000, 1),
startDate DATETIME NOT NULL,
endDate DATETIME NULL,
caseDetail CHAR(255) NOT NULL,
caseType CHAR(255) NOT NULL DEFAULT'copyright and trademark',
clientID CHAR(5) NOT NULL,
CONSTRAINT CaseTablePK PRIMARY KEY(referenceNum),
CONSTRAINT ClientTableFK FOREIGN KEY(clientID)
REFERENCES ClientTable(clientID),
CONSTRAINT caseTypeValues CHECK(
caseType LIKE 'intellectual property enforcement'
OR caseType LIKE'copyright and trademark'
OR caseType LIKE'patent and industrial design'
OR caseType LIKE'trade secret'
OR caseType LIKE 'risk management'
OR caseType LIKE'litigation'
),
CONSTRAINT clientIDValues CHECK(clientID LIKE '[A-Z][0-9][0-9][0-9][A-Z]')
);

INSERT INTO CaseTable (startDate , caseDetail , caseType, clientID)
VALUES ('03/10/2012' , 'To commercialise for cooling system CS1F3', 'intellectual property enforcement', 'C123X')
First change is required because varchar without length specified is interpreted as being of length 1. http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/09/bad-habits-to-kick-declaring-varchar-without-length.aspx

The second change is required because without the single quotes, the value is interpreted as integers - i.e., 03 divided by 10 divided by 2012.
Go to Top of Page

Mohamed Faisal
Yak Posting Veteran

51 Posts

Posted - 2012-10-07 : 03:12:30
Hi All,

Thanks for your swift reply. It really help me.

Thanks.
Go to Top of Page
   

- Advertisement -