So, I created another SQL-CLR. This time it is a User Defined Function that is used to parse barcodes.
When setting this up, I added two attributes
This will end up calling the FillRow function to populate the row, for each value in the IEnumerable. In this case I call the GetData function to parse out my barcode and return a string array of values I needed to populate each of the table fields.
Now to deploy, you will need to first set your database table as TRUSTWORTHY
Note: To help debug the deployment script, copy the auto-generated deployment sql script, which can be found in the \bin\Debug and run it in SSMS, turned on "SQLCMD Mode" and executed the statement.
Once the code has been succesfully deployed. I just need to call the function, in SSMS, like so:
SELECT* FROM [dbo].[BarCodeParsercs] (N']d2400PO_09928-1~2413000510_E~96400~9320100614-320008210~910278~9232~94')
Helpful Resources:
http://www.mssqltips.com/sqlservertip/2582/introduction-to-sql-server-clr-table-valued-functions/
http://www.mssqltips.com/sqlservertip/1986/sqlclr-function-to-return-free-space-for-all-drives-on-a-server/
When setting this up, I added two attributes
- FillRowMethodName - which is used to populate a table's row. The setup for this function, line 132 - 156, must accept an object as it's first value and then output fields that are used to populate the row.
- TableDefinition - what the table that I want to return should include. One of the problems I ran into was that I was originally using varchar as my table values, but I would get this error: "Cannot use 'varchar' column in the result table of a streaming user-defined function".
This will end up calling the FillRow function to populate the row, for each value in the IEnumerable. In this case I call the GetData function to parse out my barcode and return a string array of values I needed to populate each of the table fields.
Now to deploy, you will need to first set your database table as TRUSTWORTHY
Note: To help debug the deployment script, copy the auto-generated deployment sql script, which can be found in the \bin\Debug and run it in SSMS, turned on "SQLCMD Mode" and executed the statement.
Code Snippet
- using System;
- using System.Data;
- using System.Data.SqlClient;
- using System.Data.SqlTypes;
- using Microsoft.SqlServer.Server;
- using System.Collections;
- public partial class UserDefinedFunctions
- {
- [Microsoft.SqlServer.Server.SqlFunction(
- FillRowMethodName="FillRow",
- TableDefinition = "BarcodeType nvarchar(50) NULL, " +
- "PO nvarchar(50) NULL, " +
- "PartNumber nvarchar(50) NULL, " +
- "Qty nvarchar(50) NULL, " +
- "DateCode nvarchar(50) NULL, " +
- "SupplierName nvarchar(50) NULL, " +
- "SupplierSite nvarchar(50) NULL, " +
- "Date nvarchar(50) NULL, " +
- "Weight nvarchar(50) NULL")]
- public static IEnumerable BarCodeParsercs(SqlString barcode)
- {
- return new String[]{barcode.Value};
- }
- private static IEnumerable GetData(SqlString barcode)
- {
- string BarCodeType = "";
- string PO = "";
- string PartNumber = "";
- string Qty = "";
- string DateCode = "";
- string SupplierName = "";
- string SupplierSite = "";
- string Date = "";
- string Weight = "";
- string Barcode = barcode.Value;
- BarCodeType = Barcode.Substring(0, 3);
- switch (BarCodeType)
- {
- case "]d2":
- {
- BarCodeType = "]d2";
- Barcode = Barcode.Substring(3);
- break;
- }
- case "]C0":
- {
- BarCodeType = "]C0";
- Barcode = Barcode.Substring(3);
- break;
- }
- }
- string[] sections = Barcode.Split('~');
- foreach (string section in sections)
- {
- string ThreeDigitCode = "";
- string TwoDigitCode = "";
- bool FoundMatch = false;
- if (section.Length > 3)
- {
- ThreeDigitCode = section.Substring(0, 3);
- }
- if (section.Length > 2)
- {
- TwoDigitCode = section.Substring(0, 2);
- }
- switch (ThreeDigitCode)
- {
- case "241":
- {
- PartNumber = section.Substring(3);
- FoundMatch = true;
- break;
- }
- case "400":
- {
- PO = section.Substring(3);
- FoundMatch = true;
- break;
- }
- }
- if (FoundMatch.Equals(false))
- {
- switch (TwoDigitCode)
- {
- case "96":
- {
- Qty = section.Substring(2);
- break;
- }
- case "93":
- {
- DateCode = section.Substring(2);
- break;
- }
- case "91":
- {
- SupplierName = section.Substring(2);
- break;
- }
- case "92":
- {
- SupplierSite = section.Substring(2);
- break;
- }
- case "94":
- {
- Date = section.Substring(2);
- break;
- }
- case "95":
- {
- Weight = section.Substring(2);
- break;
- }
- }
- }
- }
- return new string[] { barcode.Value, BarCodeType, PO, PartNumber, Qty, DateCode, SupplierName, SupplierSite, Date, Weight };
- }
- public static void FillRow(Object obj,
- out SqlString BarcodeType,
- out SqlString PO,
- out SqlString PartNumber,
- out SqlString Qty,
- out SqlString DateCode,
- out SqlString SupplierName,
- out SqlString SupplierSite,
- out SqlString Date,
- out SqlString Weight)
- {
- String barcode = (String)obj;
- String[] BarCode = (String[])GetData(barcode);
- BarcodeType = (SqlString)BarCode[1];
- PO = (SqlString)BarCode[2];
- PartNumber = (SqlString)BarCode[3];
- Qty = (SqlString)BarCode[4];
- DateCode = (SqlString)BarCode[5];
- SupplierName = (SqlString)BarCode[6];
- SupplierSite = (SqlString)BarCode[7];
- Date = (SqlString)BarCode[8];
- Weight = (SqlString)BarCode[9];
- }
- };
Once the code has been succesfully deployed. I just need to call the function, in SSMS, like so:
SELECT* FROM [dbo].[BarCodeParsercs] (N']d2400PO_09928-1~2413000510_E~96400~9320100614-320008210~910278~9232~94')
Helpful Resources:
http://www.mssqltips.com/sqlservertip/2582/introduction-to-sql-server-clr-table-valued-functions/
http://www.mssqltips.com/sqlservertip/1986/sqlclr-function-to-return-free-space-for-all-drives-on-a-server/