Click here to Skip to main content
15,897,371 members
Articles / Database Development / SQL Server / SQL Server 2008

CLR Stored Procedure and Creating It Step by Step

Rate me:
Please Sign up or sign in to vote.
4.07/5 (19 votes)
22 Aug 2009CPOL6 min read 251.4K   5.8K   99  
CLR Stored procedures are very simple and can be used in most complex scenarios when dealing with database.
-- Execute following statements and conncect "DbForClrDemo" database from your application

--Part I
--Create a new databse for demo
CREATE DATABASE DbForClrDemo

--Use database
USE DbForClrDemo

--Create table for CustomerSalesInformation
CREATE TABLE [dbo].[CustomerSalesInformation](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Name] [varchar](50) NOT NULL,
	[Sales] [decimal](18, 2) NOT NULL DEFAULT ((0)),
 CONSTRAINT [PK_CustomerSalesInformation] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

--Insert dummy data to CustomerSalesInformation table
INSERT INTO [dbo].[CustomerSalesInformation]([Name], [Sales]) VALUES ('Virat Kothari', 50000)
INSERT INTO [dbo].[CustomerSalesInformation]([Name], [Sales]) VALUES ('Dhruval Shah', 5000)
INSERT INTO [dbo].[CustomerSalesInformation]([Name], [Sales]) VALUES ('Urvish Sheth', 15000)
INSERT INTO [dbo].[CustomerSalesInformation]([Name], [Sales]) VALUES ('Rakesh Bajania', 25000)
INSERT INTO [dbo].[CustomerSalesInformation]([Name], [Sales]) VALUES ('Dhaval Shah', 150000)
INSERT INTO [dbo].[CustomerSalesInformation]([Name], [Sales]) VALUES ('Amit Shah', 250000)

--Following statements can be used once the Assembly is deployed to the database

--Part II
--Enable CLR Stored Procedure in database
sp_configure 'clr enabled', 1

--Run following statuement to take effect of above statement
RECONFIGURE

--Now execute our CLR Stored Procedure. Remember "ClrDemo" is name of our Stored Procedure
EXEC [dbo].ClrDemo

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Product Manager
Denmark Denmark
A passionate techno-functional IT Executive with a total of 18+ years of experience in Delivery Head, Project/Product Management, Consulting, Development, R&D, and Delivery of enterprise global IT & Software solutions across multiple domains.

Spent 6+ years as a Program Director and 11+ years in core leadership, strategic planning & implementation, creating high-performance teams.

As a Co-founder and CTO at Xporium.com - one of the pioneer 3D virtual exhibition and digital connection platforms.

Directs and optimizes processes for evaluating alternate technologies while supervising and monitoring various IT and technology departments across the various spectrum for SDLC using various management models including waterfall, SCRUM, Agile, and Extreme Programming.

Heads large and complex projects at PMO and taking care of activities including but not limited to budgeting, funding, planning, personnel & resource allocation, risk and conflicts management, stakeholder management, and closures.

Currently working in cutting edge technologies including Quantum Computing, Artificial Intelligence and Machine Learning where I specifically love Computer Vision. I am a 2X Kaggle Expert.

He believes in "Perfection matters". He enjoys teaching people. When not working, Virat likes to swim and sleep.

Comments and Discussions