SELECT A.name AS ViewName
, count(B.TABLE_NAME) AS ColCount
FROM
sys.objects AS A
LEFT OUTER JOIN
INFORMATION_SCHEMA.COLUMNS AS B
ON A.name = B.TABLE_NAME
GROUP BY
A.name
, A.type
HAVING
(A.type = 'v')
ORDER BY
ColCount DESC
Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.IO
<System.AddIn.AddIn("ScriptMain", Version:="1.0", Publisher:="", Description:="")> _
<System.CLSCompliantAttribute(False)> _
Partial Public Class ScriptMain
Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
Enum ScriptResults
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
End Enum
Public Sub Main()
'Variable collection to hold the variables
Dim VarCol As Variables = Nothing
'Lock the 3 variables for read
Dts.VariableDispenser.LockForRead("User::StrViewName")
Dts.VariableDispenser.LockForRead("User::StrDelimiter")
Dts.VariableDispenser.LockForRead("User::StrFilePath")
'Fille the Variable collection
Dts.VariableDispenser.GetVariables(VarCol)
'Getting the variables' values, so we can use it later
Dim ViewName As String = VarCol("User::StrViewName").Value.ToString()
Dim delimiter As String = VarCol("User::StrDelimiter").Value.ToString()
Dim FilePath As String = VarCol("User::StrFilePath").Value.ToString()
'Construct the file name, example output: D:\vTargetMail.TXT
Dim FName As String = FilePath & ViewName & ".TXT"
'Build our Query
Dim Query As String = "SELECT * FROM " & ViewName
'Using StreamWriter
Dim writer As StreamWriter = Nothing
'Use our OLEDB COnnection
Dim connection As OleDbConnection = New OleDbConnection(Dts.Connections("Localhost").ConnectionString)
Dim command As OleDbCommand = Nothing
Dim reader As OleDbDataReader = Nothing
Try
'Checking for the file, delete if exist(you can append or rename with [File.Move(FName, Today() & "_" & FName)]
If File.Exists(FName) Then
File.Delete(FName)
End If
'Open the OLEDB connection
connection.Open()
'Run the query
command = New OleDbCommand(Query, connection)
reader = command.ExecuteReader()
If reader.HasRows Then
'Stream Writer using the FNAME that we declared erlier
writer = New System.IO.StreamWriter(FName)
While reader.Read()
'Counter to get the columns number
Dim counter As Integer = 0
Dim fieldCount As Integer = reader.FieldCount - 1
While counter <= fieldCount
If counter <> fieldCount Then
writer.Write(reader(counter).ToString() & delimiter)
Else
writer.WriteLine(reader(counter).ToString())
End If
counter += 1
End While
End While
End If
Catch ex As Exception
Dts.Events.FireError(1, "", "Something Wrong happened!!!", "", 0)
Finally
connection.Close()
writer.Close()
End Try
Dts.TaskResult = ScriptResults.Success
End Sub
End Class
SELECT (sys.schemas.name + '.' + sys.objects.name) AS ViewName
FROM sys.objects
INNER JOIN sys.schemas ON sys.objects.schema_id = sys.schemas.schema_id
Where type ='v'
Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.IO
Public Class ScriptMain
Public Sub Main()
'Variable collection to hold the variables
Dim VarCol As Variables = Nothing
'Lock the 3 variables for read
Dts.VariableDispenser.LockForRead("User::StrViewName")
Dts.VariableDispenser.LockForRead("User::StrDelimiter")
Dts.VariableDispenser.LockForRead("User::StrFilePath")
'Fille the Variable collection
Dts.VariableDispenser.GetVariables(VarCol)
'Getting the variables' values, so we can use it later
Dim ViewName As String = VarCol("User::StrViewName").Value.ToString()
Dim delimiter As String = VarCol("User::StrDelimiter").Value.ToString()
Dim FilePath As String = VarCol("User::StrFilePath").Value.ToString()
'Construct the file name, example output: D:\vTargetMail.TXT
Dim FName As String = FilePath & ViewName & ".TXT"
'Using StreamWriter
Dim writer As StreamWriter = Nothing
'Try
'Checking for the file, delete if exist(you can append or rename with [File.Move(FName, Today() & "_" & FName)]
If File.Exists(FName) Then
File.Delete(FName)
End If
'Open the OLEDB connection
Dim connection As New OleDbConnection("Provider=SQLOLEDB;Data Source=Localhost;Initial Catalog=AdventureWorks;Integrated Security=SSPI;")
connection.Open()
Dim Query As String = "SELECT * FROM " & ViewName
Dim command As OleDbCommand = New OleDbCommand(Query, connection)
Dim reader As OleDbDataReader = command.ExecuteReader()
If reader.HasRows Then
'Stream Writer using the FNAME that we declared erlier
writer = New System.IO.StreamWriter(FName)
While reader.Read()
'Counter to get the columns number
Dim counter As Integer = 0
Dim fieldCount As Integer = reader.FieldCount - 1
While counter <= fieldCount
If counter <> fieldCount Then
writer.Write(reader(counter).ToString() & delimiter)
Else
writer.WriteLine(reader(counter).ToString())
End If
counter += 1
End While
End While
reader.Close()
End If
connection.Close()
writer.Close()
'Catch ex As Exception
' Dts.Events.FireError(1, "Nasty", "Something Wrong happened!!!", "", 0)
' Throw ex
'Finally
'End Try
Dts.TaskResult = Dts.Results.Success
End Sub
End Class
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (2)
Commented:
You can suppress the need of first execute sql task who fetch data from sysobjects with using the ADO.NET Schema RowSet Enumerator in Foreach loop, Just take a look at this type of enumerator, and then you will find out how to fetch sysobjects data with this enumerator simply.
Great job by the way, I voted for it. Hope to read more from you on SSIS :)
Reza Rad
Author
Commented:Yep, true, I could have used ADO.NET in the Foreach Loop.
Anyway there's always 100 ways to do anything :)
I was trying also to avoid BCP as it always gives me hard time, I don't know why !!
Thank you so much...for the encouraging words and the vote
Jason Yousef