Readme_String Utilities Sample
[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]
This sample works only with SQL Server 2005 and SQL Server 2008. It will not work with any version of SQL Server earlier than SQL Server 2005.
The StringUtilities sample contains a streaming table-valued function written in Visual C# and Visual Basic, which splits a comma-separated string into a table with one column. It also contains an aggregate function that converts a string column to a comma-separated string.
The concat.cs and concat.vb source files contain the implementation of the aggregate function. The split.cs and split.vb source files contain the implementation of the streaming table-valued function. The RegularExpression.cs and RegularExpression.vb source files implement a scalar function and a table-valued function that provide regular expression replacement and regular expression searching functionality.
To implement a streaming table-valued function, create a method that returns an object that implements the IEnumerable interface. This IEnumerable method must be linked by an attribute to another method which fills in rows of the table-valued function.
Note
When you compile common language runtime integration samples, the path of the version of the Microsoft .NET Framework compiler that was distributed with SQL Server must be the first Framework directory in the PATH environment variable. The location of the compiler that is included with SQL Server is either drive:\WINNT\Microsoft.NET\Framework\v2.0.xxxxx or drive:\Windows\Microsoft.NET\Framework\v2.0.xxxxx, where drive is the installation drive and xxxxx is the version of the .NET Framework that is included with SQL Server.
The default installation directory is C:\Program Files\Microsoft SQL Server\100\Samples\Engine\Programmability\CLR\StringUtilities.
SQL Server samples and sample databases must be downloaded and installed before you can view or work with them. For more information, see Considerations for Installing SQL Server Samples and Sample Databases.
Scenario
A developer wants to create a function that returns a table of values by streaming those values to the caller.
Languages
Transact-SQL, Visual C#, and Visual Basic.
Features
The StringUtilities sample uses the following features of SQL Server:
Application Area | Features |
---|---|
Overall |
Common language runtime (CLR) |
Prerequisites
Before you run this sample, make sure the following software is installed:
- SQL Server or SQL Server Express. You can obtain SQL Server Express free of charge from the SQL Server Express Documentation and Samples Web site.
- The AdventureWorks2008R2 database that is available at the SQL Server Developer Web site.
- The SQL Server Database Engine samples that are available at the SQL Server Developer Web site.
- .NET Framework SDK 2.0 or Microsoft Visual Studio 2005. You can obtain .NET Framework SDK free of charge. See Installing the .NET Framework Documentation.
Building the Sample
To build the sample, do the following:
If you have not already created a strong name key file, generate the key file using the following instructions.
To generate a strong name key file
Open a Microsoft Visual Studio 2005 command prompt. Click Start, point to All Programs, point to Microsoft .NET Framework SDK 2.0, and then click SDK Command Prompt.
-- or --
Open a Microsoft .NET Framework command prompt. Click Start, point to All Programs, point to Microsoft .NET Framework SDK 2.0, and then click SDK Command Prompt.
Use the change directory command (CD) to change the current directory of the command prompt window to the folder where the samples are installed.
Note
To determine the folder where samples are located, click the Start button, point to All Programs, point to Microsoft SQL Server, point to Documentation and Tutorials, and then click Samples Directory. If the default installation location was used, the samples are located in <system_drive>:\Program Files\Microsoft SQL Server\100\Samples.
At the command prompt, run the following command to generate the key file:
sn -k SampleKey.snk
Important
For more information about the strong-name key pair, see "Security Briefs: Strong Names and Security in the .NET Framework" in the .NET Development Center on MSDN.
Build the sample
Compile the sample by using Visual Studio 2005 and the provided Visual Studio solution or by using Microsoft MSBuild, which is included in the .NET Framework SDK 2.0. Run a command similar to the following at the command prompt:
msbuild /nologo /verbosity:quiet /property:Configuration=Debug CS\StringUtilities.sln
Ensure that the AdventureWorks2008R2 database is installed, or, if necessary, create the database in which you want to install the samples.
Open the scripts\installCS.sql or scripts\installVB.sql file (depending on whether you compiled the Visual C# project or the Visual Basic project) in SQL Server Management Studio. Run the script that is contained in the file or run a command similar to the following at the command prompt:
sqlcmd -E -I -i Scripts\InstallCS.sql
This file contains the script needed to install and register the sample CLR objects in SQL Server.
Running the Sample
To run the sample, do the following:
Run the sample
Open the scripts\test.sql file in Management Studio and run the script that is contained in the file. Or, run the following command at the command prompt:
sqlcmd -E -I -i Scripts\test.sql
This script shows typical ways to use the sample.
Removing the Sample
To remove the sample, do the following:
Remove the sample
Open the scripts\cleanup.sql file in Management Studio and run the script that is contained in the file. Or, run the following command at the command prompt:
sqlcmd -E -I -i Scripts\cleanup.sql
This script removes the sample.
Comments
The CLR for SQL Server or SQL Server Express must be enabled for this sample to work correctly.
Samples are provided for educational purposes only. They are not intended to be used in a production environment and have not been tested in a production environment. Microsoft does not provide technical support for these samples. Sample applications and assemblies should not be connected to or used with your production SQL Server database or your report server without the permission of the system administrator.