Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to pass row to SQL CLR stored procedure ? Expand / Collapse
Author
Message
Posted Tuesday, March 5, 2013 4:29 AM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Thursday, June 26, 2014 4:08 AM
Points: 708, Visits: 3,286
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?


---------------------------------------------------------


It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
David Edwards - Media lens

Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
Howard Zinn
Post #1426740
Posted Tuesday, March 5, 2013 5:34 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:44 PM
Points: 12,904, Visits: 31,978
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
Post #1426767
Posted Tuesday, March 5, 2013 6:52 AM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Thursday, June 26, 2014 4:08 AM
Points: 708, Visits: 3,286
I see what you mean thanks. I will play around with it and see how it goes.


---------------------------------------------------------


It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
David Edwards - Media lens

Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
Howard Zinn
Post #1426801
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse