Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Parameter truncation doesn't raise error in stored proc

Posted on 2004-03-31
1
Medium Priority
?
505 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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

877 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