Wednesday, March 7, 2012
Error message: String or binary data truncated
I encountered that error message while testing my application using
MSDE. It appears when I issue this type of statement:
INSERT INT atable (Col1, Col2, ...)
VALUES (Val1, Val2,...)
The string columns are of type 'char'.
The strange is that the message does not appear always. In one case I
had error in two consecutive executions of the query. After that, in the
3rd execution *with exactly the same data!* all went OK!? I cannot
understand where lies the problem.
The same thing does not happen at all when testing with MS SQL Server 2000.
Please, can somebody give me a hint about what probably goes on.
hi Lazar,
Lazar Videnov wrote:
> Hi, all!
> I encountered that error message while testing my application using
> MSDE. It appears when I issue this type of statement:
> INSERT INT atable (Col1, Col2, ...)
> VALUES (Val1, Val2,...)
> The string columns are of type 'char'.
> The strange is that the message does not appear always. In one case I
> had error in two consecutive executions of the query. After that, in
> the 3rd execution *with exactly the same data!* all went OK!? I cannot
> understand where lies the problem.
> The same thing does not happen at all when testing with MS SQL Server
> 2000.
> Please, can somebody give me a hint about what probably goes on.
are you sure this is not depending on the provided values exceeding the
maximum storage of the columns?
DECLARE @.t TABLE (
c char(5)
)
INSERT INTO @.t VALUES ( 'abcdefghi' )
--<--
Server: Msg 8152, Level 16, State 9, Line 4
String or binary data would be truncated.
The statement has been terminated.
Andrea Montanari (Microsoft MVP - SQL Server)
http://www.asql.biz/DbaMgr.shtmhttp://italy.mvps.org
DbaMgr2k ver 0.12.0 - DbaMgr ver 0.58.0
(my vb6+sql-dmo little try to provide MS MSDE 1.0 and MSDE 2000 a visual
interface)
-- remove DMO to reply
|||Hi,
Actually, I don't do any checks for exceeding the storage space in the
string columns. I thought about that, but the strange is that this error
happens only sometimes. That is why I'm curious. And, as I wrote, it
works all fine in MS SQL Server 2000. I thought that the server just
trims the data and everything is OK.
Now, I'll make sure that I pass strings with length less than the
definition of the column, and I'll test again.
thanks
Sunday, February 19, 2012
Error Message conversion failed when converting the nvarchar value to data type int?
Hi,
I can't seem to fix the following error in my stored procedure.
Error Message: Conversion failed when converting the nvarchar value '1007-001' to data type int.
The line of code in my stored procedure that seems to be the problem is the following:
CASE WHEN [Order Details].[Job No] IS NULL THEN [Orders].[Order No] ELSE [Order Details].[Job No] END
Order No has a data type of INT and Job No has a data type NVARCHAR(8). In the above case statement i'm not trying to convert anything but just display a column depending on the out come of the case statement. If anyone knows how to get around this error you help would be very welcome.
hi,
Aston35 wrote:
Hi,
I can't seem to fix the following error in my stored procedure.
Error Message: Conversion failed when converting the nvarchar value '1007-001' to data type int.
The line of code in my stored procedure that seems to be the problem is the following:
CASE WHEN [Order Details].[Job No] IS NULL THEN [Orders].[Order No] ELSE [Order Details].[Job No] END
Order No has a data type of INT and Job No has a data type NVARCHAR(8). In the above case statement i'm not trying to convert anything but just display a column depending on the out come of the case statement. If anyone knows how to get around this error you help would be very welcome.
as you pointed out, the 2 columns are of a different data type, so you have to deal accordingly.. as you can not convert 'abc' to an integer data type, you have to perform the opposite, convert the int to a varchar...
SET NOCOUNT ON;DECLARE @.b int;
SET @.b = 0;
PRINT 'works';
SELECT CASE WHEN @.b = 0 THEN 1 ELSE 'a' END AS [Result];
SET @.b = 1;
PRINT 'does not work';
SELECT CASE WHEN @.b = 0 THEN 1 ELSE 'a' END AS [Result];
GO
DECLARE @.b int;
SET @.b = 1;
PRINT 'works';
SELECT CASE WHEN @.b = 0 THEN CONVERT(varchar, 1) ELSE 'a' END AS [Result];
--<
works
Result
--
1
does not work
Result
--
Msg 245, Level 16, State 1, Line 9
Conversion failed when converting the varchar value 'a' to data type int.
works
Result
a
regards|||Thanks Andrea, will give your suggestion ago and see how i get on.
Friday, February 17, 2012
Error Message 443
Hi,
I'm trying to create the following function:
CREATE FUNCTION dbo.get_id(@.name NVARCHAR(50)) RETURNS INT AS
BEGIN
DECLARE @.id INT
SET @.id = (SELECT id FROM dbo.language WHERE name = @.name)
IF @.@.ROWCOUNT = 0
RAISERROR('Specified item does not exist', 16, 1)
RETURN @.id
END
GO
And I get the following error message:
Invalid use of side-effecting or time-dependent operator in 'RAISERROR' within a function.
I can't find any references to the use of RAISERROR within functions or stored procedures, so I have no idea why this is not working.
What I'm trying to do is check for the existence of the row inside the function and not outside, on all the places I need to call this function. I need to have an exception in my code if this happens, that's why I thought RAISERROR is what I needed...
Any help would be appreciated!
Thanks,
Florin
Thanks, I eventually figured out that I can't use RAISERROR within a function, so I changed it to a stored procedure. My problem now is that RAISERROR doesn't stop the execution of my stored procedure, but continues with the next statement. Is there a way to achieve the same kind of functionality as with primary key or constraint violations? I'm testing some conditions inside my stored procedure and I want to stop the execution under certain circumstances and get an exception in my .NET calling code. Is that at all possible with RAISERROR?
I noticed that when an error is produced by the SQL Server engine, besides the red error message (which I can also create using RAISERROR) there is another message saying "The statement has been terminated". How can I achieve the same? :-)
Thanks, Florin
|||While you can't raise an error in a udf. . .this function:
CREATE FUNCTION dbo.get_id(@.name NVARCHAR(50)) RETURNS INT AS
BEGIN
DECLARE @.id INT
SELECT @.id = id FROM dbo.language WHERE name = @.name
RETURN @.id
END
GO
will return a null if name is not found.
Now, I contend, trying to select an id for a name that doesn't exist shouldn't be an error. . . It should return a null. Trying to use the null in a place that requires non-null will raise the error. . . and that might make perfect logical sense.
Just a thought.
On the other hand, if you want to use a procedure, look up @.@.ERROR in the help file.|||
Well, in the application logic, the foreign key can be null, which means that record has no references, but in this particular case I need to throw an error because the calling client has given a foreign key value that does not exist. Null is a valid case, non-existing referenced record is not. That's why I need the error. I want to do as much as I can on the database in one stored procedure call, to avoid multiple database calls/roundtrips...
Florin
|||huh? I think you misunderstood. . .
run this script. . .
-- BEGIN SCRIPT =========================
create table Song(ID int not null identity(1,1) primary key, name varchar(255))
go
create table Band(ID int not null identity(1,1) primary key, name varchar(255))
go
create table Record(BandID int not null references Band(ID), SongID int not null references Song(ID) )
go
CREATE FUNCTION dbo.getSongID(@.name NVARCHAR(50)) RETURNS INT AS
BEGIN
DECLARE @.id INT
SELECT @.id = id FROM dbo.Song WHERE name = @.name
RETURN @.id
END
go
CREATE FUNCTION dbo.getBandID(@.name NVARCHAR(50)) RETURNS INT AS
BEGIN
DECLARE @.id INT
SELECT @.id = id FROM dbo.Band WHERE name = @.name
RETURN @.id
END
go
Insert into Song(Name) values('Love Will Tear Us Apart')
Insert into Band(Name) values('Joy Division')
-- This works
Insert into Record values(dbo.getBandID('Joy Division'), dbo.getSongID('Love Will Tear Us Apart'))
-- This doesn't and the error is on the database
Insert into Record values(dbo.getBandID('Captain and Tennille'), dbo.getSongID('Love Will Tear Us Apart'))
-- This doesn't and the error is on the database
Insert into Record values(dbo.getBandID('Joy Division'), dbo.getSongID('Love Will Keep Us Together'))
-- END SCRIPT =========================