通过表明生成存储过程

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

相关文章

C++中使用vector存储并遍历数据的基本步骤

《C++中使用vector存储并遍历数据的基本步骤》C++标准模板库(STL)提供了多种容器类型,包括顺序容器、关联容器、无序关联容器和容器适配器,每种容器都有其特定的用途和特性,:本文主要介绍C... 目录(1)容器及简要描述‌php顺序容器‌‌关联容器‌‌无序关联容器‌(基于哈希表):‌容器适配器‌:(

使用MongoDB进行数据存储的操作流程

《使用MongoDB进行数据存储的操作流程》在现代应用开发中,数据存储是一个至关重要的部分,随着数据量的增大和复杂性的增加,传统的关系型数据库有时难以应对高并发和大数据量的处理需求,MongoDB作为... 目录什么是MongoDB?MongoDB的优势使用MongoDB进行数据存储1. 安装MongoDB

MybatisGenerator文件生成不出对应文件的问题

《MybatisGenerator文件生成不出对应文件的问题》本文介绍了使用MybatisGenerator生成文件时遇到的问题及解决方法,主要步骤包括检查目标表是否存在、是否能连接到数据库、配置生成... 目录MyBATisGenerator 文件生成不出对应文件先在项目结构里引入“targetProje

Python使用qrcode库实现生成二维码的操作指南

《Python使用qrcode库实现生成二维码的操作指南》二维码是一种广泛使用的二维条码,因其高效的数据存储能力和易于扫描的特点,广泛应用于支付、身份验证、营销推广等领域,Pythonqrcode库是... 目录一、安装 python qrcode 库二、基本使用方法1. 生成简单二维码2. 生成带 Log

SpringBoot 整合 Grizzly的过程

《SpringBoot整合Grizzly的过程》Grizzly是一个高性能的、异步的、非阻塞的HTTP服务器框架,它可以与SpringBoot一起提供比传统的Tomcat或Jet... 目录为什么选择 Grizzly?Spring Boot + Grizzly 整合的优势添加依赖自定义 Grizzly 作为

mysql-8.0.30压缩包版安装和配置MySQL环境过程

《mysql-8.0.30压缩包版安装和配置MySQL环境过程》该文章介绍了如何在Windows系统中下载、安装和配置MySQL数据库,包括下载地址、解压文件、创建和配置my.ini文件、设置环境变量... 目录压缩包安装配置下载配置环境变量下载和初始化总结压缩包安装配置下载下载地址:https://d

springboot整合gateway的详细过程

《springboot整合gateway的详细过程》本文介绍了如何配置和使用SpringCloudGateway构建一个API网关,通过实例代码介绍了springboot整合gateway的过程,需要... 目录1. 添加依赖2. 配置网关路由3. 启用Eureka客户端(可选)4. 创建主应用类5. 自定

Python使用Pandas库将Excel数据叠加生成新DataFrame的操作指南

《Python使用Pandas库将Excel数据叠加生成新DataFrame的操作指南》在日常数据处理工作中,我们经常需要将不同Excel文档中的数据整合到一个新的DataFrame中,以便进行进一步... 目录一、准备工作二、读取Excel文件三、数据叠加四、处理重复数据(可选)五、保存新DataFram

SpringBoot生成和操作PDF的代码详解

《SpringBoot生成和操作PDF的代码详解》本文主要介绍了在SpringBoot项目下,通过代码和操作步骤,详细的介绍了如何操作PDF,希望可以帮助到准备通过JAVA操作PDF的你,项目框架用的... 目录本文简介PDF文件简介代码实现PDF操作基于PDF模板生成,并下载完全基于代码生成,并保存合并P

最新版IDEA配置 Tomcat的详细过程

《最新版IDEA配置Tomcat的详细过程》本文介绍如何在IDEA中配置Tomcat服务器,并创建Web项目,首先检查Tomcat是否安装完成,然后在IDEA中创建Web项目并添加Web结构,接着,... 目录配置tomcat第一步,先给项目添加Web结构查看端口号配置tomcat    先检查自己的to