Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SQL Server Stored Procedures and SET options Expand / Collapse
Author
Message
Posted Wednesday, August 28, 2013 10:48 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, September 12, 2014 2:30 AM
Points: 1,418, Visits: 1,838
Comments posted to this topic are about the item SQL Server Stored Procedures and SET options

Thanks & Regards,
Nakul Vachhrajani.
http://nakulvachhrajani.com
Be courteous. Drive responsibly.

Follow me on
Twitter: @sqltwins
Google Plus: +Nakul
Post #1489518
Posted Thursday, August 29, 2013 12:32 AM


SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Yesterday @ 11:20 AM
Points: 41, Visits: 370
Good article, Nakul, with thorough tests. Thanks for posting.

One small quibble: ansi_warnings is actually respected inside stored procedures. The behavior you demonstrated occurs because SQL Server doesn't raise errors (regardless of the ansi_warnings setting) when you assign a too-long value to a char/varchar variable - this is true in both ad-hoc queries and stored procedures - and it handles assigning the values of stored procedure & UDF parameters the same way as assigning variables.

Here's a quick test to see that even with ansi_warnings on, assigning a too-long string to a varchar variable produces no error in ad-hoc SQL:

set ansi_warnings on

-- this won't generate an error
declare @Small varchar(3)
set @Small = 'Long Text'
select @Small -- selects 'Lon'

-- this will generate a "String or binary data would be truncated" error
declare @Test table (
Small varchar(3)
)

insert into @Test (
Small
)
values (
'Long Text'
)

So when you declare your stored procedure with a parameter that's the same length as a corresponding column in the table you'll insert/update in, the too-long value gets silently truncated to the length allowed by the parameter (the same as assigning a variable), and then is short enough to fit into the column without an error.

You're right that you can perform the length checks in the client application code, and in fact that's pretty much necessary if the application wants to present the user with friendly validation messages instead of SQL errors, but you can still use ansi_warnings as a fallback when you're using stored procedures: just declare the sproc's parameters to be one character longer than is allowed in the corresponding columns. Here's an example:

set ansi_warnings on
GO

create procedure dbo.TruncationTest (
-- the parameter is one character longer than the "Small" column
@String varchar(4)
)
as begin
declare @Test table (
Small varchar(3)
)

insert into @Test (
Small
)
values (
@String
)
end
GO

-- this will generate a "String or binary data would be truncated" error
exec dbo.TruncationTest 'Long Text'

Hope you find that helpful. :)
Post #1489545
Posted Thursday, August 29, 2013 7:09 AM
Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Yesterday @ 5:45 AM
Points: 665, Visits: 461
Thank you for this post! I found a small problem in the first block of code. PRINT 'ANSI_WARNINGS is OFF'
is not executed because SELECT @userOptions = @@OPTIONS; is executed before ANSI_WARNINGS is reset.

Post #1489695
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse