banner



How To Write To Sp Register From C Variable Ccs

In this article you will learn about how to create a stored process in SQL. This article covers answers to the post-obit questions,

  1. What is a stored procedure in SQL?
  2. Why do we use SET NOCOUNT ON in a stored procedure?
  3. How many types of stored procedure are at that place?
  4. How to write comments in SQL Server?
  5. What are the naming conventions for stored procedures?
  6. How to create a stored procedure to select data from a database tabe using SELECT SQL query?
  7. How to execute stored procedures in SQL Server?
  8. What is are parameters in stored procedures?
  9. How to create parameters in a SELECT query stored procedure which return records as per parameter passed?
  10. How to create an INSERT query based stored procedure?
  11. How to create an UPDATE query based stored procedure?
  12. How to create a stored process to delete records using DELETE query?

What is a Stored Procedure?

A SQL stored process (SP) is a collection SQL statements and sql command logic, which is compiled and stored on the database. Stored procedues in SQL allows us to create SQL queries to be stored and executed on the server. Stored procedures can besides be cached and reused. The principal purpose of stored procedures to hibernate direct SQL queries from the code and better operation of database operations such as select, update, and delete data.

Why do nosotros utilise SET NOCOUNT ON in a stored procedure?

While we gear up SET NOCOUNT ON information technology means there is no messages which shows the number of rows affected.

NOCOUNT means do not count that is ON.

Now you will come to know what happened when Ready NOCOUNT OFF.

Types of stored procedures

At that place are 2 types of stored procedures available in SQL Server:

  1. User divers stored procedures
  2. Organisation stored procedures

User divers stored procedures

User defined stored procedures are created by database developers or database administrators. These SPs contains one more more SQL statements to select, update, or delete records from database tables. User defined stored procedure tin can take input parameters and return output parameters. User divers stored process is mixture of DDL (Data Definition Language) and DML (Information Manipulation Language ) commands.

User divers SPs are farther classified into two types:

T-SQL stored procedures:T-SQL (Transact SQL) SPs receive and returns parameters. These SPs procedure the Insert, Update and Delete queries with or without parameters and render data of rows every bit output. This is one of the most common ways to write SPs in SQL Server.

CLR stored procedures:CLR (Mutual Linguistic communication Runtime) SPs are written in a CLR based programming language such as C# or VB.NET and are executed by the .Internet Framework.

System stored procedures

Organisation stored procedyres are created and executed by SQL Server for the server administrative activities. Developers usually don't interfere with system SPs.

Login to SQL Server database

Allow's login to our SQL Server database, so we can accomplish the folowing:

  • How to create a SELECT QUERY based stored procedure which return all records?
  • How to create a PARAMETER based SELECT QUERY stored procedure which return records based on parameters?
  • How to create an INSERT query based stored process?
  • How to create an UPDATE query based stored procedure?
  • How to create a DELETE query based stored process?

Login in SQL SERVER with your Server Proper name, Login and Countersign.

stored procedure In SQL Server

Switch to your database. My database name is MBKTest.

stored procedure In SQL Server

Empty stored procedure will be created using the following:

stored procedure In SQL Server

The empty template created by SQL Server for a SP looks like the post-obit. The CREATE PROCEDURE SQL control is used to create a procedure, followed by a SP name and its parameters. The BEGIN and END area is used to ascertain the query for the operation. This is where you will write a select, update, insert, or delete queries.

  1. SET  ANSI_NULLS ON
  2. GO
  3. SET  QUOTED_IDENTIFIER ON
  4. Become
  5. CREATE PROCEDURE  <Procedure_Name, sysname, ProcedureName>
  6.     <@Param1, sysname, @p1> <Datatype_For_Param1, ,int > = <Default_Value_For_Param1, , 0>,
  7.     <@Param2, sysname, @p2> <Datatype_For_Param2, ,int > = <Default_Value_For_Param2, , 0>
  8. AS
  9. Brainstorm
  10. Prepare  NOCOUNT ON ;
  11. SELECT  <@Param1, sysname, @p1>, <@Param2, sysname, @p2>
  12. END
  13. GO

How to write comments in SQL SERVER?

You tin annotate in sql server in the following ways,

  1. -- (two hyphens / dash) for a single line of comment.
  2. beginning with /* ……. stop with  */    for multiline comments.

What is the naming convention for stored procedures?

We must follow standard naming conventions which may also depend on your projection and coding policies.

For user divers stored procedure naming conventions, my suggestions are to add ane of the post-obit prefixes to your SP names.

  1. sp
  2. stp
  3. stp_
  4. udstp
  5. udstp_

Naming conventions are just to identify objects. By calculation these prefixes in the proper name, nosotros can clearly identify that this object is a stored procedure.

