How to pass row to SQL CLR stored procedure ?

  • 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[/url]

    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

  • 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


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • 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[/url]

    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

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply