|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Today @ 10:45 AM
Points: 489,
Visits: 2,138
|
|
Please consider the following process:
I have a database table which may contain one of 15 different columns like Forename, Surname, Address1, Address2 etc.. Sometimes the table contains forename, surname and sometimes the table may contain Fullname. Other times the table may contain Address1, Address2, Address3 and other it may contain Address1, Town, Postcode etc..
This data needs to go into a standard staging table which has a fixed number of columns.
My current process uses a series of IF statements to work out what columns are in the original table, then it builds an INSERT INTO SELECT statement. Along the way it references several SQL CLR C# UDFs.
This works fine at the moment but I would like to change the design and pass the entire row of data to one SQL CLR C# stored procedure which can then perform all the required string manipulation and return a new row which I can insert into my standardised staging table.
I'm considering the use of table valued parameters but I wonder if this is appropriate?
Any ideas?
----------------------------------- http://www.SQL4n00bs.com
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 12:16 PM
Points: 11,625,
Visits: 27,685
|
|
a CLR has access to all tables in the scope of the transaction if you use the context connection; (ie #temp tables and @Tablevariables one option is to have the CLR select the data directly;
then your process could sya, populate a temp table,and you could process more than one row at a time in your proc if needed, but it would still satisfy the current one-row-at-a-time thing it's doing now.
a rough example:
<Microsoft.SqlServer.Server.SqlProcedure()> _ Public Shared Function CLR_ExportTableToHTML(ByVal TableName As SqlString, ByVal FilePath As SqlString, ByVal IncludeHeaders As SqlInt32, ByVal FileName As SqlString, ByVal Title As SqlString, ByVal Summary As SqlString, ByVal HTMLStyle As SqlInt32) As IEnumerable Dim str As String = "SELECT * FROM {0}" Dim _sFilePath As String = FormattedPath(FilePath.ToString.Trim) Dim _sFileName As String = FormattedFileName(FileName.ToString.Trim) Dim _sFileNameWithPath As String = _sFilePath & _sFileName
Dim MyConnection As New SqlConnection("context connection=true")
str = String.Format(str, TableName) Try
str = String.Format(str, TableName)
MyConnection.Open()
Dim myCommand As New SqlCommand(str, MyConnection) Dim myDataReader As SqlDataReader myDataReader = myCommand.ExecuteReader() Dim MyDataTable As New DataTable(TableName.ToString) MyDataTable.Load(myDataReader)
'NOW we can, in theory, do something with the DataTable. str = FormatDataTableAsHTML(MyDataTable, GetInteger(IncludeHeaders.Value), GetString(Title), GetString(Summary), GetInteger(HTMLStyle.Value)) SaveTextToFile(_sFileNameWithPath, str)
FilePath = "Created: " & _sFileNameWithPath Dim _sResults(0) As SqlChars _sResults(0) = New SqlChars(New SqlString("Created: " & _sFileNameWithPath)) Return _sResults
Catch sqlex As SqlException Throw New Exception("FileName: " & _sFileNameWithPath, sqlex) Catch ex As Exception Throw New Exception("FileName: " & _sFileNameWithPath, ex) End Try
End Function
Lowell
--There is no spoon, and there's no default ORDER BY in sql server either. Actually, Common Sense is so rare, it should be considered a Superpower. --my son
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Today @ 10:45 AM
Points: 489,
Visits: 2,138
|
|
I see what you mean thanks. I will play around with it and see how it goes.
----------------------------------- http://www.SQL4n00bs.com
|
|
|
|