打開SQL Server 2005的管理工具,選中須要建立存儲過程的數據庫,找到「可編程性」,展開後能夠看到「存儲過程」。右鍵點擊它,選擇「新建存儲過程」,右側的編輯窗口打開了,裏面裝着微軟自動生成的SQL Server建立存儲過程的語句。html
將存儲過程的名字,參數,操做語句寫好後,點擊語法分析,沒有錯誤就直接「F5」運行就行了,存儲過程建立完畢,如下是一個基本的存儲過程的代碼:sql
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 成功執行,修改存儲過程完成。
http://www.cnblogs.com/sosoft/
開始寫了一個存儲過程,用來實現數據的插入操做,說白了就是添加數據。這個存儲過程的代碼以下:sqlserver
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 18 --實際例子:http://hovertree.com/hovertreescj/sql/p_hovertreescj_urls_add.htm
F5一下,建立成功,調用它插入數據,OK,沒問題插入成功,達到了預期的目的
用於更新數據,代碼以下:url
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
或者:
修改後的代碼spa
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
C#調用存儲過程例子: http://www.cnblogs.com/sosoft/p/csccgc.htmlcode
開發技術文章收集 http://www.cnblogs.com/sosoft/p/kaifajishu.htmlserver