Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create stored procedures in SQLServer

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces how to create stored procedures in SQLServer, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.

Open the management tools of SQL Server 2005, select the database where you need to create stored procedures, find programmability, and expand to see stored procedures. Right-click it, select "New stored procedure", and the editing window on the right opens with Microsoft's automatically generated SQL Server statement to create the stored procedure.

After writing the name, parameters and operation statements of the stored procedure, click on the syntax analysis, and run "F5" directly without errors. After the stored procedure has been created, the following is the code of a basic stored procedure:

CREATE PROCEDURE Get_Data (@ Dealer_ID VARCHAR (50)) AS SELECT * FROM myData WHERE Dealer_ID = @ Dealer_ID

Click to view actual examples

Now that you know how to create a stored procedure, will it be difficult to modify it? Apparently not. It is also quite easy to modify stored procedures. First of all, refresh the list of stored procedures in the current database, and then you can see the name of the stored procedure you just created. Right-click it, select modify, and open an editing window on the right. It contains the code to modify the stored procedure (see below).

ALTER PROCEDURE [dbo]. [Get_Data] (@ Dealer_ID VARCHAR (50)) AS SELECT * FROM myData WHERE Dealer_ID = @ Dealer_ID

Simply modify it, the code is as follows

ALTER PROCEDURE [dbo]. [Get_Data] (@ Dealer_ID VARCHAR (50), @ Period VARCHAR (20)) AS SELECT * FROM myData WHERE Dealer_ID = @ Dealer_ID AND Period = @ Period

F5 was executed successfully, and the modified stored procedure was completed.

Began to write a stored procedure to implement the data insertion operation, to put it bluntly, to add data. The code for this stored procedure is as follows:

CREATE PROCEDURE PROC_INSERT_DATA_ID @ DealerID varchar (50) AS BEGIN 5 DECLARE @ COUNT INT SET @ COUNT = (SELECT COUNT (*) FROM myDATA_Details WHERE DealerID = @ DealerID) IF (@ COUNT > 0) BEGIN DELETE FROM myDATA_Details WHERE DealerID = @ DealerID INSERT INTO myDATA_Details (DealerID) VALUES (@ DealerID) END ELSE BEGIN INSERT INTO myDATA_Details (DealerID) VALUES (@ DealerID) END END-practical example: http://hovertree .com / hovertreescj/sql/p_hovertreescj_urls_add.htm

F5, create successfully, call it to insert data, OK, no problem insert successfully, achieve the desired goal

To update the data, the code is as follows:

CREATE PROCEDURE PROC_INSERT_DATA_DETAIL @ DealerID varchar (50), @ FieldName varchar (2000), @ FieldValue varchar (2000) AS BEGIN DECLARE @ Count INT SET @ Count = (SELECT COUNT (*) FROM myDATA_Details WHERE DealerID = @ DealerID) IF (@ COUNT > 0) BEGIN UPDATE myDATA_Details SET DealValue = @ FieldValue WHERE DealerID = @ DealerID END ELSE BEGIN INSERT INTO myDATA_Details (DealerID) VALUES (@ DealerID) END END

Or:

Modified code

ALTER PROCEDURE PROC_INSERT_DATA_DETAIL @ DealerID varchar (50), @ FieldName varchar (2000) @ FieldValue varchar (2000) AS BEGIN DECLARE @ Count INT DECLARE @ StrSQL VARCHAR (2000) SET @ Count = (SELECT COUNT (*) FROM myDATA_Details WHERE DealerID = @ DealerID) IF (@ COUNT > 0) BEGIN SET @ StrSQL = 'UPDATE myDATA_Details SET' + @ FieldName +'=''+ @ FieldValue +''WHERE DealerID =' + @ DealerID EXEC (@ StrSQL) END ELSE BEGIN INSERT INTO myDATA_Details (DealerID) VALUES (@ DealerID) SET @ StrSQL = 'UPDATE myDATA_Details SET' + @ FieldName +'=''+ @ FieldValue +''WHERE DealerID =' + @ DealerID EXEC (@ StrSQL) END END about how to create stored procedures in SQLServer I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report