Create a database table

Before, we can create and execute any SPs, we need a database tabular array. I create a database table named, "tblMembers" using the following SQL query and execute it on the server. Equally you tin see, my table has iv cavalcade where the first cavalcade is an idenity column. Once the table is created, open tabular array in your SSMS and add together some data past manually inbound data to the table.

  1. USE [MBKTest]
  2. Become
  3. /****** Object:Table  [dbo].[tblMembers]    Script Date : xviii-Nov-17,Sabbatum half dozen:47:55 PM ******/
  4. SET  ANSI_NULLS ON
  5. Become
  6. Set  QUOTED_IDENTIFIER ON
  7. Go
  8. Fix  ANSI_PADDING ON
  9. GO
  10. CREATE Table  [dbo].[tblMembers](
  11.     [MemberID] [int ] IDENTITY(1,1) NOT NULL ,
  12.     [MemberName] [varchar ](50) Nada ,
  13.     [MemberCity] [varchar ](25) NULL ,
  14.     [MemberPhone] [varchar ](15) Aught
  15. )
  16. Get
  17. SET  ANSI_PADDING OFF
  18. Become

How to create a SELECT stored procedure?

Click on your Database and expand "Programmability" item and right click on "Stored Procedures" or press CTRL + N to get new query window. In the query area betwixt BEGIN and END, type your SELECT statement to select records from the table. See the Select statement in the beneath code.

  1. Set  ANSI_NULLS ON
  2. Become
  3. Fix  QUOTED_IDENTIFIER ON
  4. GO
  5. CREATE Procedure  stpGetAllMembers
  6. AS
  7. Brainstorm
  8. SET  NOCOUNT ON ;
  9. Select  * from  tblMembers
  10. END
  11. GO

Now, press F5 or click on Execute button to execute the SP.

stored procedure In SQL Server

stored procedure In SQL Server

You lot should see a message, "Command(due south) completed successfully."

Now go to Programmability -->Stored Procedures, Right Click and select Refresh.

You tin can encounter in the following image, the new SP called stpGetAllMembers is created.

stored procedure In SQL Server

Execute stored procedures in SQL Server

In beneath UI, right click on the SP name and select Execute Stored Process... to execute a SP. From here, you tin can also alter an exisitng SP.

stored procedure In SQL Server

Alternatively, you lot tin also execute a SP from the Query window.

To run stored procedure in SQL Server Direction Studio, switch to Query window or CTRL +Northward to open up an new query window and blazon the following command.

  • Syntax - EXEC <stored procedure name>
  • Example - EXEC stpGetAllMembers

Now, we run our stored procedure called stpGetAllMembers. The output looks similar the post-obit:

OUTPUT

stored procedure In SQL Server

What are parameters in stored procedures?

Parameters in SPs are used to laissez passer input values and render output values. At that place are two types of parameters:

  1. Input parameters - Pass values to a stored process.
  2. Output parameters - Return values from a stored process.

How to create a SELECT query SP with parameters?

In the previous steps, nosotros created a simple SP that returned all rows from a table. At present, let's create a new SP that will take a city name equally an inpurt parameter and will return all rows where city proper name matches the input parameter value.

Hither is the updated SP with a parameter @CityName.

  1. Gear up  ANSI_NULLS ON
  2. Go
  3. SET  QUOTED_IDENTIFIER ON
  4. GO
  5. CREATE PROCEDURE  stpGetMembersByCityName
  6.     @CityName nvarchar(xxx)
  7. AS
  8. Brainstorm
  9. SET  NOCOUNT ON ;
  10. Select  * From  tblMembers
  11. where  MemberCity like '%' +@CityName+ '%'
  12. Finish
  13. GO

Execute information technology.

To run this SP, type the following command in SQL query tool:

EXEC GetMemberByCityName   @CityName = 'mal'

OR from the UI, run the SP and provide the following input.

stored procedure In SQL Server

The code to execute looks like the following:

  1. Employ [MBKTest]
  2. Become
  3. DECLARE  @return_value int
  4. EXEC     @return_value = [dbo].[GetMemberByCityName]
  5.         @CityName = Due north'mal'
  6. SELECT 'Return Value'  = @return_value
  7. Become

OUTPUT

stored procedure In SQL Server

How to create a INSERT query based stored procedure?

Nosotros tin can utilize an INSERT INTO SQL query to insert data into a table. The following SQL statement creates an INSERT SP with three parameters.

  1. SET  ANSI_NULLS ON
  2. Get
  3. SET  QUOTED_IDENTIFIER ON
  4. GO
  5. CREATE Procedure  stpInsertMember
  6. @MemberNamevarchar (50),
  7. @MemberCityvarchar (25),
  8. @MemberPhonevarchar (15)
  9. As
  10. Brainstorm
  11. SET  NOCOUNT ON ;
  12. Insert into  tblMembers (MemberName,MemberCity,MemberPhone)
  13. Values  (@MemberName,@MemberCity, @MemberPhone)
  14. END
  15. GO

