Sql server數據庫修改存儲過程

Sql server數據庫修改存儲過程數據庫

 

1 CREATE PROCEDURE Get_Data   server

2    (   io

3        @Dealer_ID VARCHAR(50)  數據

4     )   存儲過程

5     AS   db

6 SELECT * FROM myData WHERE Dealer_ID = @Dealer_IDarc

點擊查看實際例子

 

既然建立存儲過程已經會了,那麼修改還會難嗎?顯然不會。修改存儲過程也是至關的容易。首先,刷新當前數據庫的存儲過程列表,這時就能看到你剛建立的存儲過程的名字了,右鍵點擊它,選擇修改,右側又打開了一個編輯窗口,裝着的就是修改存儲過程的代碼(以下)

 

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

相關文章
相關標籤/搜索