Solved

Parameter truncation doesn't raise error in stored proc

Posted on 2004-03-31
1
495 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
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now