Tag Archive : Database

How to Save Column Names with Results : SQL Server 2005

2
Digg me

Whether you are copying query output from SQL Server Management Studio or Saving result as .csv file, if you need to save the column name or column header along with result.. here is simple option you have to enable.

Goto Query > Query Options or Press CTRL + Shift + O

Goto Option Grid Under Results

Check the box which says “Include column headers when copying or saving the results”

Click OK.

Run the query and save result as .csv or copy paste to excel with column headers. :)

Asp.Net Filter Datatable by Column

0
Digg me

Filter Datatable by Rows

If you need to get subset of a big dataset into datatable Rowwise …

[sourcecode language='vb']

Dim dtEmp as DataTable
Dim dsEmployee as New DataSet

dsEmployee = someobject.getdata()  ‘dataset can be populated in many ways which is not explained here.

Dim sExpr as String
Dim drRows() as DataRow, drSingleRow as DataRow

sExpr = “EmpID > 100″  ‘Condition

drRows = dsEmployee.Tables(0).Select(sExpr)

‘If you need to add Sort Order it can be added to sExpr

Dim sSortOrder as String
sSortOrder = “EmpName DESC”
drRows = dsEmployee.Tables(0).Select(sExpr,sSortOrder)

For Each drSingleRow in drRows
 dtEmp.ImportRow(drRows)
Next

‘ Datatable dtEmp has filtered records

[/sourcecode]
Filter Datatable by Column

If you need to get subset of big dataset into datatable columnwise…

[sourcecode language='vb']

Dim dtEmp as DataTable
Dim dsEmployee as New DataSet

dsEmployoee = someobject.getdata()  ‘dataset can be populated in many ways which is not explained here.

‘This copies the structure and data

dtEmp = dsEmployee.Tables(0).Copy

dtEmp.Columns.Remove(“Unwanted Column 1″)
dtEmp.Columns.Remove(“Unwanted Column 2″)
dtEmp.Columns.Remove(“Unwanted Column 3″)[/sourcecode]

Copy Datatable from one Dataset to another Dataset

0
Digg me

 Copying a datatable from one dataset  to another was not that straightforward as I thought.

[sourcecode language='vb']
Dim dsSource As New DataSet
Dim dsDestination As New DataSet

‘Your way to get data from XML or DB.
dsSource = object.getdata()

dsDestination = object.getmoredata()

‘This is important, without this it will result in error
‘when you try to copy the datatable from one dataset to another

dsSource.Tables(0).TableName = “NewTableName”

dsDestination.Tables.Add(dsSource.Tables(0).Copy)

dsDestination.Tables(0).Tablename = “SomeTable”
dsDestination.Tables(1).Tablename = “NewTableName”   ’The one we copied from other Dataset

[/sourcecode]
Note 1 : When you are using more than one datatable in a dataset it is advisable you name the datatables.

Note 2 : These kinds of scenarios arise, when you are trying to establish Relation between datatables in a dataset.

(Cascading master – detail relation)
 

SQL2000 : Drop Database Contents from Query Analyzer

0
Digg me

U : Table
P : SP
PK : Primary Key
UQ : Unique Constraint
F : Foreign Key
FN : Functions
D : Default

To see the list of User Create Items
– SELECT * FROM sysObjects WHERE xType in (‘U’,'P’,'PK’,'UQ’,'F’,'FN’,'D’)

– Declare @sql NVarChar(4000)– Select @sql= isnull(@sql,”) +’Drop Table ‘ + Name + ‘;’ From sysObjects Where xType=’U'– Exec sp_executesql @sql

– Declare @sql NVarChar(4000)– Select @sql= isnull(@sql,”) +’Drop Proc ‘ + Name + ‘;’ From sysObjects Where xType=’P'– Exec sp_executesql @sql

– Declare @sql NVarChar(4000)– Select @sql= isnull(@sql,”) +’Drop Function ‘ + Name + ‘;’ From sysObjects Where xType=’FN’– Exec sp_executesql @sql