<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>MSSQL</title><link>http://blogs.x2line.com/al/category/8.aspx</link><description>Posts related to Microsoft SQL Server as a relational database management system, Transact-SQL, performance, hands-on development, 3rd party tools. Tips and tricks, configuration and t-sql best practices and performance considerations. Info related to editions like SQL Server Express Edition and future versions - like Katmai.</description><managingEditor>Anatoly Lubarsky</managingEditor><dc:language>en-US</dc:language><generator>Version 0.97.2006.1</generator><image><url>http://blogs.x2line.com/Images/x2line_logo_feed.jpg</url><title>x2line: Social Network Services, Weblog Hosting</title><link>http://blogs.x2line.com/</link></image><item><dc:creator>Anatoly Lubarsky</dc:creator><title>MSSQL 2008: Spatial Data Types - Geography and Geometry</title><link>http://blogs.x2line.com/al/archive/2008/05/15/3429.aspx</link><pubDate>Thu, 15 May 2008 02:28:00 GMT</pubDate><guid>http://blogs.x2line.com/al/archive/2008/05/15/3429.aspx</guid><wfw:comment>http://blogs.x2line.com/al/comments/3429.aspx</wfw:comment><comments>http://blogs.x2line.com/al/archive/2008/05/15/3429.aspx#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://blogs.x2line.com/al/comments/commentRss/3429.aspx</wfw:commentRss><trackback:ping>http://blogs.x2line.com/al/services/trackbacks/3429.aspx</trackback:ping><description>&lt;p&gt;In addition to &lt;a href="http://blogs.x2line.com/al/archive/2008/05/13/3426.aspx"&gt;new DATETIME data types&lt;/a&gt; introduced in MSSQL 2008 there are &lt;b&gt;geography&lt;/b&gt; and &lt;b&gt;geometry&lt;/b&gt; data types introduced - useful for storing and manipulating geodetic data. Geometry data type is used to store flat map data like points, lines, and shapes. Geography data type takes account of the curvature of the Earth in addition. Both provide the ability to perform spatial operations on geography instances. Geography and Geometry are likely to be supported in MSSQL 2008 Express versions.&lt;/p&gt;&lt;br /&gt;
&lt;p&gt;&lt;i&gt;"The ability to use the geography data type in a Microsoft SQL Server database enables you to extend business applications to handle geospatial data that describes simple and complex geographic elements on the surface of the Earth."&lt;/i&gt;&lt;/p&gt;&lt;br /&gt;
&lt;p&gt;Geography data type has functions which support parsing back from the known geo formats supported by the OGC (Open Geospatial Consortium) like text formats, binary representations, GML (Geography Markup Language - XML for geo data).&lt;/p&gt;&lt;br /&gt;
&lt;p&gt;Small example of spatial operations - let's assume we need to find the area (i.e. in square meters) of the stored polygons (SalesRegion of SalesPerson table). The query:&lt;/p&gt;&lt;br&gt;
&lt;pre&gt;SELECT SalesRegionName, SalesRegion.STArea()
  FROM SalesPerson
