存储过程 与 SQL Server语句大比拼
来源:互联网 发布:通讯网络机柜 编辑:程序博客网 时间:2024/05/21 04:39
使用sql server存储过程,可以在数据库中实现多种功能,下面就为您介绍其中的一种,供您参考,希望对您学习sql server存储过程的使用有所帮助。
如果需要同时插入N条数据,不想在程序里控制,但是SQL Sever又不支持数组参数.所以只能用变通的办法了.利用SQL Server强大的字符串处理传把数组格式化为类似"1,2,3,4,5,6",然后在sql server存储过程中用SubString配合CharIndex把分割开来。
详细的sql server存储过程:
- CREATE PROCEDURE dbo.ProductListUpdateSpecialList
- @ProductId_Array varChar(800),
- @ModuleId int
- AS
- DECLARE @PointerPrev int
- DECLARE @PointerCurr int
- DECLARE @TId int
- Set @PointerPrev=1
- set @PointerCurr=1
- begin transaction
- Set NoCount ON
- delete from ProductListSpecial where ModuleId=@ModuleId
- Set @PointerCurr=CharIndex(',',@ProductId_Array,@PointerPrev+1)
- set @TId=cast(SUBSTRING(@ProductId_Array,@PointerPrev,@PointerCurr-@PointerPrev) as int)
- Insert into ProductListSpecial (ModuleId,ProductId) Values(@ModuleId,@TId)
- SET @PointerPrev = @PointerCurr
- while (@PointerPrev+1 < LEN(@ProductId_Array))
- Begin
- Set @PointerCurr=CharIndex(',',@ProductId_Array,@PointerPrev+1)
- if(@PointerCurr>0)
- Begin
- set @TId=cast(SUBSTRING(@ProductId_Array,@PointerPrev+1,@PointerCurr-@PointerPrev-1) as int)
- Insert into ProductListSpecial (ModuleId,ProductId) Values(@ModuleId,@TId)
- SET @PointerPrev = @PointerCurr
- End
- else
- Break
- End
- set @TId=cast(SUBSTRING(@ProductId_Array,@PointerPrev+1,LEN(@ProductId_Array)-@PointerPrev) as int)
- Insert into ProductListSpecial (ModuleId,ProductId) Values(@ModuleId,@TId)
- Set NoCount OFF
- if @@error=0
- begin
- commit transaction
- end
- else
- begin
- rollback transaction
- end
- GO
本文从多个角度阐述了SQL Server与存储过程的比较。
①为什么要使用存储过程?
因为它比SQL语句执行快。
②存储过程是什么?
把一堆SQL语句罗在一起,还可以根据条件执行不通SQL语句。
③来一个最简单的存储过程:
CREATE PROCEDURE dbo.testProcedure_AXASselect userID from USERS order by userid desc
注:dbo.testProcedure_AX是你创建的存储过程名,可以改为:AXzhz等,别跟关键字冲突就行了.AS下面就是一条SQL语句,不会写SQL语句的请回避。
④怎么在ASP.NET中调用这个存储过程:
public static string GetCustomerCName(ref ArrayList arrayCName,ref ArrayList arrayID) { SqlConnection con=ADConnection.createConnection(); SqlCommand cmd=new SqlCommand("testProcedure_AX",con); cmd.CommandType=CommandType.StoredProcedure; con.Open(); try { SqlDataReader dr=cmd.ExecuteReader(); while(dr.Read()) { if(dr[0].ToString()=="") { arrayCName.Add(dr[1].ToString()); } } con.Close(); return "OK!"; } catch(Exception ex) { con.Close(); return ex.ToString(); } }
注:其实就是把以前:
SqlCommand cmd=new SqlCommand("select userID from USERS order by userid desc",con);
中的SQL语句替换为存储过程名,再把cmd的类型标注为CommandType.StoredProcedure(存储过程)。
⑤写个带参数的存储过程:
CREATE PROCEDURE dbo.AXzhz/*这里写注释*/@startDate varchar(16),@endDate varchar(16) AS select id from table_AX where commentDateTime>@startDate and commentDateTime<@endDate order by contentownerid DESC
注:@startDate varchar(16)是声明@startDate 这个变量,多个变量名间用【,】隔开.后面的SQL就可以使用这个变量了。
⑥我怎么在ASP.NET中调用这个带参数的存储过程:
public static string GetCustomerCNameCount(string startDate,string endDate,ref DataSet ds){ SqlConnection con=ADConnection.createConnection();//-----------------------注意这一段-------------------------------------------------------------------------------------------------------- SqlDataAdapter da=new SqlDataAdapter("AXzhz",con); para0=new SqlParameter("@startDate",startDate); para1=new SqlParameter("@endDate",endDate); da.SelectCommand.Parameters.Add(para0); da.SelectCommand.Parameters.Add(para1); da.SelectCommand.CommandType=CommandType.StoredProcedure;//------------------------------------------------------------------------------------------------------------------------------- try { con.Open(); da.Fill(ds); con.Close(); return "OK"; } catch(Exception ex) { return ex.ToString(); } }
注:把命令的参数添加进去,就可以了。
⑦重新验证SQL命令执行是否成功。
CREATE PROCEDURE dbo.AXzhz/* @parameter1 用户名 @parameter2 新密码*/@password nvarchar(20),@userName nvarchar(20)ASdeclare @err0 intupdate WL_user set password=@password where UserName=@userNameset @err0=@@error select @err0 as err0
注:先声明一个整型变量@err0,再给其赋值为@@error(这个是系统自动给出的语句是否执行成功,0为成功,其它为失败),最后通过select把它选择出来。
⑧那怎么从后台获得这个执行成功与否的值:
下面这段代码可以告诉你答案:
public static string GetCustomerCName() { SqlConnection con=ADConnection.createConnection(); SqlCommand cmd=new SqlCommand("AXzhz",con); cmd.CommandType=CommandType.StoredProcedure; para0=new SqlParameter("@startDate","2006-9-10"); para1=new SqlParameter("@endDate","2006-9-20"); da.SelectCommand.Parameters.Add(para0); da.SelectCommand.Parameters.Add(para1); con.Open(); try { Int32 re=(int32)cmd.ExecuteScalar(); con.Close(); if (re==0) return "OK!"; else return "false"; } catch(Exception ex) { con.Close(); return ex.ToString(); } }
注:就是通过SqlCommand的ExecuteScalar()方法取回这个值。
⑨我要根据传入的参数判断执行哪条SQL语句:
ALTER PROCEDURE dbo.selectCustomerCNameCount@customerID intASif @customerID=-1 begin select contentownerid ,userCName,count(*) as countAll from view_usercomment group by contentownerid,userCName order by contentownerid DESC endelse begin select contentownerid ,userCName,count(*) as countAll from view_usercomment where contentownerid=@customerID group by contentownerid,userCName order by contentownerid DESC end
- 存储过程 与 SQL Server语句大比拼
- 存储过程 与 SQL Server语句大比拼
- sql分页存储过程大比拼
- SQL Server 存储过程的分页方案比拼
- SQL Server 存储过程的分页方案比拼
- SQL Server 存储过程的分页方案比拼
- SQL Server 存储过程的分页方案比拼
- SQL Server 存储过程的分页方案比拼
- SQL Server 存储过程的分页方案比拼
- SQL Server 存储过程的分页方案比拼
- SQL Server 存储过程的分页方案比拼
- SQL Server 存储过程的分页方案比拼
- SQL Server 存储过程的分页方案比拼
- SQL Server 存储过程的分页方案比拼
- SQL Server 存储过程的分页方案比拼
- SQL Server 存储过程的分页方案比拼
- SQL Server 存储过程的分页方案比拼
- SQL Server 存储过程的分页方案比拼
- 巧用U盘破除XP管理员密码
- 《深入理解计算机系统》读书笔记(1)
- 推荐10个实用MySQL开发工具精品给大家下载
- 'mysql'不是内部或外部命令,也不是可运行的程序或批处理文件
- 搞笑语录
- 存储过程 与 SQL Server语句大比拼
- SQL Server游标应用实例
- C# 批处理调用方法
- Python中set的用法
- 功率、转矩、转速关系推导
- 回汉,无颜见江东父老
- Finalize 与 Dispose 之间的区别
- .NET FRAMEWORK 正则表达式(例子)
- Spring实战之org.springframework.beans.factory.config.MethodInvokingFactoryBean