如何同时对多个表或列操作
如何同时对多个表或列操作,如何同时对多个表或列操作
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