Tuesday, March 13, 2018

Query to find missing Indexes in DB

SELECT TOP 1000000
dm_mid.database_id AS DatabaseID,
dm_migs.avg_user_impact*(dm_migs.user_seeks+dm_migs.user_scans) Avg_Estimated_Impact,
dm_migs.last_user_seek AS Last_User_Seek,
OBJECT_NAME(dm_mid.OBJECT_ID,dm_mid.database_id) AS [TableName],
'CREATE INDEX [IX_' + OBJECT_NAME(dm_mid.OBJECT_ID,dm_mid.database_id) + '_'
+ REPLACE(REPLACE(REPLACE(ISNULL(dm_mid.equality_columns,''),', ','_'),'[',''),']','') 
+ CASE
WHEN dm_mid.equality_columns IS NOT NULL
AND dm_mid.inequality_columns IS NOT NULL THEN '_'
ELSE ''
END
+ REPLACE(REPLACE(REPLACE(ISNULL(dm_mid.inequality_columns,''),', ','_'),'[',''),']','')
+ ']'
+ ' ON ' + dm_mid.statement
+ ' (' + ISNULL (dm_mid.equality_columns,'')
+ CASE WHEN dm_mid.equality_columns IS NOT NULL AND dm_mid.inequality_columns 
IS NOT NULL THEN ',' ELSE
'' END
+ ISNULL (dm_mid.inequality_columns, '')
+ ')'
+ ISNULL (' INCLUDE (' + dm_mid.included_columns + ')', '') AS Create_Statement
FROM sys.dm_db_missing_index_groups dm_mig
INNER JOIN sys.dm_db_missing_index_group_stats dm_migs
ON dm_migs.group_handle = dm_mig.index_group_handle
INNER JOIN sys.dm_db_missing_index_details dm_mid
ON dm_mig.index_handle = dm_mid.index_handle
WHERE dm_mid.database_ID = DB_ID()
ORDER BY Avg_Estimated_Impact DESC

SQL Server table creation date query

This query will tell you when this table is created.


SELECT [name]        AS [TableName], [create_date] AS [CreatedDate] 
FROM   sys.tables 
WHERE  NAME = 'user' -- table name

Saturday, March 3, 2018

Run query on dataset

if you want to query on dataset you can simply do this no need to again hit database:

-- storing in Dataset
  Dim ds As New DataSet
    Dim ds_1 As New DataSet
   ds = getData()
   Session("ds_data") = ds


 -- Retrieving from Dataset
      Dim foundRows() As DataRow
       ds_1 = DirectCast(Session("ds_data"), DataSet) ' get data from session data
foundRows = ds.Tables(0).Select("table_ID = '" + hdn_ID.Value + "' ") ' table_ID is my column name
dim user_name as String =foundRows(0)("Customer Name")

-- Another Option
One more you can do you can make a new data-set and copy that filter data in to new data-set

Dim ds_Stats As DataSet
Dim ds_StatsAddress As DataSet
Dim ds_StatsPic As DataSet
ds_Stats = oApproveaddress.getData()
            ds_StatsPic = New DataSet()
            ds_StatsPic.Merge(ds_Stats.Tables(0).Select("id = '3' "))
            ds_StatsAddress = New DataSet()
            ds_StatsAddress.Merge(ds_Stats.Tables(0).Select("id = '4' "))


If ds_Stats.Tables(0).Select("documentid = '3' ").Length > 0 Then
                RepeaterPic.DataSource = ds_StatsPic.Tables(0)
                RepeaterPic.DataBind()
            Else
                RepeaterPic.DataSource = Nothing
                RepeaterPic.DataBind()
            End If


            If ds_Stats.Tables(0).Select("documentid = '4' ").Length > 0 Then
                RepeaterAddress.DataSource = ds_StatsAddress.Tables(0)
                RepeaterAddress.DataBind()
            Else
                RepeaterAddress.DataSource = Nothing
                RepeaterAddress.DataBind()
            End If

-- Sorting in Dataset

  Dim ds_Re As DataSet
        ds_Re= New DataSet()
        ds_Re.Merge(ds.Tables(0).Select("", "Date DESC"))
        If ds.Tables(0).Select("Is_Recommended = True ").Length > 0 Then
            rptReco.DataSource = ds_Recommended.Tables(0)
            rptReco.DataBind()

        End If

Refer

Thursday, March 1, 2018

Find Stored Procedure that Inserts Into a Specific Table

SELECT O.NAME
FROM   sysobjects O
       JOIN syscomments C
         ON O.id = C.id
WHERE  C.text LIKE '%insert into%user_log%'