Create Stored Procedure using Visual Studio 2013

This short article shows you how to create Stored Procedure using Visual Studio 2013.

When people is asking how to create Store Procedure using C# and I think create Stored Procedure using Visual Studio 2013 is more accurate. Unless you really want to do SQL CLR https://msdn.microsoft.com/en-us/library/vstudio/zxsa8hkf(v=vs.100).aspx but is that really what you need?

I have gotten a database script from this site http://www.codeproject.com/Tips/806562/Create-Table-and-Stored-Procedure-in-SQL-Server-an to create the Stored Procedure using Visual Studio 2013.

1. Create a database in SQL Management studio, for this example, UsedCarManagement.

2. Create a SQL login, UsedCarManagement in my case and set the default database to UsedCarManagement.

 UserCarManagement

3. Run the script to create Table.

/****** Object:  Table [dbo].[VehicleMaintenanceRecord]    Script Date: 08/16/2014 18:18:03 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE TABLE [dbo].[VehicleMaintenanceRecord](

       [ID] [int] IDENTITY(1,1) NOT NULL,

       [Vehicle_ID] [int] NOT NULL,

       [ServiceBy] [nvarchar](100) NOT NULL,

       [ServiceDate] [date] NULL,

       [ConditionDescr] [nvarchar](max) NULL,

       [ModifiedDate] [datetime] NOT NULL,

 CONSTRAINT [PK_VehicleMaintenanceRecord] PRIMARY KEY CLUSTERED

(

       [ID] ASC

)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

) ON [PRIMARY]

GO

ALTER TABLE [dbo].[VehicleMaintenanceRecord]  WITH CHECK ADD  CONSTRAINT [FK_VehicleMaintenanceRecord_Vehicle] FOREIGN KEY([Vehicle_ID])

REFERENCES [dbo].[Vehicle] ([ID])

GO

ALTER TABLE [dbo].[VehicleMaintenanceRecord] CHECK CONSTRAINT [FK_VehicleMaintenanceRecord_Vehicle]

GO

 dbscript

4. Run the script below to create Stored Procedure.

USE [UsedCarManagement]

GO

/****** Object:  StoredProcedure [dbo].[AddVehicle]    Script Date: 08/16/2014 18:55:16 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE PROCEDURE [dbo].[AddVehicle]

       @Year char(4),

       @make varchar(100),

       @model varchar(100),

       @color varchar(50),

       @trim varchar(50),

       @condition varchar(50),

       @mileage int,

       @VINumber varchar(16),

       @isHold bit,

       @note varchar(max),

       @modifiedDate datetime

AS

BEGIN

       INSERT INTO dbo.Vehicle( Year,Make,Model,Color,Trim,Condition,Mileage,VINumber,IsHold,Note,ModifiedDate)   

        VALUES(@Year,@make,@model,@color,@trim,@condition,@mileage,@VINumber,@isHold,@note,@modifiedDate)

END

GO

—————————–

 

USE [UsedCarManagement]

GO

/****** Object:  StoredProcedure [dbo].[GetAllVehicles]    Script Date: 08/16/2014 18:55:16 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE  PROCEDURE [dbo].[GetAllVehicles]

 

AS

BEGIN

       SELECT vh.ID, vh.Year,vh.Make,vh.Model,vh.Color,vh.Trim,vh.Condition,vh.Mileage,vh.VINumber,vh.ModifiedDate,

              vh.IsHold, vh.Note,vhm.ID as VehMaintenanceID, vhm.ServiceBy, vhm.ServiceDate, vhm.ConditionDescr,vhm.ModifiedDate

       FROM dbo.Vehicle vh

       LEFT JOIN dbo.VehicleMaintenanceRecord vhm on vhm.Vehicle_ID = vh.id

END

GO

—————————-

USE [UsedCarManagement]

GO

/****** Object:  StoredProcedure [dbo].[GetVehicleByVIN]    Script Date: 08/16/2014 18:55:16 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE  PROCEDURE [dbo].[GetVehicleByVIN]

(

       @VINumber varchar(16)

)

 

AS

BEGIN

       SELECT vh.ID, vh.Year,vh.Make,vh.Model,vh.Color,vh.Trim,vh.Condition,vh.Mileage,vh.VINumber,vh.Ishold,Vh.Note,

              vh.ModifiedDate, vhm.ID as VehMaintenanceID, vhm.ServiceBy, vhm.ServiceDate, vhm.ConditionDescr,vhm.ModifiedDate

       FROM dbo.Vehicle vh

       LEFT JOIN dbo.VehicleMaintenanceRecord vhm on vhm.Vehicle_ID = vh.id

       WHERE vh.VINumber = @VINumber

END

GO

 

I edited the original solution and report to http://onedrive.live.com. The sample file name is ConnectSqlServerMM.zip. My MSN ID is chanmmn@hotmail.com.

About chanmingman

Since March 2011 Microsoft Live Spaces migrated to Wordpress (http://www.pcworld.com/article/206455/Microsoft_Live_Spaces_Moves_to_WordPress_An_FAQ.html) till now, I have is over 1 million viewers. This blog is about more than 50% telling you how to resolve error messages, especial for Microsoft products. The blog also has a lot of guidance teaching you how to get stated certain Microsoft technologies. The blog also uses as a help to keep my memory. The blog is never meant to give people consulting services or silver bullet solutions. It is a contribution to the community. Thanks for your support over the years. Ming Man is Microsoft MVP since year 2006. He is a software development manager for a multinational company. With 25 years of experience in the IT field, he has developed system using Clipper, COBOL, VB5, VB6, VB.NET, Java and C #. He has been using Visual Studio (.NET) since the Beta back in year 2000. He and the team have developed many projects using .NET platform such as SCM, and HR based applications. He is familiar with the N-Tier design of business application and is also an expert with database experience in MS SQL, Oracle and AS 400.
This entry was posted in .Net, Community and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s