Solved

Parameter truncation doesn't raise error in stored proc

Posted on 2004-03-31
1
501 Views
Last Modified: 2008-02-01
I can't get my stored proc to return an error when the value passed to the parameter is larger than the parameter's definition and larger than the field definition.  If I execute the SQL in the query window, an error is returned.

Table definition:
CREATE TABLE Employee(Eid int, Fname varchar(20), Lname varchar(20))

Populate 1 record:
  INSERT INTO Employee (EId, FName, LName) VALUES (1,'Ford','Perfect')

Update it from Query Analyzer
  UPDATE Employee  
  SET fname = 'FName', Lname = 'LNamefjdkfjkdfjkdjfkdjfk'

This raises the following error:
  Server: Msg 8152, Level 16, State 9, Line 1
  String or binary data would be truncated.
  The statement has been terminated.

Here is the update statement converted into a stored proc:

CREATE PROC updateEmployee3 (
      @FName varchar(10)
      , @LName varchar(10))
AS  
  UPDATE Employee  
  SET  fname = @Fname, lname = @Lname

RETURN @@ERROR  
 
When I call the stored proc in query window as follows, the value for FName is truncated but no error is raised.  Why?
  exec updateEmployee3 'ABCDEFGHIJKLM', 'LName'

I've tried this with other types of errors, e.g., assigning NULL to a field defined as NOT NULL, assigning a string to a int field, and in those cases the appropriate error is returned.
0
Comment
Question by:dancebert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 13

Accepted Solution

by:
danblake earned 100 total points
ID: 10725587
Having a look here:

CREATE PROC updateEmployee3 (
     @FName varchar(10)
     , @LName varchar(10))
AS  
  select @FName,@LName

  UPDATE Employee  
  SET  fname = @Fname, lname = @Lname

SELECT @@ERROR, @@ROWCOUNT

===============================

Running the following QA (Command):
updateEmployee3 'FName','LNamefjdkfjkdfjkdjfkdjfk'

You will notice that when the vars are passed to the SP, they are truncated on input.  They will not be trapped within the @@Error value.

Input vars into SPs are automatically truncated (without error) on input this is due to no checking when passing values to variables on variable length and if exceeding input length -- generate an error.  This is different than inputing into a table.

The following demonstrates this:
DECLARE @find varchar(3)
SET @find = 'Ring'
select @find

Output
---------------------------------------------------------------
Rin
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question