如何同时对多个表或列操作
类别: 数据库教程
通过使用这个存储过程,你就可以方便的对数据库中具有一定规则的或者全部表,对这里的字段进行各种操作,具体看示例!
CREATE PROCEDURE SP_execSQLonDB
(@TABLENAME VARCHAR(50), --表名条件
@COLUMNNAME VARCHAR(50), --字段条件
@SQL NVARCHAR(4000), --执行的SQL
@INCLUDE_NTI CHAR(1) = ’N’) --是否包含Text,NText,Image数据类型
AS
BEGIN
--Variable Declaration
--变量定义
DECLARE @strSQL NVARCHAR(4000)
DECLARE @SQL2 NVARCHAR(4000)
DECLARE @sTableName VARCHAR(200)
DECLARE @sColumnName VARCHAR(200)
DECLARE @SQLTemp NVARCHAR(4000)
--Check whether to include TEXT, NTEXT, IMAGE data types
--检查是否需要包含 Text,NText,Image数据类型
SET @INCLUDE_NTI = UPPER(LTRIM(RTRIM(@INCLUDE_NTI)))
IF @INCLUDE_NTI NOT IN (’N’, ’Y’)
SET @INCLUDE_NTI = ’N’
--Construct a cursor to get the list of Table/Column Names according to the @TABLENAME and @COLUMNNAME parameters.
--创建一个游标来读取表名和列名的列表,这里列表由参数@TABLENAME 和 @COLUMNNAME 决定
SET @strSQL = N’DECLARE TabColCursor CURSOR FOR SELECT RTRIM(LTRIM(SU.name)) + ’’.’’ + LTRIM(RTRIM(SO.name)), SC.name FROM sysobjects SO INNER JOIN syscolumns SC ON SO.id = SC.id INNER JOIN sysusers SU ON SO.uid = SU.uid WHERE SO.xtype = ’’U’’ ’
--Filter out Text/NText/Image data types if it is not included
--假如不包含Text/NText/Image数据类型,把他们过滤掉
IF @INCLUDE_NTI = ’N’
--In SysColumns sytem table XTYPE column corresponds to Column Data Type
SET @strSQL = @strSQL + ’ AND SC.xtype NOT IN (35, 99, 34) ’
--Add the TABLE(S) name i.e. filter if it is supplied
--假如有提供表名参数,把它写入过滤条件中
IF @TABLENAME IS NOT NULL AND ltrim(rtrim(@TABLENAME)) <> ’’
BEGIN
SET @TABLENAME = REPLACE(@TABLENAME, ’, ’, ’,’)
SET @strSQL = @strSQL + ’ AND (SO.name LIKE ’’’ + REPLACE(@TABLENAME, ’,’, ’’’ OR SO.name LIKE ’’’) + ’’’)’
SET @SQLTemp= ’ AND (SO.name LIKE ’’’ + REPLACE(@TABLENAME, ’,’, ’’’ OR SO.name LIKE ’’’) + ’’’)’
END
--Add the COLUMN(S) name i.e. filter if it is supplied
--假如有提供列名参数,把它写入过滤条件中
IF @COLUMNNAME IS NOT NULL AND ltrim(rtrim(@COLUMNNAME)) <> ’’
BEGIN
SET @COLUMNNAME = REPLACE(@COLUMNNAME, ’, ’, ’,’)
SET @strSQL = @strSQL + ’ AND (SC.name LIKE ’’’ + REPLACE(@COLUMNNAME, ’,’, ’’’ OR SC.name LIKE ’’’) + ’’’)’
END
--Execute the constructed "Cursor Declaration" string
--执行定义游标的SQL语句
EXECUTE sp_executesql @strSQL
IF @@ERROR > 0
BEGIN
PRINT ’Error while declaring the Cursor. Please check out the parameters supplied to the Procedure’
RETURN -1
END
--Database Transaction.
--标记一个显式本地事务的起始点
BEGIN TRANSACTION gDatabaseTrans
--Open the cursor
--打开游标
OPEN TabColCursor
--Fetch te Table, Column names to variables
--用游标取出标名、列名对应到参数
FETCH NEXT FROM TabColCursor
INTO @sTableName, @sColumnName
CREATE PROCEDURE SP_execSQLonDB
(@TABLENAME VARCHAR(50), --表名条件
@COLUMNNAME VARCHAR(50), --字段条件
@SQL NVARCHAR(4000), --执行的SQL
@INCLUDE_NTI CHAR(1) = ’N’) --是否包含Text,NText,Image数据类型
AS
BEGIN
--Variable Declaration
--变量定义
DECLARE @strSQL NVARCHAR(4000)
DECLARE @SQL2 NVARCHAR(4000)
DECLARE @sTableName VARCHAR(200)
DECLARE @sColumnName VARCHAR(200)
DECLARE @SQLTemp NVARCHAR(4000)
--Check whether to include TEXT, NTEXT, IMAGE data types
--检查是否需要包含 Text,NText,Image数据类型
SET @INCLUDE_NTI = UPPER(LTRIM(RTRIM(@INCLUDE_NTI)))
IF @INCLUDE_NTI NOT IN (’N’, ’Y’)
SET @INCLUDE_NTI = ’N’
--Construct a cursor to get the list of Table/Column Names according to the @TABLENAME and @COLUMNNAME parameters.
--创建一个游标来读取表名和列名的列表,这里列表由参数@TABLENAME 和 @COLUMNNAME 决定
SET @strSQL = N’DECLARE TabColCursor CURSOR FOR SELECT RTRIM(LTRIM(SU.name)) + ’’.’’ + LTRIM(RTRIM(SO.name)), SC.name FROM sysobjects SO INNER JOIN syscolumns SC ON SO.id = SC.id INNER JOIN sysusers SU ON SO.uid = SU.uid WHERE SO.xtype = ’’U’’ ’
--Filter out Text/NText/Image data types if it is not included
--假如不包含Text/NText/Image数据类型,把他们过滤掉
IF @INCLUDE_NTI = ’N’
--In SysColumns sytem table XTYPE column corresponds to Column Data Type
SET @strSQL = @strSQL + ’ AND SC.xtype NOT IN (35, 99, 34) ’
--Add the TABLE(S) name i.e. filter if it is supplied
--假如有提供表名参数,把它写入过滤条件中
IF @TABLENAME IS NOT NULL AND ltrim(rtrim(@TABLENAME)) <> ’’
BEGIN
SET @TABLENAME = REPLACE(@TABLENAME, ’, ’, ’,’)
SET @strSQL = @strSQL + ’ AND (SO.name LIKE ’’’ + REPLACE(@TABLENAME, ’,’, ’’’ OR SO.name LIKE ’’’) + ’’’)’
SET @SQLTemp= ’ AND (SO.name LIKE ’’’ + REPLACE(@TABLENAME, ’,’, ’’’ OR SO.name LIKE ’’’) + ’’’)’
END
--Add the COLUMN(S) name i.e. filter if it is supplied
--假如有提供列名参数,把它写入过滤条件中
IF @COLUMNNAME IS NOT NULL AND ltrim(rtrim(@COLUMNNAME)) <> ’’
BEGIN
SET @COLUMNNAME = REPLACE(@COLUMNNAME, ’, ’, ’,’)
SET @strSQL = @strSQL + ’ AND (SC.name LIKE ’’’ + REPLACE(@COLUMNNAME, ’,’, ’’’ OR SC.name LIKE ’’’) + ’’’)’
END
--Execute the constructed "Cursor Declaration" string
--执行定义游标的SQL语句
EXECUTE sp_executesql @strSQL
IF @@ERROR > 0
BEGIN
PRINT ’Error while declaring the Cursor. Please check out the parameters supplied to the Procedure’
RETURN -1
END
--Database Transaction.
--标记一个显式本地事务的起始点
BEGIN TRANSACTION gDatabaseTrans
--Open the cursor
--打开游标
OPEN TabColCursor
--Fetch te Table, Column names to variables
--用游标取出标名、列名对应到参数
FETCH NEXT FROM TabColCursor
INTO @sTableName, @sColumnName
-= 资 源 教 程 =-
文 章 搜 索