Right click on stored procedure in Object Explorer and select Refresh.

stored procedure In SQL Server

Laissez passer the value of parameter in Execute dialog box. Something like this:

stored procedure In SQL Server

The following lawmaking tin can be used to execute this SP in SSMS.

  1. USE [MBKTest]
  2. GO
  3. DECLARE  @return_value int
  4. EXEC     @return_value = [dbo].[stpInsertMember]
  5.         @MemberName = N'Mahesh Chand' ,
  6.         @MemberCity = N'NewYork' ,
  7.         @MemberPhone = North'9999945121'
  8. SELECT 'Return Value'  = @return_value
  9. GO

OUTPUT

In the query window, you can cheque if a new record for Member Proper noun 'Mahesh Chand' is added to the table.

stored procedure In SQL Server

Y'all tin can also run the same SP in code.

EXEC stpInsertMember @MemberName = 'Suhana & Ashish Kalla ', @MemberCity = 'Mumbai ', @MemberPhone = N'9022592774xxx'

stored procedure In SQL Server

OUTPUT

You tin can check "Suhana & Ashish Kalla" record is added successfully.

stored procedure In SQL Server

How to create an UPDATE quert based stored process?

Let's create a new SP that volition update a table records based on the Member ID cavalcade. The ID is passed as an input parameter. Here is the new SP that uses an UPDATE..SET..WHERE command.

  1. Prepare  ANSI_NULLS ON
  2. GO
  3. Set  QUOTED_IDENTIFIER ON
  4. Become
  5. CREATE PROCEDURE  stpUpdateMemberByID
  6. @MemberIDint ,
  7. @MemberNamevarchar (l),
  8. @MemberCityvarchar (25),
  9. @MemberPhonevarchar (15)
  10. As
  11. Begin
  12. SET  NOCOUNT ON ;
  13. UPDATE  tblMembers
  14. Ready  MemberName = @MemberName,
  15.         MemberCity = @MemberCity,
  16.         MemberPhone = @MemberPhone
  17. Where  MemberID = @MemberID
  18. Terminate
  19. Become

Right click on stored procedure in the Object Explorer and select Refresh. You volition see the SP is created.

At present, Right click on SP name and select Execute stored procedure…. Provide the input values and execute.

stored procedure In SQL Server

We tin use the following control in SSMS.

  1. Apply [MBKTest]
  2. Get
  3. DECLARE  @return_value int
  4. EXEC     @return_value = [dbo].[stpUpdateMemberByID]
  5.         @MemberID = 20,
  6.         @MemberName = Northward'Nirupama Kalla' ,
  7.         @MemberCity = N'Mumbai' ,
  8.         @MemberPhone = North'904512541xxxx'
  9. SELECT 'Return Value'  = @return_value
  10. GO

EXEC stpUpdateMemberByID  17,'Gopal Madhavrai','Bikaner','90454564xxx'

The results should show you lot the updated values.

stored procedure In SQL Server

How to create a DELETE query based stored procedure?

Let's create a SP that will delete records. The new SP uses a DELETE command and delete all records that matches provided Member ID.

  1. Set  ANSI_NULLS ON
  2. Get
  3. Prepare  QUOTED_IDENTIFIER ON
  4. GO
  5. CREATE Process  stpDeleteMemberByMemberID
  6.     @MemberIDint
  7. AS
  8. Begin
  9. SET  NOCOUNT ON ;
  10. Delete from  tblMembers
  11. where  MemberId = @MemberID
  12. Stop
  13. Become

Execute information technology.

Right click on Stored Procedures in the Object Explorer and select Refresh.

RUN stored procedure BY UI

Now over again correct click on stored procedure and select Execute stored procedure…

Equally you can see in the prototype, I passed @MemberID parameter value = four.

stored procedure In SQL Server

RUN DELETE stored procedure Past MANUALLY (CODING)

EXEC stpDeleteMemberByMemberID  2

OUTPUT

Y'all can come across in paradigm MemberID = 4 tape has been deleted successfully.

stored procedure In SQL Server

In this article, we saw how to create stored procedures in a SQL Server database for inserting, updating, and deleting records.

How To Write To Sp Register From C Variable Ccs,

Source: https://www.c-sharpcorner.com/article/how-to-create-a-stored-procedure-in-sql-server-management-studio/

Posted by: lukenrion1963.blogspot.com

0 Response to "How To Write To Sp Register From C Variable Ccs"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel