sql生成(c#调用存储过程返回数据表)代码的存储过程

GO
/****** 对象:  StoredProcedure [dbo].[pro_GenerateServiceFunction]    脚本日期: 08/04/2012 11:26:43 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pro_GenerateServiceFunction]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pro_GenerateServiceFunction]
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*****************************************************
** DECRIPTION: 生成(c#调用存储过程返回数据表)代码的存储过程
** VERSION      AUTH          DATE          Defect No			DESC
** --------  ------------  ------------  -----------------   ------------------------------
** V000.0.1    pukuimin     08/04/2012							新建程序
** --------  ------------  ------------  -----------------   -------------------------------
*******************************************************/
CREATE procedure [dbo].[pro_GenerateServiceFunction](
					@ProName NVARCHAR(200), ---存储过程名
					@TableName NVARCHAR(200) ---表名
)
as
begin
DECLARE @SqlParams     VARCHAR(8000)  --生成存储过程参数
DECLARE @ParamValue     VARCHAR(8000) --参数赋值
declare @tempProperty    varchar(200)--临时字段
declare @DATA_TYPE    varchar(200)--临时数据类型
declare @ParamCount int --参数计数器
declare @opr_typstr varchar(20) --
SELECT @SqlParams='',@tempProperty='',@DATA_TYPE='',@ParamCount=0,@ParamValue='',@opr_typstr=''

if isnull(@ProName,'')='' or isnull(@TableName,'')=''
begin
	print '存储过程名或表名不能为空!'
	return 0
end 

 if exists (select * from sys.all_parameters where object_id = object_id(@ProName))
 begin
	select
	@opr_typstr=case when [name]='@opr_typ' and @opr_typstr='' then 'int Opr_typ ,' else @opr_typstr end,
	@DATA_TYPE=type_name(user_type_id),  --sql类型
	@tempProperty=dbo.fun_get_UpperFirst(replace([name],'@','')), --参数
	@SqlParams=@SqlParams+dbo.fun_get_tabspace(3)+'new  SqlParameter("'+[name]+'",'+
	(CASE
		WHEN @DATA_TYPE='NVARCHAR' OR @DATA_TYPE='VARCHAR' OR @DATA_TYPE='CHAR'OR @DATA_TYPE='NCHAR' or @DATA_TYPE='numeric'
		THEN dbo.[fun_get_cssqlpdt_by_sqldt](@DATA_TYPE)+',' +dbo.[fun_get_param_length](@ProName,[name])
		ELSE
        dbo.[fun_get_cssqlpdt_by_sqldt](@DATA_TYPE)
		END)+'),'+CHAR(10),
	@ParamValue=@ParamValue+dbo.fun_get_tabspace(3)+
    (CASE
    when  [name]='@opr_typ' then 'paras['+cast(@ParamCount as varchar(20))+'].Value = '+@tempProperty+';'
	WHEN @DATA_TYPE='NVARCHAR' OR @DATA_TYPE='VARCHAR' OR @DATA_TYPE='CHAR' OR @DATA_TYPE='NCHAR' OR @DATA_TYPE='NTEXT' OR @DATA_TYPE='TEXT' OR @DATA_TYPE='OUT' or @DATA_TYPE='uniqueidentifier' or @DATA_TYPE='image' or @DATA_TYPE='variant'
    THEN 'paras['+cast(@ParamCount as varchar(20))+'].Value = model.'+@tempProperty+';'
    ELSE
		'if (model.'+@tempProperty+'.Equals('+dbo.[fun_get_cssdt_by_sqldt](@DATA_TYPE)+'.MinValue))'+
		CHAR(10)+dbo.fun_get_tabspace(3)+'{'+
		CHAR(10)+dbo.fun_get_tabspace(4)+'paras['+cast(@ParamCount as varchar(20))+'].Value = null;'+
		CHAR(10)+dbo.fun_get_tabspace(3)+'}'+
		CHAR(10)+dbo.fun_get_tabspace(3)+'else'+
		CHAR(10)+dbo.fun_get_tabspace(3)+'{'+
		CHAR(10)+dbo.fun_get_tabspace(4)+'paras['+cast(@ParamCount as varchar(20))+'].Value = model.'+@tempProperty+';'+
		CHAR(10)+dbo.fun_get_tabspace(3)+'}'
    END)+CHAR(10),
	@ParamCount=@ParamCount+1
	from sys.all_parameters where object_id = object_id(@ProName)
	set @SqlParams=LEFT(@SqlParams,LEN(@SqlParams)-2)
	set @ParamValue=LEFT(@ParamValue,LEN(@ParamValue)-1)

 end
else
	begin
		print '没有此存储过程!'
		return 0
	end

print dbo.fun_get_tabspace(2)+'#region 执行存储过程'+@ProName+'的函数'
print dbo.fun_get_tabspace(2)+'/// <summary>'
print dbo.fun_get_tabspace(2)+'/// 执行存储过程'+@ProName
print dbo.fun_get_tabspace(2)+'/// <summary>'
if @opr_typstr <>''
begin
	print dbo.fun_get_tabspace(2)+'/// <param name="Opr_typ"> 操作类型,1:新增 2:修改,3 删除 </param>'
end
print dbo.fun_get_tabspace(2)+'/// <param name="model">'+@TableName+'对应的model对象 </param>'
print dbo.fun_get_tabspace(2)+'public object Run'+@ProName+'('+@opr_typstr+@TableName+'Model model)'  --
print dbo.fun_get_tabspace(2)+'{'
print dbo.fun_get_tabspace(3)+'object result=new object();'
print dbo.fun_get_tabspace(3)+'SqlParameter[] paras = new SqlParameter[]'
print dbo.fun_get_tabspace(3)+'{'
print @SqlParams
print dbo.fun_get_tabspace(3)+'};'
print @ParamValue
print dbo.fun_get_tabspace(3)+'try'
print dbo.fun_get_tabspace(3)+'{'
print dbo.fun_get_tabspace(4)+'result = new DbHelper().ExecProDataTable("'+@ProName+'", paras);'
print dbo.fun_get_tabspace(4)+''
print dbo.fun_get_tabspace(3)+'}'
print dbo.fun_get_tabspace(3)+'catch (SqlException ex)'
print dbo.fun_get_tabspace(3)+'{'
print dbo.fun_get_tabspace(4)+'result = null;'
print dbo.fun_get_tabspace(4)+'throw new Exception("数据库操作异常", ex);'
print dbo.fun_get_tabspace(3)+'}'
print dbo.fun_get_tabspace(3)+'return result;'
print dbo.fun_get_tabspace(2)+'}'
print dbo.fun_get_tabspace(2)+'#endregion'

end
/*

exec [pro_GenerateServiceFunction] 'pro_get_Stuinfo','stuinfo'

*/
时间: 2024-08-17 18:22:50

sql生成(c#调用存储过程返回数据表)代码的存储过程的相关文章

link中如何编写中间件,中间件如何访问数据,又怎么返回数据?代码怎么写?

问题描述 link中如何编写中间件,中间件如何访问数据,又怎么返回数据?代码怎么写? link中如何编写中间件,中间件如何访问数据,又怎么返回数据?代码怎么写? 解决方案 参考我在http://ask.csdn.net/questions/169089 的回答

jsp修改mysql数据表代码

jsp修改mysql数据表代码  function repair($tables,$operation)  {   $tables = is_array($tables) ? implode(',',$tables) : $tables;   if($tables && in_array($operation,array('repair','optimize')))   {    $this->db->query("$operation TABLE $tables&q

SQL存储过程中调用存储过程返回的表

      存储过程pro_A返回一个表集合TableA,在存储过程pro_B中以TableA为数据源再做一重过滤,想实现代码复用,因此原意是在存储过程pro_B中调用存储过程pro_A,但事实发现我们不能写成select * from (exec pro_A para1,para2) a ;虽然返回结果是表集合. 后来想执行存储过程返回一个output表变量,就像一般变量一样,但存储过程可不支持这种写法:想到最后只能将pro_A 的内容写在一个函数中fn_A,函数支持返回表,可以写成selec

用SQL语句获得一个存储过程返回的表

定义一个存储过程如下: create proc [dbo].[test1]@id intasselect 1 as id,'abc' as name union all select @id as id,'zzz' as name 返回两行数据.现在想用SQL语句来调用这个存储过程,并把他返回的表放入变量中.可以如下做: declare   @table   table(id   int,name varchar(50))--定义表变量来存放存储过程返回的内容insert  into @tabl

利用SQL *Plus复制遗留的Oracle数据表

你可能运行的是Oracle 10g Release 2数据库服务器,但是需要支持某些应用程序可能是很早之前写好的.在Oracle 8i之前的版本创建的数据表常用LONG数据类型来存储大型文本. 随着Large Object(LOB)数据类型的引入,LONG和LONG RAW数据类型就被抛弃了.如果你要复制使用了LONG数据类型的数据表,那么使用CREATE TABLE AS SELECT语法将不能完成任务,这时将会返回错误:ORA-00997: illegal use of LONG 数据类型.

SQL Server“偷懒”也能更新数据表

问:请专家指教!问题是这样的:我这里有1000个数据表,每个表的结构一模一样(每个表里都有"qq,tj,yj,ej,sj,sij,wj,lj,zs,zjl"10个字段),只是表名不一样.还有一个"数据更新表jj(table_index,qq,tj,yj,ej,sj,sij,wj,lj,zs,zjl)",除了table_index字段外,同样有"qq,tj,yj,ej,sj,sij,wj,lj,zs,zjl"10个字段,有1000行,该表的用途是用

SQL Server分布式分区视图分解数据表

分布式分区视图可以将来自一个或多个SQL Server数据库中的数据连接起来.当开发一个水平分区数据库环境时,你可以使用分布式分区视图将来自不同服务器的分区表连接起来,使得这些数据看起来像来自同一个服务器. 你可以设计这些视图,因此,如果你的潜在数据表结构设计合理的话,查询优化器就可以知道从那个数据表得到查询需要的数据,从而加速运行.一个设计合理的分布式分区视图还可以实现更新.插入和删除.我们将在本文的下一部分深入探讨它是如何实现这样操作的. 示例 本例中,我们假设SalesHistory表非常

Jquery Ajax学习实例3 向WebService发出请求,调用方法返回数据_jquery

一.WebService.asmx 处理业务数据,在GetWhether方法中产生天气情况数据,供JqueryRequest.aspx调用,代码如下: 复制代码 代码如下: [System.Web.Script.Services.ScriptService] public class WebService : System.Web.Services.WebService { public WebService () { //如果使用设计的组件,请取消注释以下行 //InitializeCompo

PHP生成和获取XML格式数据实现代码

生成XML格式数据 我们假设系统中有一张学生信息表student,需要提供给第三方调用,并有id,name,sex,age分别记录学生的姓名.性别.年龄等信息.  代码如下 复制代码 CREATE TABLE `student` (    `id` int(11) NOT NULL auto_increment,    `name` varchar(50) NOT NULL,    `sex` varchar(10) NOT NULL,    `age` smallint(3) NOT NULL