Objects in this mirror are closer to Microsoft Technologies. DNM objective is to help users on Microsoft technologies by providing Articles, snippets, Interview Questions.

07 December 2011

IDENT_CURRENT function in SQL Server



IDENT_CURRENT function will return the last identity value generated by table of any session/scope.

Syntax:

IDENT_CURRENT(‘Table Name’)
Parameters - Table Name is varchar.
Retrun Type – Returns the last identity value of input table. Return data type is Numeric(38,0).

Example:

Below we created a sample table with identity column. So while inserting data we no need to insert id value, it will be taken care by identity column.

CREATE TABLE tblSampleWithIdentity
(
ID INT PRIMARY KEY IDENTITY(1,1),
Name VARCHAR(10)
)

INSERT INTO tblSampleWithIdentity VALUES('A')
INSERT INTO tblSampleWithIdentity VALUES('B')
INSERT INTO tblSampleWithIdentity VALUES('C')
INSERT INTO tblSampleWithIdentity VALUES('D')

Output:

SELECT IDENT_CURRENT('tblSampleWithIdentity') -- output is 4
INSERT INTO tblSampleWithIdentity VALUES('E')
SELECT IDENT_CURRENT('tblSampleWithIdentity') -- output is 5

Note:If your identity starts with 0 then IDENT_CURRENT returns 0.

CREATE TABLE tblIdentFrom0(ID INT IDENTITY(0,1))
SELECT IDENT_CURRENT('tblIdentFrom0')  -- 0

When IDENT_CURRENT returns NULL:
  • If the table does not exists in the database. Eg: Table name ‘myTable’ is not exists in your current database then if you try to execute IDENT_CURRENT(‘myTable’) then it returns NULL value.
  •  If table does not have identity column. Then IDENT_CURRENT will return null value.Below script shows a table with out identity column so if you try to execute IDENT_CURRENT it returns null.
Create table tblSampleWithOutIdentity
(
ID int primary key,
Name varchar(10)
)
insert into tblSampleWithOutIdentity values(1,'A')
insert into tblSampleWithOutIdentity values(2,'B')

select IDENT_CURRENT('tblSampleWithOutIdentity') -- Output is Null
  • ·If user doesn’t have permission to access the meta data then IDENT_CURRENT returns NULL value.
Caution: Value generated from IDENT_CURRENT may differ from IDENT_CURRENT + IDENTITY_SEED because of insertions performed by some other sessions.

04 December 2011

Does UNIQUE KEY supports more than one NULL Value

Does UNIQUE KEY supports more than one NULL Value?

No. In SQL server we can insert only one NULL value to column which has UNIQUE KEY constraint.

 Example:

CREATE TABLE SamepleUnique
(
Id INT PRIMARY KEY,
Name varchar(20) UNIQUE
)
INSERT INTO SamepleUnique VALUES(1,'Ram')
INSERT INTO SamepleUnique VALUES(2,'Krish')
INSERT INTO SamepleUnique VALUES(3,Null)
INSERT INTO SamepleUnique VALUES(4,Null) --Error as Violation of UNIQUE KEY constraint 'UQ__SamepleU__737584F603317E3D'. Cannot insert duplicate key in object 'dbo.SamepleUnique'.
--The statement has been terminated.
Output:

03 December 2011

Setting Tab Order for WPF Controls


Tab order of controls in form defines the sequence of focus change on controls when user press tab key.
A well defined tab order form is very easy to user to fill the forms. So always define the tab order for your forms. By default tab order assigned is how the controls are defined in the XAML. Tab order will start from 0.

So manually in WPF we can set tab order for controls using TabIndex or
KeyboardNavigation.TabIndex.

Syntax:
<Control TabIndex="Value"> </Control>

If the form is defined with TabIndex for all the controls the execution of tab order will be lower index value to higher index value.if the two controls as same tabiTabIndex value then tab order will be how the controls are defined in XAML.
We can restrict a control not to focus when tab key is pressed using IsTabStop="False" or KeyboardNavigation.IsTabStop="False" or Focusable="False"

 Example:

  <Grid>      
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="150"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <TextBox Height="23" Grid.Column="0" Grid.Row="0" TabIndex="0">TabOrder0</TextBox>
        <ComboBox Grid.Column="0" Grid.Row="1" KeyboardNavigation.TabIndex="2">
            <ComboBoxItem IsSelected="True">TabOrder2</ComboBoxItem>
        </ComboBox>
        <RadioButton Content="IsTabStopFalse" Grid.Column="0" Grid.Row="2" IsTabStop="False"   />
        <TextBox Height="23" Grid.Column="1" Grid.Row="0" TabIndex="1">TabOrder1</TextBox>
        <TextBox Height="23" Grid.Column="1" Grid.Row="1" KeyboardNavigation.TabIndex="6">TabOrder6</TextBox>
        <RadioButton Content="IsTabStopFalse" Grid.Column="1" Grid.Row="2" KeyboardNavigation.IsTabStop="False"   />
        <CheckBox Grid.Column="0" Grid.Row="3" Content="No Tab Order"/>
        <CheckBox Grid.Column="1" Grid.Row="3" Content="No Focusable" Focusable="False"/>
        <GroupBox Grid.ColumnSpan="2" Header="MyGroupBox" Height="100" HorizontalAlignment="Left" Margin="10,10,0,0" Name="groupBox1" VerticalAlignment="Top" Width="Auto" Grid.Row="4" TabIndex="3">

            <StackPanel>
                <TextBox Height="23" Grid.Column="1" Grid.Row="0" TabIndex="4">TabOrder4</TextBox>
                <TextBox Height="23" Grid.Column="1" Grid.Row="1" KeyboardNavigation.TabIndex="5">TabOrder5</TextBox>
            </StackPanel>
        </GroupBox>
    </Grid>

Output:
















Focust Execution order is TabOrder0, TabOrder1, TabOrder2, TabOrder3, TabOrder4, TabOrder5, TabOrder6, No Tab Order.
Checkboxes will not get focus.