通过表明生成存储过程

2024-02-09 13:08
文章标签 生成 过程 存储 表明

本文主要是介绍通过表明生成存储过程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

/*
调用示例:
cp t_goods
生成常用存储过程
*/
ALTER        procedure cp
@tablename varchar(50) = '' -- 表名称
as    
declare @tableid int     
set @tableid = object_id(@tablename)
if @tableid is null 
begin    
select 'table not exists' as error
return    
end
declare @tmptb table(code varchar(4000))
declare @fieldsName varchar(4000)
declare @parmName varchar(4000)
declare @keyname varchar(50)
declare @proctablename varchar(50)
declare @ident_seed int
select @ident_seed = ident_seed(@tablename)
if(@ident_seed is null)
set @ident_seed = -1
set @proctablename = @tablename 
if( left(@proctablename,2) ='t_')
set @proctablename = substring(@proctablename,3,len(@proctablename) - 2)
print @proctablename
select top 1 @keyname= [name] from syscolumns where id = @tableid order by colid 
set @fieldsname = ''
select @fieldsname = @fieldsname + a.[name] + ',' 
from syscolumns a inner join systypes b on a.xtype = b.xtype
where id = @tableid and a.colid <> @ident_seed
and b.name <> 'datetime' and b.name <> 'smalldatetime'
order by colid
if( right(@fieldsname,1) = ',' )
set @fieldsname = substring(@fieldsname,1,len(@fieldsname) - 1)
set @parmname = ''
select @parmname = @parmname + '@' + a.[name] + ',' 
from syscolumns a inner join systypes b on a.xtype = b.xtype
where id = @tableid and a.colid <> @ident_seed
and b.name <> 'datetime' and b.name <> 'smalldatetime'
order by colid
if( right(@parmname,1) = ',' )
set @parmname = substring(@parmname,1,len(@parmname) - 1)
-- proc_pagelist1
delete from @tmptb
insert into @tmptb select 'create procedure p_' + @proctablename + '_list'
insert into @tmptb 
select '@page int = 1, --当前页' union all
select '@pagesize int = 10, --每页显示' union all
select '@total int = 0 output, --总记录数' union all
select '@typeid int = 0 ''这里需要修改''' union all
select 'as' union all
select 'set nocount on' union all
select 'set transaction isolation level read uncommitted' union all
select 'set xact_abort on' union all
select ''
insert into @tmptb 
select 'if( @total = 0 )' union all
select '    select @total = count(1) from ' + @tablename + ' where typeid = @typeid ''这里需要修改''' union all
select '' union all
select 'if( @page = 1 )' union all
select 'begin' union all
select '    set rowcount @pagesize' union all
select '    select * from ' + @tablename + ' where typeid = @typeid ''这里需要修改'' order by ' + @keyname +  ' desc ' union all
select '    set rowcount 0' union all
select '    return' union all
select 'end' union all
select 'else' union all
select 'begin' union all
select '    declare @tb table(rownum int identity(1,1),keyid int)' union all
select '    insert into @tb(keyid) select ' + @keyname + ' from ' + @tablename union all
select '        where typeid = @typeid ''这里需要修改'' order by ' + @keyname +  ' desc' union all
select '    select a.* from ' + @tablename + ' a inner join @tb b on a.'+ @keyname +  ' = b.keyid ' union all
select '        where b.rownum > @pagesize * (@page - 1) and b.rownum <= @pagesize * @page' union all
select 'end'
insert into @tmptb select '' union all select 'go'
select code as proc_pagelist1 from @tmptb
-- proc_pagelist2
delete from @tmptb
insert into @tmptb select 'create procedure p_' + @proctablename + '_list'
insert into @tmptb 
select '@page int = 1, --当前页' union all
select '@pagesize int = 10, --每页显示' union all
select '@total int = 0 output, --总记录数' union all
select '@typeid int = 0 ''这里需要修改''' union all
select 'as' union all
select 'set nocount on' union all
select 'set transaction isolation level read uncommitted' union all
select 'set xact_abort on' union all
select ''
insert into @tmptb 
select 'declare @tablename varchar(1024)' union all           
select 'declare @orderby  varchar(1024)' union all      
select 'declare @orderby2 varchar(1024)' union all      
select 'declare @orderbyclause varchar(1024)' union all      
select 'declare @keyname varchar(50)' union all      
select 'declare @keyinorder tinyint' union all
select 'declare @whereclause varchar(1024)' union all      
select 'declare @showclause varchar(1024)' union all    
select '' union all        
select '-- 表名称' union all
select 'set @tablename = ''' + @tablename + ''' ' union all           
select '--排序字段,字段前面加b.,字段后加 desc或asc,格式如 b.rid desc,b.rname asc' union all
select 'set @orderby = ''b.' + @keyname + ' desc'' ' union all           
select '--排序字段,字段前面加b.,字段后面和上面的orderby正好相反,格式如 b.rid asc,b.ranme desc' union all
select 'set @orderby2 = ''b.' + @keyname + ' asc'' '  union all
select '--排序字段,这里不需要加 asc 和 desc,格式如 b.rid,b.rname' union all
select 'set @orderbyclause = ''b.' + @keyname + '''' union all
select '--主键是否在排序字段里,1 是 0 不是,一般是1' union all
select 'set @keyinorder = 1'  union all           
select '--这个表的主键' union all
select 'set @keyname = ''' + @keyname +  ''''  union all           
select '--查询条件' union all
select 'set @whereclause = '' where typeid = '' + convert(varchar,@typeid) ' + '''这里需要修改'''  union all           
select 'set @showclause = ''a.*'''   union all             
select '' union all
select '--调用分页存储过程' union all
select 'exec P_SplitPageOneSql_v2 @tablename,@pagesize,@page,@orderby,@orderby2,' union all           
select '@orderbyclause,@keyname,@keyinorder,@whereclause,@showclause,@total output'
insert into @tmptb select '' union all select 'go'
select code as proc_pagelist2 from @tmptb
-- proc_insert
delete from @tmptb
insert into @tmptb select 'create procedure p_' + @proctablename + '_insert'
insert into @tmptb 
select 
'@' + a.name + ' ' + 
case b.name 
when 'char' then 'char(' + convert(varchar,a.length) + ') = '''','    
when 'nchar' then 'nchar(' + convert(varchar,a.length) + ')= '''','    
when 'varchar' then 'varchar(' + convert(varchar,a.length) + ') = '''','    
when 'nvarchar' then 'nvarchar(' + convert(varchar,a.length) + ') = '''','
when 'datetime' then 'datetime,'
when 'smalldatetime' then 'smalldatetime,'
else b.name + ' = 0,' end
from syscolumns a inner join systypes b    
on a.xtype = b.xtype     
where a.id = object_id(@tablename) 
and a.colid <> @ident_seed
and b.name <> 'datetime' and b.name <> 'smalldatetime'
order by colid 
insert into @tmptb 
select 'as' union all
select 'set nocount on' union all
select 'set transaction isolation level read uncommitted' union all
select 'set xact_abort on' union all
select ''
insert into @tmptb 
select 'insert into ' + @tablename + '(' + @fieldsname + ')' union all
select '    values(' + @parmname + ')'
insert into @tmptb select '' union all select 'go'
select code as proc_insert from @tmptb
-- proc_update
delete from @tmptb
insert into @tmptb select 'create procedure p_' + @proctablename + '_update'
insert into @tmptb 
select 
'@' + a.name + ' ' + 
case b.name 
when 'char' then 'char(' + convert(varchar,a.length) + ') = '''','    
when 'nchar' then 'nchar(' + convert(varchar,a.length) + ')= '''','    
when 'varchar' then 'varchar(' + convert(varchar,a.length) + ') = '''','    
when 'nvarchar' then 'nvarchar(' + convert(varchar,a.length) + ') = '''','
when 'datetime' then 'datetime,'
when 'smalldatetime' then 'smalldatetime,'
else b.name + ' = 0,' end
from syscolumns a inner join systypes b    
on a.xtype = b.xtype     
where a.id = object_id(@tablename)    
and b.name <> 'datetime' and b.name <> 'smalldatetime'
order by colid 
insert into @tmptb 
select 'as' union all
select 'set nocount on' union all
select 'set transaction isolation level read uncommitted' union all
select 'set xact_abort on' union all
select ''
set @fieldsname = ''
select @fieldsname = @fieldsname + a.[name] + '=' + '@' + a.[name] + ',' 
from syscolumns a inner join systypes b on a.xtype = b.xtype
where id = @tableid and a.colid <> @ident_seed
and b.name <> 'datetime' and b.name <> 'smalldatetime'
order by colid
if( right(@fieldsname,1) = ',' )
set @fieldsname = substring(@fieldsname,1,len(@fieldsname) - 1)
insert into @tmptb 
select 'update ' + @tablename union all
select ' set ' + @fieldsname  union all
select ' where ' + @keyname + '=@' + @keyname
insert into @tmptb select '' union all select 'go'
select code as proc_update from @tmptb
-- proc_delete
delete from @tmptb
insert into @tmptb select 'create procedure p_' + @proctablename + '_delete'
insert into @tmptb 
select top 1
'@' + a.name + ' ' + 
case b.name 
when 'char' then 'char(' + convert(varchar,a.length) + ') = '''''    
when 'nchar' then 'nchar(' + convert(varchar,a.length) + ')= '''''    
when 'varchar' then 'varchar(' + convert(varchar,a.length) + ') = '''''    
when 'nvarchar' then 'nvarchar(' + convert(varchar,a.length) + ') = '''''
else b.name + ' = 0' end
from syscolumns a inner join systypes b    
on a.xtype = b.xtype     
where a.id = object_id(@tablename)    
order by a.colid 
insert into @tmptb 
select 'as' union all
select 'set nocount on' union all
select 'set transaction isolation level read uncommitted' union all
select 'set xact_abort on' union all
select ''
insert into @tmptb select 'delete from ' + @tablename + ' where ' + @keyname + '=@' + @keyname
insert into @tmptb select '' union all select 'go'
select code as proc_delete from @tmptb
-- proc_detail
delete from @tmptb
insert into @tmptb select 'create procedure p_' + @proctablename + '_detail'
insert into @tmptb 
select top 1
'@' + a.name + ' ' + 
case b.name 
when 'char' then 'char(' + convert(varchar,a.length) + ') = '''''    
when 'nchar' then 'nchar(' + convert(varchar,a.length) + ')= '''''    
when 'varchar' then 'varchar(' + convert(varchar,a.length) + ') = '''''    
when 'nvarchar' then 'nvarchar(' + convert(varchar,a.length) + ') = '''''
else b.name + ' = 0' end
from syscolumns a inner join systypes b    
on a.xtype = b.xtype     
where a.id = object_id(@tablename)    
order by a.colid 
insert into @tmptb 
select 'as' union all
select 'set nocount on' union all
select 'set transaction isolation level read uncommitted' union all
select 'set xact_abort on' union all
select ''
insert into @tmptb select 'select * from ' + @tablename + ' where ' + @keyname + '=@' + @keyname
insert into @tmptb select '' union all select 'go'
select code as proc_detail from @tmptb
-- proc_listall
delete from @tmptb
insert into @tmptb select 'create procedure p_' + @proctablename + '_listall'
insert into @tmptb 
select 'as' union all
select 'set nocount on' union all
select 'set transaction isolation level read uncommitted' union all
select 'set xact_abort on' union all
select ''
insert into @tmptb 
select 'select * from ' + @tablename 
insert into @tmptb select '' union all select 'go'
select code as proc_listall from @tmptb
go