&lt;/pre&gt;&lt;br&gt;
&lt;p&gt;Other supported functions include &lt;b&gt;STIntersects()&lt;/b&gt;, &lt;b&gt;STUnion()&lt;/b&gt; for polygons and &lt;b&gt;STDistance()&lt;/b&gt; for points.&lt;/p&gt;&lt;br&gt;
&lt;p&gt;To boost performance one can &lt;a href="http://msdn.microsoft.com/en-us/library/bb934196%28SQL.100%29.aspx" target="_blank"&gt;CREATE SPATIAL INDEX&lt;/a&gt; for the data of GEOGRAPHY or GEOMETRY column.&lt;/p&gt;&lt;br&gt;
&lt;p&gt;Enjoy :)&lt;/p&gt;&lt;img src ="http://blogs.x2line.com/al/aggbug/3429.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Anatoly Lubarsky</dc:creator><title>MSSQL 2008: What is DATETIME2</title><link>http://blogs.x2line.com/al/archive/2008/05/13/3426.aspx</link><pubDate>Tue, 13 May 2008 19:13:00 GMT</pubDate><guid>http://blogs.x2line.com/al/archive/2008/05/13/3426.aspx</guid><wfw:comment>http://blogs.x2line.com/al/comments/3426.aspx</wfw:comment><comments>http://blogs.x2line.com/al/archive/2008/05/13/3426.aspx#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://blogs.x2line.com/al/comments/commentRss/3426.aspx</wfw:commentRss><trackback:ping>http://blogs.x2line.com/al/services/trackbacks/3426.aspx</trackback:ping><description>&lt;p&gt;MSSQL 2008 introduces several new DATETIME datatypes for T-SQL. It is obvious that TIME and DATE provide the ability to store separately DATE and TIME parts of DATETIME. It was not possible to do in previous versions of MSSQL, however it was possible to get either part of the DATETIME using DATETIME functions, for example a couple of years ago I posted about how to &lt;a href="http://blogs.x2line.com/al/archive/2006/02/17/1458.aspx"&gt;Get only Date from DateTime&lt;/a&gt;.&lt;/p&gt;&lt;br&gt;
&lt;ul&gt;
&lt;li&gt;TIME&lt;/li&gt;
&lt;li&gt;DATE&lt;/li&gt;
&lt;li&gt;&lt;b&gt;DATETIME2&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;DATETIMEOFFSET&lt;/li&gt;
&lt;/ul&gt;&lt;br&gt;
&lt;p&gt;What is DATETIME2 ? It is an improved version of DATETIME. It we compare between them:&lt;/p&gt;&lt;br&gt;
&lt;ul&gt;
&lt;li&gt;Storage: DATETIME2 takes 6-8 bytes, DATETIME takes 8 bytes.&lt;/li&gt;
&lt;li&gt;Accuracy: DATETIME2 (100 nanosec), DATETIME (333 nanosec).&lt;/li&gt;
&lt;li&gt;Range: DATETIME2 (0001/01/01 - 9999/12/31), DATETIME (1753/01/01 - 9999/12/31).&lt;/li&gt;
&lt;/ul&gt;&lt;br&gt;
&lt;p&gt;Sounds cool. Except strange name used for the datatype...(LOL) Seems like folks in Microsoft copied  VARCHAR2 convention from Oracle which I didn't understand either.&lt;/p&gt;&lt;br&gt;
&lt;p&gt;DATETIMEOFFSET datatype is like DATETIME2 plus in addition it can store TIMEZONE in hh:mm while the actual datetime is stored in UTC. This datatype takes up to 10 bytes in storage however.&lt;/p&gt;&lt;br&gt;
&lt;p&gt;EDIT: There are also new system functions introduced: SYSDATETIME(), SYSUTCDATETIME(). Both return system time AS DATETIME2. And also SYSDATETIMEOFFSET() which returns system time as DATETIMEOFFSET.&lt;/p&gt;&lt;br&gt;
&lt;p&gt;HTH&lt;/p&gt;&lt;img src ="http://blogs.x2line.com/al/aggbug/3426.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Anatoly Lubarsky</dc:creator><title>T-SQL: LIKE Operator and Wildcard Characters</title><link>http://blogs.x2line.com/al/archive/2008/05/08/3420.aspx</link><pubDate>Thu, 08 May 2008 02:37:00 GMT</pubDate><guid>http://blogs.x2line.com/al/archive/2008/05/08/3420.aspx</guid><wfw:comment>http://blogs.x2line.com/al/comments/3420.aspx</wfw:comment><comments>http://blogs.x2line.com/al/archive/2008/05/08/3420.aspx#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://blogs.x2line.com/al/comments/commentRss/3420.aspx</wfw:commentRss><trackback:ping>http://blogs.x2line.com/al/services/trackbacks/3420.aspx</trackback:ping><description>&lt;p&gt;An interesting issue I came into recently. Suppose you have a search procedure - which consists of some SELECT query which gets a query phrase as a parameter and searches through a table column using LIKE.&lt;/p&gt;&lt;br&gt;
&lt;p&gt;The most common pattern is to add % at the beginning and the end of the search parameter so the query looks like so:&lt;/p&gt;&lt;br&gt;
&lt;pre&gt;WHERE Col LIKE '%' + @param + '%'
&lt;/pre&gt;&lt;br&gt;
&lt;p&gt;Or you may perform concatenation before running the query as well :) However need to take into account the following. The search parameter which usually comes down to the procedure from the application user interface can contain T-SQL reserved wildcard characters such as =&amp;gt; % [ ] _ etc. Such input will cause the query to return wrong results.&lt;/p&gt;&lt;br&gt;
&lt;p&gt;Therefore it is useful to remember to filter out or replace these characters so MSSQL will consider these characters literals. For example if we have rectangular braces =&amp;gt; [], need to replace the opening brace =&amp;gt; [ with the sequence =&amp;gt; [[] before running the query.&lt;/p&gt;&lt;img src ="http://blogs.x2line.com/al/aggbug/3420.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Anatoly Lubarsky</dc:creator><title>MSSQL: Search and Replace in a TEXT(NTEXT) column</title><link>http://blogs.x2line.com/al/archive/2008/05/03/3417.aspx</link><pubDate>Sat, 03 May 2008 20:16:00 GMT</pubDate><guid>http://blogs.x2line.com/al/archive/2008/05/03/3417.aspx</guid><wfw:comment>http://blogs.x2line.com/al/comments/3417.aspx</wfw:comment><comments>http://blogs.x2line.com/al/archive/2008/05/03/3417.aspx#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://blogs.x2line.com/al/comments/commentRss/3417.aspx</wfw:commentRss><trackback:ping>http://blogs.x2line.com/al/services/trackbacks/3417.aspx</trackback:ping><description>&lt;p&gt;It's been a while since I've posted anything SQL related. So... Sometimes we need to search and replace a text value in the entire table. The column in question is of TEXT or NTEXT datatype. T-SQL REPLACE function does not work with TEXT/NTEXT datatype.&lt;/p&gt;&lt;br /&gt;
&lt;p&gt;Instead we have to use several other functions:&lt;/p&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;UPDATETEXT&lt;/b&gt; - Updates an existing text, ntext, or image field.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;TEXTPTR&lt;/b&gt; - Returns the text-pointer value that corresponds to a text, ntext, or image column in varbinary format. The retrieved text pointer value can be used in READTEXT, WRITETEXT, and UPDATETEXT statements.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;PATINDEX&lt;/b&gt; - Returns the starting position of the first occurrence of a pattern in a specified expression, or zeros if the pattern is not found, on all valid text and character data types.&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
&lt;p&gt;A while ago I wrote a small utility procedure which uses above functions to implement Search and Replace functionality.&lt;/p&gt;&lt;br /&gt;
&lt;pre&gt;
ALTER PROC dbo.SearchAndReplace 
(
     @FindString    NVARCHAR(100)
    ,@ReplaceString NVARCHAR(100)
)
AS
BEGIN
    SET NOCOUNT ON

    DECLARE @TextPointer VARBINARY(16) 
    DECLARE @DeleteLength INT 
    DECLARE @OffSet INT 

    SELECT @TextPointer = TEXTPTR([MY_TEXT_COLUMN])
      FROM [MY_TABLE]

    SET @DeleteLength = LEN(@FindString) 
    SET @OffSet = 0
    SET @FindString = '%' + @FindString + '%'

    WHILE (SELECT COUNT(*)
             FROM [MY_TABLE]
            WHERE PATINDEX(@FindString, [MY_TEXT_COLUMN]) &lt;&gt; 0) &gt; 0
    BEGIN 
        SELECT @OffSet = PATINDEX(@FindString, [MY_TEXT_COLUMN]) - 1
          FROM [MY_TABLE]
         WHERE PATINDEX(@FindString, [MY_TEXT_COLUMN]) &lt;&gt; 0

        UPDATETEXT [MY_TABLE].[MY_TEXT_COLUMN]
            @TextPointer
            @OffSet
            @DeleteLength
            @ReplaceString
    END

    SET NOCOUNT OFF
END
&lt;/pre&gt;&lt;br /&gt;
&lt;p&gt;Hope this helps...&lt;/p&gt;&lt;img src ="http://blogs.x2line.com/al/aggbug/3417.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Anatoly Lubarsky</dc:creator><title>T-SQL: Get Age from Date of Birth</title><link>http://blogs.x2line.com/al/archive/2007/08/10/3235.aspx</link><pubDate>Fri, 10 Aug 2007 18:15:00 GMT</pubDate><guid>http://blogs.x2line.com/al/archive/2007/08/10/3235.aspx</guid><wfw:comment>http://blogs.x2line.com/al/comments/3235.aspx</wfw:comment><comments>http://blogs.x2line.com/al/archive/2007/08/10/3235.aspx#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://blogs.x2line.com/al/comments/commentRss/3235.aspx</wfw:commentRss><trackback:ping>http://blogs.x2line.com/al/services/trackbacks/3235.aspx</trackback:ping><description>&lt;p&gt;I just like tiny SQL tricks. Once I posted how to &lt;a href="http://blogs.x2line.com/al/archive/2006/02/17/1458.aspx" target="_blank"&gt;Get only Date from DateTime&lt;/a&gt; without performing any casting or conversion.&lt;/p&gt;&lt;br&gt;
&lt;p&gt;So...&lt;/p&gt;&lt;br /&gt;
&lt;p&gt;Sometimes it is useful for social oriented networks to calculate on the fly or store user age given his/her date of birth. Below is a small snippet in T-SQL:&lt;/p&gt;
&lt;pre&gt;SELECT FLOOR(DATEDIFF(DAY, @BirthDate, @TargetDate) / 365.25)&lt;/pre&gt;&lt;img src ="http://blogs.x2line.com/al/aggbug/3235.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Anatoly Lubarsky</dc:creator><title>MSSQL 2005 Tip: Using TOP with Subquery</title><link>http://blogs.x2line.com/al/archive/2007/08/05/3229.aspx</link><pubDate>Sun, 05 Aug 2007 22:53:00 GMT</pubDate><guid>http://blogs.x2line.com/al/archive/2007/08/05/3229.aspx</guid><wfw:comment>http://blogs.x2line.com/al/comments/3229.aspx</wfw:comment><comments>http://blogs.x2line.com/al/archive/2007/08/05/3229.aspx#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://blogs.x2line.com/al/comments/commentRss/3229.aspx</wfw:commentRss><trackback:ping>http://blogs.x2line.com/al/services/trackbacks/3229.aspx</trackback:ping><description>&lt;p&gt;SQL Server 2005 introduced new TOP behavior - like TOP function supports variables and expressions.&lt;/p&gt;&lt;br /&gt;
&lt;p&gt;As a result it is possible to use a subquery as input to TOP. Subquery should return scalar. Like so:&lt;/p&gt;&lt;br /&gt;
&lt;pre&gt;
SELECT TOP(SELECT COUNT(*)
             FROM Table2) Col1, Col2
  FROM Table1
 ORDER BY Col1
&lt;/pre&gt;&lt;br /&gt;
&lt;p&gt;Which seems kinda strange for people with MSSQL 2000 experience. In addition it is possible to use TOP with INSERT, UPDATE and DELETE like:&lt;/p&gt;&lt;br /&gt;
&lt;pre&gt;
DELETE TOP(1000)
  FROM Table1
 WHERE Id &lt; 10000
&lt;/pre&gt;&lt;br /&gt;
&lt;p&gt;Just wanted to share these small tips :)&lt;/p&gt;&lt;img src ="http://blogs.x2line.com/al/aggbug/3229.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Anatoly Lubarsky</dc:creator><title>T-SQL Query Processing Order</title><link>http://blogs.x2line.com/al/archive/2007/06/30/3187.aspx</link><pubDate>Sat, 30 Jun 2007 21:29:00 GMT</pubDate><guid>http://blogs.x2line.com/al/archive/2007/06/30/3187.aspx</guid><wfw:comment>http://blogs.x2line.com/al/comments/3187.aspx</wfw:comment><comments>http://blogs.x2line.com/al/archive/2007/06/30/3187.aspx#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://blogs.x2line.com/al/comments/commentRss/3187.aspx</wfw:commentRss><trackback:ping>http://blogs.x2line.com/al/services/trackbacks/3187.aspx</trackback:ping><description>&lt;p&gt;One of the most important aspects in optimizing MSSQL queries is understanding the steps and order the query is processed since the query is processed in the order that is different than the code written. IMHO, this is the main difficulty in understanding complicated queries.&lt;/p&gt;&lt;br /&gt;
&lt;p&gt;According to ANSI standard the query should be written in the following way:&lt;/p&gt;&lt;br /&gt;
&lt;pre&gt;
SELECT [...]
  FROM [table1]
  JOIN [table2]
    ON [condition]
 WHERE [...]
 GROUP BY [...]
HAVING [...]
 ORDER BY [...]
&lt;/pre&gt;&lt;br&gt;
&lt;p&gt;However the query will be processed by MSSQL in the following order like so:&lt;/p&gt;&lt;br /&gt;
&lt;pre&gt;
  FROM [table1]
    ON [condition]
  JOIN [table2]
 WHERE [...]
 GROUP BY [...]
HAVING [...]
SELECT [...]
 ORDER BY [...]
&lt;/pre&gt;&lt;br /&gt;
&lt;p&gt;Please note, that &lt;i&gt;ORDER BY&lt;/i&gt; is the last to be processed, only &lt;i&gt;TOP&lt;/i&gt; clause would be processed after &lt;i&gt;ORDER BY&lt;/i&gt;. &lt;i&gt;DISTINCT&lt;/i&gt; will be processed after &lt;i&gt;SELECT&lt;/i&gt; and before &lt;i&gt;ORDER BY&lt;/i&gt;.&lt;/p&gt;&lt;img src ="http://blogs.x2line.com/al/aggbug/3187.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Anatoly Lubarsky</dc:creator><title>How to Insert DBCC Output into MSSQL Table</title><link>http://blogs.x2line.com/al/archive/2007/06/23/3173.aspx</link><pubDate>Sat, 23 Jun 2007 02:54:00 GMT</pubDate><guid>http://blogs.x2line.com/al/archive/2007/06/23/3173.aspx</guid><wfw:comment>http://blogs.x2line.com/al/comments/3173.aspx</wfw:comment><comments>http://blogs.x2line.com/al/archive/2007/06/23/3173.aspx#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://blogs.x2line.com/al/comments/commentRss/3173.aspx</wfw:commentRss><trackback:ping>http://blogs.x2line.com/al/services/trackbacks/3173.aspx</trackback:ping><description>&lt;p&gt;Sometimes it is useful to save DBCC maintenance routines into MSSQL server table, so the info will be logged. Some DBCC commands support this directly via the ...&lt;/p&gt;&lt;br /&gt;
&lt;pre&gt;INSERT INTO #table EXEC('DBCC ...')&lt;/pre&gt;&lt;br /&gt;
&lt;p&gt;... type format (like for example &lt;b&gt;DBCC USEROPTIONS&lt;/b&gt;), others don't (like &lt;b&gt;DBCC SHOWCONTIG&lt;/b&gt;). Fortunately there is some trick that will help :)&lt;/p&gt;&lt;br /&gt;
&lt;p&gt;DBCC can be used with &lt;i&gt;WITH TABLERESULTS&lt;/i&gt; hint that outputs the output in the table rowset format. So - here is a small code sample that demonstrates the technique:&lt;/p&gt;&lt;br /&gt;
&lt;pre&gt;
CREATE TABLE #x
(
    f1 VARCHAR(50)
   ,f2 VARCHAR(50)
   ,indexname VARCHAR(50)
   ,indexid VARCHAR(50)
   ,f5 VARCHAR(50)
   ,f6 VARCHAR(50)
   ,f7 VARCHAR(50)
   ,f8 VARCHAR(50)
   ,f9 VARCHAR(50)
   ,f10 VARCHAR(50)
   ,f11 VARCHAR(50)
   ,f12 VARCHAR(50)
   ,f13 VARCHAR(50)
   ,f14 VARCHAR(50)
   ,f15 VARCHAR(50)
   ,f16 VARCHAR(50)
   ,bestcount VARCHAR(50)
   ,actualcount VARCHAR(50)
   ,logicalfragmentation VARCHAR(50)
   ,f20 varchar(50))

INSERT #x 
  EXEC('DBCC SHOWCONTIG(MyTable) WITH ALL_INDEXES, TABLERESULTS')

SELECT * 
  FROM #x

DROP TABLE #x
&lt;/pre&gt;&lt;br /&gt;
&lt;p&gt;Enjoy :)&lt;/p&gt;&lt;img src ="http://blogs.x2line.com/al/aggbug/3173.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Anatoly Lubarsky</dc:creator><title>MSSQL: sp_xml_preparedocument is not supported in fibers mode</title><link>http://blogs.x2line.com/al/archive/2007/01/12/2779.aspx</link><pubDate>Fri, 12 Jan 2007 17:25:00 GMT</pubDate><guid>http://blogs.x2line.com/al/archive/2007/01/12/2779.aspx</guid><wfw:comment>http://blogs.x2line.com/al/comments/2779.aspx</wfw:comment><comments>http://blogs.x2line.com/al/archive/2007/01/12/2779.aspx#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://blogs.x2line.com/al/comments/commentRss/2779.aspx</wfw:commentRss><trackback:ping>http://blogs.x2line.com/al/services/trackbacks/2779.aspx</trackback:ping><description>&lt;p&gt;Did you know, that XML procedures like &lt;b&gt;sp_xml_preparedocument&lt;/b&gt; or &lt;b&gt;sp_xml_removedocument&lt;/b&gt; are not supported in MSSQL when the server is configured as "Use Windows NT fibers" (under server » processor) ? I didn't. Consider the following T-SQL snippet, which extracts data from XML document:&lt;/p&gt;&lt;br&gt; &lt;pre&gt;DECLARE @xml  VARCHAR(1000)
DECLARE @idoc INT
 
