Sql server数据库修改存储过程
1 CREATE PROCEDURE Get_Data
2 (
3 @Dealer_ID VARCHAR(50)
4 )
5 AS
6 SELECT * FROM myData WHERE Dealer_ID = @Dealer_ID
点击查看实际例子
既然创建存储过程已经会了,那么修改还会难吗?显然不会。修改存储过程也是相当的容易。首先,刷新当前数据库的存储过程列表,这时就能看到你刚创建的存储过程的名字了,右键点击它,选择修改,右侧又打开了一个编辑窗口,装着的就是修改存储过程的代码(如下)
1 ALTER PROCEDURE [dbo].[Get_Data]
2 (
3 @Dealer_ID VARCHAR(50)
4 )
5 AS
6 SELECT * FROM myData WHERE Dealer_ID = @Dealer_ID
简单的修改下吧,代码如下
复制代码
1 ALTER PROCEDURE [dbo].[Get_Data]
2 (
3 @Dealer_ID VARCHAR(50),
4 @Period VARCHAR(20)
5 )
6 AS
7 SELECT * FROM myData WHERE Dealer_ID = @Dealer_ID AND Period = @Period
F5 成功执行,修改存储过程完成
开始写了一个存储过程,用来实现数据的插入操作,说白了就是添加数据。这个存储过程的代码如下:
复制代码
1 CREATE PROCEDURE PROC_INSERT_DATA_ID
2 @DealerID varchar(50)
3 AS
4 BEGIN
5 DECLARE @COUNT INT
6
7 SET @COUNT = (SELECT COUNT(*) FROM myDATA_Details WHERE DealerID = @DealerID)
8 IF (@COUNT>0)
9 BEGIN
10 DELETE FROM myDATA_Details WHERE DealerID = @DealerID
11 INSERT INTO myDATA_Details (DealerID) VALUES (@DealerID)
12 END
13 ELSE
14 BEGIN
15 INSERT INTO myDATA_Details (DealerID) VALUES (@DealerID)
16 END
17 END
用于更新数据,代码如下:
复制代码
1 CREATE PROCEDURE PROC_INSERT_DATA_DETAIL
2 @DealerID varchar(50),
3 @FieldName varchar(2000),
4 @FieldValue varchar(2000)
5 AS
6 BEGIN
7 DECLARE @Count INT
8 SET @Count = (SELECT COUNT(*) FROM myDATA_Details WHERE DealerID = @DealerID)
9
10 IF (@COUNT>0)
11 BEGIN
12 UPDATE myDATA_Details SET DealValue = @FieldValue WHERE DealerID = @DealerID
13 END
14 ELSE
15 BEGIN
16 INSERT INTO myDATA_Details (DealerID) VALUES (@DealerID)
17 END
18 END
或者:
修改后的代码
复制代码
1 ALTER PROCEDURE PROC_INSERT_DATA_DETAIL
2 @DealerID varchar(50),
3 @FieldName varchar(2000),
4 @FieldValue varchar(2000)
5 AS
6 BEGIN
7 DECLARE @Count INT
8 DECLARE @StrSQL VARCHAR(2000)
9 SET @Count = (SELECT COUNT(*) FROM myDATA_Details WHERE DealerID = @DealerID)
10
11 IF (@COUNT>0)
12 BEGIN
13 SET @StrSQL = 'UPDATE myDATA_Details SET '+ @FieldName + ' = ''' +@FieldValue + ''' WHERE DealerID = '+ @DealerID
14 EXEC(@StrSQL)
15 END
16 ELSE
17 BEGIN
18 INSERT INTO myDATA_Details (DealerID) VALUES (@DealerID)
19 SET @StrSQL = 'UPDATE myDATA_Details SET '+ @FieldName + ' = ''' +@FieldValue + ''' WHERE DealerID = '+ @DealerID
20 EXEC(@StrSQL)
21 END
22 END