这篇关于通过表明生成存储过程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/694314

相关文章

Python如何在Word中生成多种不同类型的图表

《Python如何在Word中生成多种不同类型的图表》Word文档中插入图表不仅能直观呈现数据,还能提升文档的可读性和专业性,本文将介绍如何使用Python在Word文档中创建和自定义各种图表,需要的... 目录在Word中创建柱形图在Word中创建条形图在Word中创建折线图在Word中创建饼图在Word

Spring Security注解方式权限控制过程

《SpringSecurity注解方式权限控制过程》:本文主要介绍SpringSecurity注解方式权限控制过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、摘要二、实现步骤2.1 在配置类中添加权限注解的支持2.2 创建Controller类2.3 Us

MySQL常见的存储引擎和区别说明

《MySQL常见的存储引擎和区别说明》MySQL支持多种存储引擎,如InnoDB、MyISAM、MEMORY、Archive、CSV和Blackhole,每种引擎有其特点和适用场景,选择存储引擎时需根... 目录mysql常见的存储引擎和区别说明1. InnoDB2. MyISAM3. MEMORY4. A

Spring AI集成DeepSeek三步搞定Java智能应用的详细过程

《SpringAI集成DeepSeek三步搞定Java智能应用的详细过程》本文介绍了如何使用SpringAI集成DeepSeek,一个国内顶尖的多模态大模型,SpringAI提供了一套统一的接口,简... 目录DeepSeek 介绍Spring AI 是什么?Spring AI 的主要功能包括1、环境准备2

