Showing posts with label SQl. Show all posts
Showing posts with label SQl. Show all posts

Monday, 10 June 2013

Custom SQL exceptions (error messages) to end users

Protected Sub FormView_Paper_ItemInserted(sender As Object, e As FormViewInsertedEventArgs) Handles FormView_Paper.ItemInserted
       
    If Not IsNothing(e.Exception) Then
            Dim DoDisplayCustomError As Boolean = False

            Dim SQLError As System.Data.SqlClient.SqlException
            If e.Exception.Source = ".Net SqlClient Data Provider" Then
                SQLError = DirectCast(e.Exception, SqlException)
                If SQLError.ErrorCode = -2146232060 Then
                    DoDisplayCustomError = True
                    ' Develop ur message display function!
                    Me.CurrentMasterPage.DisplayError("Missing Mandatory fields", "Fill in all mandatory fields.")
                End If
            End If

            If Not DoDisplayCustomError Then
                Me.CurrentMasterPage.DisplayError(e.Exception)
            End If
            e.ExceptionHandled = True
            e.KeepInInsertMode = True
        Else
            Response.Redirect("OtherPage.aspx")
        End If
    End Sub

Sunday, 9 June 2013

Generating a sequence of numbers using SQL Select statement

SELECT (Tens.N + Units.N) NN
FROM
(
SELECT 0 N
UNION SELECT 1 N
UNION SELECT 2 N
UNION SELECT 3 N
UNION SELECT 4 N
UNION SELECT 5 N
UNION SELECT 6 N
UNION SELECT 7 N
UNION SELECT 8 N
UNION SELECT 9 N
)Units
CROSS JOIN
(
SELECT 0 N
UNION SELECT 10 N
--UNION SELECT 20 N
--UNION SELECT 30 N
--UNION SELECT 40 N
--UNION SELECT 50 N
--UNION SELECT 60 N
--UNION SELECT 70 N
--UNION SELECT 80 N
--UNION SELECT 90 N
)Tens

-----
NN
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

Thanks for:
http://sqlmag.com/t-sql/generating-sequences-t-sql

Wednesday, 5 June 2013

SQL: Get aggregates for multiple values from the same table

Table Doc

ID Version
--  --
1  1
1  2
1  3*
2  1
2  2*
3  1
3  2*
4  1
4  2
4  3
4  4*
5  1*

To get maximum versions for each document

SELECT D.ID, D.Version
FROM Doc D 
INNER JOIN (SELECT ID, MAX(Version) FROM Doc GROUP BY ID) DM ON D.ID = DM.ID



Thanks Dinos for the inspiration!



Monday, 15 April 2013

Print Date and Time Using TSQL

PRINT LEFT(REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR, GETDATE(), 121), '-','_'),':','_'),' ','_'), 16)

16 characters on the LEFT only to omit extra time details.

Wednesday, 11 July 2012

What does SQL data type: numeric(5, 2) mean?

 numeric(5, 2) and decimal(5,2)
mean the same thing 5 digits with 2 decimal places.
e.g. 123.45