sql server一个存储过程

一段C#版 dapper调用存储过程的代码示例

 DynamicParameters dy = new DynamicParameters();
 dy.Add("IdStr", "1,2,3,4,5,6,7");
 var aa = conn.Query<dynamic>("MyProcedureNameGetNodesInfo", dy, commandType: CommandType.StoredProcedure);

对以上代码片段的备注:

  1. conn就是这个连接本身,使用dapper的人应该不需要解释这个。
  2. 调用了存储过程,也不见得一定要是Execute,也可以是Query。
    尽管实际上一般Execute方式调用的存储过程我用来处理业务为主,最多返回一点点信息,通过out参数什么的。
    一般Query方式调用的存储过程,才是我期望多返回信息,如返回结果集这样的情况。

一段sql server 2008 存储过程的代码示例

USE [XueTianTest.local]
GO
/****** Object:  StoredProcedure [dbo].[MyProcedureNameGetNodesInfo]    Script Date: 06/28/2019 11:05:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Batch submitted through debugger: my_proc.sql|7|0|C:\Users\Administrator\Documents\SQL Server Management Studio\Projects\my_proc.sql
-- Batch submitted through debugger: SQLQuery33.sql|7|0|C:\Users\Administrator\AppData\Local\Temp\~vsE53C.sql
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[MyProcedureNameGetNodesInfo]
	-- Add the parameters for the stored procedure here
	@IdStr nvarchar(1000)
	
AS
BEGIN
	
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	
	Declare @idSqlStr nvarchar(1000);
	Declare @tempId int;
	Declare @resultSql nvarchar(1000);
	
	declare @a int,@error int    
	set @a=1
    set @error=0
    
    set @IdStr = '1,2,3,4,5,6,7,8,9'
    
    -- 临时保存传入的节点id的表
    IF EXISTS(Select 1 From Sysobjects Where Name='temp_table_NodeIdTable')  
	DROP table #temp_table_NodeIdTable     
	create Table #temp_table_NodeIdTable( Id int );
    
	--保存将要输出的内容的表
    IF EXISTS(Select 1 From Sysobjects Where Name='temp_table_NodeInfoTable')  
	DROP table #temp_table_NodeInfoTable     
	create Table #temp_table_NodeInfoTable(
		Id int,
		Name varchar(100)
	);
		
	set @idSqlStr =  'select col='''+ replace(@IdStr,',',''' union all select ''')+''''
	
	insert into #temp_table_NodeIdTable  exec(@idSqlStr);   --这一句究竟行不行,一度让我很为难很难处理,又难以调试
	
	declare ids_cursor cursor for select * from #temp_table_NodeIdTable;	
	
	print @IdStr;
		
	BEGIN			
		open ids_cursor
			fetch next from ids_cursor into @tempId
			 while @@FETCH_STATUS = 0    --返回被 FETCH语句执行的最后游标的状态--
			 BEGIN
				insert into #temp_table_NodeInfoTable(Id,Name) values(@tempId, GETDATE());
			 
				set @a=@a+1
				set @error= @error + @@ERROR   --记录每次运行sql后是否正确,0正确
				
				fetch next from ids_cursor into @tempId   --转到下一个游标,没有会死循环
			 END
		 close ids_cursor  --关闭游标
	END
		
	SELECT * from #temp_table_NodeInfoTable;
END

发布了177 篇原创文章 · 获赞 47 · 访问量 43万+

猜你喜欢

转载自blog.csdn.net/festone000/article/details/93978542
今日推荐