SpringBoot集成图片验证码框架easy-captcha的详细过程

《SpringBoot集成图片验证码框架easy-captcha的详细过程》本文介绍了如何将Easy-Captcha框架集成到SpringBoot项目中,实现图片验证码功能,Easy-Captcha是... 目录SpringBoot集成图片验证码框架easy-captcha一、引言二、依赖三、代码1. Ea

Golang基于内存的键值存储缓存库go-cache

《Golang基于内存的键值存储缓存库go-cache》go-cache是一个内存中的key:valuestore/cache库,适用于单机应用程序,本文主要介绍了Golang基于内存的键值存储缓存库... 目录文档安装方法示例1示例2使用注意点优点缺点go-cache 和 Redis 缓存对比1)功能特性

nginx生成自签名SSL证书配置HTTPS的实现

《nginx生成自签名SSL证书配置HTTPS的实现》本文主要介绍在Nginx中生成自签名SSL证书并配置HTTPS,包括安装Nginx、创建证书、配置证书以及测试访问,具有一定的参考价值,感兴趣的可... 目录一、安装nginx二、创建证书三、配置证书并验证四、测试一、安装nginxnginx必须有"-

pycharm远程连接服务器运行pytorch的过程详解

《pycharm远程连接服务器运行pytorch的过程详解》:本文主要介绍在Linux环境下使用Anaconda管理不同版本的Python环境,并通过PyCharm远程连接服务器来运行PyTorc... 目录linux部署pytorch背景介绍Anaconda安装Linux安装pytorch虚拟环境安装cu

SpringBoot项目注入 traceId 追踪整个请求的日志链路(过程详解)

《SpringBoot项目注入traceId追踪整个请求的日志链路(过程详解)》本文介绍了如何在单体SpringBoot项目中通过手动实现过滤器或拦截器来注入traceId,以追踪整个请求的日志链... SpringBoot项目注入 traceId 来追踪整个请求的日志链路,有了 traceId, 我们在排

Java实战之利用POI生成Excel图表

《Java实战之利用POI生成Excel图表》ApachePOI是Java生态中处理Office文档的核心工具,这篇文章主要为大家详细介绍了如何在Excel中创建折线图,柱状图,饼图等常见图表,需要的... 目录一、环境配置与依赖管理二、数据源准备与工作表构建三、图表生成核心步骤1. 折线图(Line Ch