扩展sp_MSforeach
浅析SQL SERVER一个没有公开的存储过程中介绍了sp_MSforeachtable的工作方式和工作原理。不过令人失望的是它们不能扩展到其它的对象(比如视图、存储过程、触发器等)。那我们就自己对手来完全扩展这些存储过程。
USE MASTER
GO
if exists (select name from sysobjects
where name = 'sp_MSforeachsproc' AND type = 'P')
drop procedure sp_MSforeachsproc
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
create proc sp_MSforeachsproc
@command1 nvarchar(2000), @replacechar nchar(1) = N'?', @command2 nvarchar(2000) = null,
@command3 nvarchar(2000) = null, @whereand nvarchar(2000) = null,
@precommand nvarchar(2000) = null, @postcommand nvarchar(2000) = null
as
/**//* This proc returns one or more rows for each table (optionally, matching @where), with each table defaulting to its own result set */
/**//* @precommand and @postcommand may be used to force a single result set via a temp table. */
/**//* Preprocessor won't replace within quotes so have to use str(). */
declare @mscat nvarchar(12)
select @mscat = ltrim(str(convert(int, 0x0002)))
if (@precommand is not null)
exec(@precommand)
/**//* Create the select */
exec(N'declare hCForEach cursor global for select ''['' + REPLACE(user_name(uid), N'']'', N'']]'') + '']'' + ''.'' + ''['' + REPLACE(object_name(id), N'']'', N'']]'') + '']'' from dbo.sysobjects o '
+ N' where OBJECTPROPERTY(o.id, N''IsProcedure'') = 1 ' + N' and o.category & ' + @mscat + N' = 0 '
+ @whereand)
declare @retval int
select @retval = @@error
if (@retval = 0)
exec @retval = sp_MSforeach_worker @command1, @replacechar, @command2, @command3
if (@retval = 0 and @postcommand is not null)
exec(@postcommand)
return @retval
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
USE MASTER
GO
if exists (select name from sysobjects
where name = 'sp_MSforeachtrigger' AND type = 'P')
drop procedure sp_MSforeachtrigger
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
create proc sp_MSforeachtrigger
@command1 nvarchar(2000), @replacechar nchar(1) = N'?', @command2 nvarchar(2000) = null,
@command3 nvarchar(2000) = null, @whereand nvarchar(2000) = null,
@precommand nvarchar(2000) = null, @postcommand nvarchar(2000) = null
as
/**//* This proc returns one or more rows for each table (optionally, matching @where), with each table defaulting to its own result set */
/**//* @precommand and @postcommand may be used to force a single result set via a temp table. */
/**//* Preprocessor won't replace within quotes so have to use str(). */
declare @mscat nvarchar(12)
select @mscat = ltrim(str(convert(int, 0x0002)))
if (@precommand is not null)
exec(@precommand)
/**//* Create the select */
exec(N'declare hCForEach cursor global for select ''['' + REPLACE(user_name(uid), N'']'', N'']]'') + '']'' + ''.'' + ''['' + REPLACE(object_name(id), N'']'', N'']]'') + '']'' from dbo.sysobjects o '
+ N' where OBJECTPROPERTY(o.id, N''IsTrigger'') = 1 ' + N' and o.category & ' + @mscat + N' = 0 '
+ @whereand)
declare @retval int
select @retval = @@error
if (@retval = 0)
exec @retval = sp_MSforeach_worker @command1, @replacechar, @command2, @command3
if (@retval = 0 and @postcommand is not null)
exec(@postcommand)
return @retval
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
USE MASTER
GO
if exists (select name from sysobjects
where name = 'sp_MSforeachview' AND type = 'P')
drop procedure sp_MSforeachview
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
create proc sp_MSforeachview
@command1 nvarchar(2000), @replacechar nchar(1) = N'?', @command2 nvarchar(2000) = null,
@command3 nvarchar(2000) = null, @whereand nvarchar(2000) = null,
@precommand nvarchar(2000) = null, @postcommand nvarchar(2000) = null
as
/**//* This proc returns one or more rows for each table (optionally, matching @where), with each table defaulting to its own result set */
/**//* @precommand and @postcommand may be used to force a single result set via a temp table. */
/**//* Preprocessor won't replace within quotes so have to use str(). */
declare @mscat nvarchar(12)
select @mscat = ltrim(str(convert(int, 0x0002)))
if (@precommand is not null)
exec(@precommand)
/**//* Create the select */
exec(N'declare hCForEach cursor global for select ''['' + REPLACE(user_name(uid), N'']'', N'']]'') + '']'' + ''.'' + ''['' + REPLACE(object_name(id), N'']'', N'']]'') + '']'' from dbo.sysobjects o '
+ N' where OBJECTPROPERTY(o.id, N''IsView'') = 1 ' + N' and o.category & ' + @mscat + N' = 0 '
+ @whereand)
declare @retval int
select @retval = @@error
if (@retval = 0)
exec @retval = sp_MSforeach_worker @command1, @replacechar, @command2, @command3
if (@retval = 0 and @postcommand is not null)
exec(@postcommand)
return @retval
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
USE MASTER
GO
if exists (select name from sysobjects
where name = 'sp_MSforeachfk' AND type = 'P')
drop procedure sp_MSforeachfk
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
create proc sp_MSforeachfk
@command1 nvarchar(2000), @replacechar nchar(1) = N'?', @command2 nvarchar(2000) = null,
@command3 nvarchar(2000) = null, @whereand nvarchar(2000) = null,
@precommand nvarchar(2000) = null, @postcommand nvarchar(2000) = null
as
/**//* This proc returns one or more rows for each table (optionally, matching @where), with each table defaulting to its own result set */
/**//* @precommand and @postcommand may be used to force a single result set via a temp table. */
/**//* Preprocessor won't replace within quotes so have to use str(). */
declare @mscat nvarchar(12)
select @mscat = ltrim(str(convert(int, 0x0002)))
if (@precommand is not null)
exec(@precommand)
/**//* Create the select */
exec(N'declare hCForEach cursor global for select ''['' + REPLACE(user_name(uid), N'']'', N'']]'') + '']'' + ''.'' + ''['' + REPLACE(object_name(id), N'']'', N'']]'') + '']'' from dbo.sysobjects o '
+ N' where OBJECTPROPERTY(o.id, N''IsForeignKey'') = 1 ' + N' and o.category & ' + @mscat + N' = 0 '
+ @whereand)
declare @retval int
select @retval = @@error
if (@retval = 0)
exec @retval = sp_MSforeach_worker @command1, @replacechar, @command2, @command3
if (@retval = 0 and @postcommand is not null)
exec(@postcommand)
return @retval
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
扩展sp_MSforeach相关推荐
- VS Code 安装 Go 插件、自定义扩展配置、断点调试
1. 安装插件 使用快捷键 Ctrl+Shift+X 打开插件安装页面,安装 Go 插件. 2. 自定义扩展配置 使用快捷键 Ctrl+, 打开自定义配置页,编辑 settings.json ,定义与 ...
- VS Code 安装插件、自定义模板、自定义配置参数、自定义主题、配置参数说明、常用的扩展插件
1. 下载和官网教程 下载地址:https://code.visualstudio.com/ 官方教程:https://code.visualstudio.com/docs 2. 安装插件 安装扩展插 ...
- gcc 自动识别的文件扩展名,gcc/g++ -x 选项指定语言,不同 gcc 版本 -std 编译选项支持列表
对于执行 C 或者 C++ 程序,需要借助 gcc(g++)指令来调用 GCC 编译器. 对于以 .c 为扩展名的文件,GCC 会自动将其视为 C 源代码文件 对于以 .cpp 为扩展名的文件,GCC ...
- 用动态实现扩展TVM
用动态实现扩展TVM Extending TVM with Dynamic Execution Outline ● Motivation for Dynamism ● Representing Dyn ...
- TVM apps extension示例扩展库
TVM apps extension示例扩展库 此文件夹包含TVM的示例扩展库.演示了其它库如何在C++和Python API中扩展TVM. 该库扩展了TVM的功能. python模块加载新的共享库, ...
- Python 扩展 Op
Python 扩展 Op 注意 :本文涉及的 Python Kernel 仅在 gcc 4.8.5 编译环境下充分测试,进一步的完善计划见 Issue 3951. 背景介绍 OneFlow 将各种对于 ...
- TensorFlow常用Python扩展包
TensorFlow常用Python扩展包 TensorFlow 能够实现大部分神经网络的功能.但是,这还是不够的.对于预处理任务.序列化甚至绘图任务,还需要更多的 Python 包. 下面列出了一些 ...
- 部署可扩展的目标检测管道:推理过程(下)
部署可扩展的目标检测管道:推理过程(下) 融合 感兴趣的目标可以被遮挡.有时只能看到目标的一小部分(少至几个像素). • 图19.车辆和交通信号灯被遮挡. • 图20:阻塞了总线. • 图21:左侧的 ...
- 部署可扩展的目标检测管道:推理过程(上)
部署可扩展的目标检测管道:推理过程(上) 基于YOLOv3的目标检测推理过程的所有代码都可以在eriklindernoren/PyTorch-YOLOv3 GitHub repo找到. 为了进行审查, ...
最新文章
- [转]oracle 存储过程的基本语法 及注意事项
- Unity3D中使用KiiCloud总结一
- 【Linux】一步一步学Linux——whereis命令(15)
- Marketing Cloud tile的semantic信息
- 电路设计之干扰问题总结与分析
- 美团无人配送CVPR2020论文CenterMask解读
- vue 功能模块后台可配置_Github14k的Springboot后台管理系统
- 揭秘世界首位机器人公民:按照赫本形象设计、曾扬言毁灭人类
- 计算机一级在线练习,计算机一级练习系统
- DBSCAN(自适应密度聚类)算法解析
- 蚂蚁金服井贤栋:用技术联手金融机构,形成服务小微的生态合力
- 基于PHP图书馆图书借阅管理系统
- 1G PHP免费空间
- 62_LP-3DCNN: Unveiling Local Phase in 3D Convolutional Neural Networks 2019 论文笔记
- QT程序到arm板(s5pv210)的移植之旅
- python爬虫实验总结_python3爬虫总结(共4篇).docx
- 正常人肺动脉内皮细胞 Pulmonary artery endothelial cells
- 迪赛推出国家公立医院绩效考核服务系统,融合迪赛智慧数加持赋能,让二三级医院用户领跑“国考”!
- 杠杆平台的炒股技巧是什么?
- APEX V2.0 - ANGULAR 5+ BOOTSTRAP 4 HTML ADMIN TEMPLATE启动报错解决办法