SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Error
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Mohamed Faisal
Starting Member

44 Posts

Posted - 10/03/2012 :  12:51:12  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 10/03/2012 :  12:59:16  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 10/03/2012 :  13:00:19  Show Profile  Reply with Quote
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
Starting Member

44 Posts

Posted - 10/07/2012 :  03:12:30  Show Profile  Reply with Quote
Hi All,

Thanks for your swift reply. It really help me.

Thanks.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000