SET @xml = '&amp;lt; ROOT&amp;gt;
                &amp;lt; User Id="1" /&amp;gt;
                &amp;lt; User Id="2" /&amp;gt;
                &amp;lt; User Id="3" /&amp;gt;
            &amp;lt; /ROOT&amp;gt;'

EXEC sp_xml_preparedocument @idoc OUTPUT, @xml
 
SELECT UserId 
  FROM OPENXML (@idoc, '/ROOT/User', 2)  
  WITH (UserId INT '@Id')

EXEC sp_xml_removedocument @idoc
&lt;/pre&gt;&lt;br&gt;
&lt;p&gt;If the processor is configured in "NT fibers mode", you get run-time exception:&lt;/p&gt;&lt;br&gt;
&lt;p&gt;&lt;i&gt;"XML stored procedures are not supported in fibers mode"&lt;/i&gt;&lt;/p&gt;&lt;br&gt;
&lt;p&gt;BOL does not tell anything about the issue (at least under &lt;i&gt;sp_xml_preparedocument&lt;/i&gt; it doesn't ). To fix this problem, go to the processor tab, and uncheck the box "Use Windows NT fibers" (requires restart). If it is your server - everything is fine.&lt;/p&gt;&lt;img src ="http://blogs.x2line.com/al/aggbug/2779.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Anatoly Lubarsky</dc:creator><title>MSSQL: Got arithmetic overflow for INT ?</title><link>http://blogs.x2line.com/al/archive/2006/12/07/2602.aspx</link><pubDate>Thu, 07 Dec 2006 23:21:00 GMT</pubDate><guid>http://blogs.x2line.com/al/archive/2006/12/07/2602.aspx</guid><wfw:comment>http://blogs.x2line.com/al/comments/2602.aspx</wfw:comment><comments>http://blogs.x2line.com/al/archive/2006/12/07/2602.aspx#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://blogs.x2line.com/al/comments/commentRss/2602.aspx</wfw:commentRss><trackback:ping>http://blogs.x2line.com/al/services/trackbacks/2602.aspx</trackback:ping><description>&lt;p&gt;I had a nasty bug today in SQL with INT overflow:&lt;/p&gt;&lt;br /&gt; 
&lt;pre&gt;SELECT SUM(COL)
  FROM MyTable
&lt;/pre&gt;&lt;br&gt;
&lt;p&gt;When the sum gets bigger than 2,147,483,647 in sql runtime it overflows.&lt;/p&gt;&lt;br&gt;
&lt;p&gt;&lt;i&gt;"Arithmetic overflow error converting expression to data type int."&lt;/i&gt;&lt;/p&gt;&lt;br&gt;
&lt;p&gt;
The bad thing is that I knew it. The SUM was also inside more complicated expression with assign to BIGINT parameter :(&lt;/p&gt;&lt;br&gt;
&lt;p&gt;BTW, &lt;b&gt;SUM&lt;/b&gt; in T-SQL returns only INT, DECIMAL, MONEY, FLOAT according to BOL. So the fix should be like so:&lt;/p&gt;&lt;br&gt;
&lt;pre&gt;SELECT SUM(CAST(COL AS DECIMAL))
  FROM MyTable
&lt;/pre&gt;
&lt;p&gt;Which looks wierd at a 1st glance, but is correct. Another point: SUM can also return BIGINT in MSSQL 2005 like so:&lt;/p&gt;&lt;br&gt;
&lt;pre&gt;SELECT SUM(CAST(COL AS BIGINT))
  FROM MyTable
&lt;/pre&gt;
&lt;p&gt;But it is not documented...&lt;/p&gt;&lt;img src ="http://blogs.x2line.com/al/aggbug/2602.aspx" width = "1" height = "1" /&gt;</description></item></channel></rss>