我的SQLSERVER 分页存储过程

/**
*  procedure name : kk_fenye
*  author : michael
*  此段代码是转载别人的。 
*  create date : 2011-12-07
*/
create procedure kk_fenye
---获得某一页的数据---
@currPage int = 1,   --->当前页页码(即top currPage)
@showColumn varchar(2000) = '*', --->需要查询的字段(即column1,column2,......)
@tabName varchar(2000),   --->需要查看的表名(即from table_name)
@strCondition varchar(2000) = '', --->查询条件(即where condition......) 不用加where关键字
@ascColumn varchar(100) = '',  --->排序的字段名(即order by column asc/desc)
@bitOrderType int = 0,   --->排序的类型(0为升序,1为降序)
@pkColumn varchar(50) = '',  --->主键名称
@pageSize int = 20   --->分页大小
as
begin  --->存储过程开始
---该存储过程需要用到的几个变量---
declare @strTemp varchar(1000)
declare @strSql varchar(4000)  --->该存储过程最后执行的语句
declare @strOrderType varchar(1000) --->排序类型语句(order by column asc/desc)
begin
if @bitOrderType = 1  --->@bitOrderType = 1 即执行降序
begin
    set @strOrderType = ' order by ' + @ascColumn + ' desc'
    set @strTemp = '<(select min'
end
else
begin
    set @strOrderType = ' order by ' + @ascColumn + ' asc'
    set @strTemp = '>(select max'
end
if @currPage = 1 --->如果是第一页
begin
    if @strCondition != ''
set @strSql = 'select top ' + str(@pageSize) + ' ' + @showColumn + ' from ' + @tabName + ' where ' + @strCondition + @strOrderType
    else
set @strSql = 'select top ' + str(@pageSize) + ' ' + @showColumn + ' from ' + @tabName + @strOrderType
end
else  --->其他页
begin
    if @strCondition != ''
set @strSql = 'select top ' + str(@pageSize) + ' ' + @showColumn + ' from ' + @tabName + ' where ' + @strCondition + ' and ' +
@pkColumn + @strTemp + '(' + @pkColumn + ')' + ' from (select top ' + str((@currPage-1)*@pageSize) + ' ' + @pkColumn +
' from ' + @tabName + @strOrderType + ') as TabTemp)' + @strOrderType
    else
set @strSql = 'select top ' + str(@pageSize) + ' ' + @showColumn + ' from ' + @tabName + ' where ' + @pkColumn + @strTemp +
'(' + @pkColumn + ')' + ' from (select top ' + str((@currPage-1)*@pageSize) + ' ' + @pkColumn + ' from ' +
@tabName + @strOrderType + ') as TabTemp)' + @strOrderType
end
end
EXEC (@strSql)
end --->存储过程结束
go 


调用方法:
  exec kk_fenye 1,'*','accout1','','transactionDate',1,'id',25 

查询表TableName的所有字段的前25条记录,因为是第一页,排序字段为CreateDate,1表示降序排列主键是PkID。这个存储过程的功能比较强大,用在项目中
非常的适用。尤其是在百万级数据上,它的优势就显露无疑了。


 int pageNum = 1;
 int startdate = 20111011;
 int enddate = 20111013;

sql="exec kk_fenye '"+pageNum+"','*','accout1','transactionDate >= "+startdate+"+and transactionDate <= "+enddate+"','transactionDate',1,'id',25";
st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
					ResultSet.CONCUR_UPDATABLE);
rs=st.executeQuery("SET NOCOUNT ON;"+sql);


查询表记录数的存储过程
create procedure procRowsCount
@tabName varchar(200),  --->需要查询的表名
@colName varchar(200) = '*', --->需要查询的列名
@condition varchar(200) = '' --->查询条件
as
begin
    declare @strSql varchar(255)
    if @condition = ''
set @strSql = 'select count(' + @colName + ') from ' + @tabName
    else
set @strSql = 'select count(' + @colName + ') from ' + @tabName + ' where ' + @condition
    EXEC (@strSql)
end 


调用方法:
exec kk_procRowsCount 'accout1','transactionDate','transactionDate >= 20111011 and transactionDate <= 20111012'


通用存储过程之一:插入、更新、删除存储过程。
临时存储过程如下
CREATE PROC #AutoGeneration_IUD_P
@TABLENAME VARCHAR(50),
@FLAT  TINYINT
AS
BEGIN
 DECLARE @HOST_NAME VARCHAR(200)
 DECLARE @GET_DATE DATETIME
 SELECT @HOST_NAME=HOST_NAME(),@GET_DATE=GETDATE()
 IF @FLAT=1 --插入
 BEGIN
 DECLARE @INSERT_SQLROC NVARCHAR(4000)
 DECLARE @INSERT_SQL VARCHAR(8000)
 DECLARE @INSERT_PARAMETER VARCHAR(8000)
 DECLARE @INSERT_DESCRIPTION VARCHAR(8000)
 DECLARE @INSERT_REMARK VARCHAR(2000)
 DECLARE @INSERT_COLUMN VARCHAR(8000)
 SELECT @INSERT_SQLROC='',@INSERT_SQL='',@INSERT_PARAMETER='',@INSERT_DESCRIPTION='',
   @INSERT_REMARK='',@INSERT_COLUMN=''
 SET @INSERT_SQLROC=@INSERT_SQLROC+'IF EXISTS (SELECT 1 FROM SYSOBJECTS WHERE ID=OBJECT_ID(''SP_'+@TABLENAME+'_AddNew'') AND XTYPE IN (N''P''))'+CHAR(10)
 SET @INSERT_SQLROC=@INSERT_SQLROC+SPACE(5)+'DROP PROC SP_'+@TABLENAME+'_AddNew'+CHAR(10)
 SET @INSERT_SQLROC=@INSERT_SQLROC+'GO '
 SELECT @INSERT_PARAMETER=@INSERT_PARAMETER+SPACE(4)+'@'+LTRIM(NAME)+SPACE(15-LEN(NAME))+
     CASE WHEN xtype=34 THEN 'image'
       WHEN xtype=35 THEN 'text'
       WHEN xtype=36 THEN 'uniqueidentifier'
       WHEN xtype=48 THEN 'tinyint'
       WHEN xtype=52 THEN 'smallint'
       WHEN xtype=56 THEN 'int'
       WHEN xtype=58 THEN 'smalldatetime'
       WHEN xtype=59 THEN 'real'
       WHEN xtype=60 THEN 'money'
       WHEN xtype=61 THEN 'datetime'
       WHEN xtype=62 THEN 'float'
       WHEN xtype=98 THEN 'sql_variant'
       WHEN xtype=99 THEN 'ntext'
       WHEN xtype=104 THEN 'bit'
       WHEN xtype=106 THEN 'decimal'
       WHEN xtype=108 THEN 'numeric'
       WHEN xtype=122 THEN 'smallmoney'
       WHEN xtype=127 THEN 'bigint'
       WHEN xtype=165 THEN 'varbinary'
          WHEN xtype=167 THEN 'varchar'+'('+LTRIM(length)+')'
       WHEN xtype=173 THEN 'binary'
       WHEN xtype=175 THEN 'char'+'('+LTRIM(length)+')'
       WHEN xtype=189 THEN 'timestamp'
       WHEN xtype=231 THEN 'nvarchar'+'('+LTRIM(length)+')'
       WHEN xtype=239 THEN 'nchar'+'('+LTRIM(length)+')'
       WHEN xtype=241 THEN 'xml'
       WHEN xtype=231 THEN 'sysname' END+','+CHAR(10),
   @INSERT_REMARK=@INSERT_REMARK+',@'+NAME,
   @INSERT_COLUMN=@INSERT_COLUMN+','+NAME
 FROM
  ( 
  SELECT NAME,CDEFAULT,A.ID,XTYPE,LENGTH,COLORDER
  FROM SYSCOLUMNS A 
  WHERE A.ID=OBJECT_ID(''+@TABLENAME+'')
  AND COLUMNPROPERTY(A.ID,A.NAME,'IsIdentity')<>1 
  )TB
 ORDER BY COLORDER
 SET @INSERT_DESCRIPTION=@INSERT_DESCRIPTION+'/*+--------------------------------------+'+CHAR(10)
 SET @INSERT_DESCRIPTION=@INSERT_DESCRIPTION+'| 过程名称:SP_'+@TABLENAME+'_AddNew'+CHAR(10)
 SET @INSERT_DESCRIPTION=@INSERT_DESCRIPTION+'| 功能说明:插入表'+@TABLENAME+'的存储过程'+CHAR(10)
 SET @INSERT_DESCRIPTION=@INSERT_DESCRIPTION+'| 入口参数:'+STUFF(@INSERT_REMARK,1,1,'')+''+CHAR(10)
 SET @INSERT_DESCRIPTION=@INSERT_DESCRIPTION+'| 过程返回:无返回记录'+CHAR(10)
 SET @INSERT_DESCRIPTION=@INSERT_DESCRIPTION+'| 维护记录:Y/A'+CHAR(10)
 SET @INSERT_DESCRIPTION=@INSERT_DESCRIPTION+'| 使用案例:SP_'+@TABLENAME+'_AddNew'+CHAR(10)
 SET @INSERT_DESCRIPTION=@INSERT_DESCRIPTION+'| 工作站名:'+@HOST_NAME+''+CHAR(10)
 SET @INSERT_DESCRIPTION=@INSERT_DESCRIPTION+'| 联系方式:[email protected]'+CHAR(10)
 SET @INSERT_DESCRIPTION=@INSERT_DESCRIPTION+'| 创建日期:'+CONVERT(VARCHAR(20),@GET_DATE,120)+''+CHAR(10) 
 SET @INSERT_DESCRIPTION=@INSERT_DESCRIPTION+'+--------------------------------------+*/'+CHAR(10)
 SELECT @INSERT_SQLROC=@INSERT_SQLROC+CHAR(10)+@INSERT_DESCRIPTION+'CREATE PROC SP_'+@TABLENAME+'_AddNew'
 SET @INSERT_SQLROC=@INSERT_SQLROC+CHAR(13)+CHAR(10)+LEFT(@INSERT_PARAMETER,LEN(@INSERT_PARAMETER)-2)+CHAR(10)
 SET @INSERT_SQLROC=@INSERT_SQLROC+'AS'+CHAR(10)+'BEGIN'
 SET @INSERT_SQLROC=@INSERT_SQLROC+CHAR(10)+SPACE(4)+'SET NOCOUNT ON'
 SET @INSERT_SQL=SPACE(8)+'INSERT INTO '+@TABLENAME+'('+STUFF(@INSERT_COLUMN,1,1,'')+')'+CHAR(13)+CHAR(10)+SPACE(8)+' SELECT '+STUFF(@INSERT_REMARK,1,1,'')
 SET @INSERT_SQLROC=@INSERT_SQLROC+CHAR(10)+CHAR(32)+@INSERT_SQL
 SET @INSERT_SQLROC=@INSERT_SQLROC+CHAR(10)+SPACE(8)+' SELECT _ROWCOUNT=@@ROWCOUNT'
 SET @INSERT_SQLROC=@INSERT_SQLROC+CHAR(10)+SPACE(4)+'SET NOCOUNT OFF' 
 SET @INSERT_SQLROC=@INSERT_SQLROC+CHAR(10)+'END'
 PRINT @INSERT_SQLROC+CHAR(10)+'GO '
 --EXEC(@INSERT_SQLROC)
 --EXEC SP_EXECUTESQL @INSERT_SQLROC
 END
 IF @FLAT=2 --修改
 BEGIN
 DECLARE @UPDATE_SQLROC VARCHAR(8000)
 DECLARE @UPDATE_SQL VARCHAR(8000) 
 DECLARE @UPDATE_PARAMETER VARCHAR(1000)
 DECLARE @UPDATE_DESCRIPTION VARCHAR(1000)
 DECLARE @UPDATE_REMARK VARCHAR(1000)
 DECLARE @UPDATE_KEY_COLUMN VARCHAR(1000)
 DECLARE @UPDATE_COLUMN VARCHAR(5000)
 SELECT  @UPDATE_SQLROC='',
   @UPDATE_SQL='',@UPDATE_PARAMETER='',@UPDATE_DESCRIPTION='',
   @UPDATE_REMARK='',@UPDATE_KEY_COLUMN='',@UPDATE_COLUMN=''
 SET @UPDATE_SQLROC=@UPDATE_SQLROC+'IF EXISTS (SELECT 1 FROM SYSOBJECTS WHERE ID=OBJECT_ID(''SP_'+@TABLENAME+'_Update'') AND XTYPE IN (N''P''))'+CHAR(10)
 SET @UPDATE_SQLROC=@UPDATE_SQLROC+SPACE(5)+'DROP PROC SP_'+@TABLENAME+'_Update'+CHAR(10)
 SET @UPDATE_SQLROC=@UPDATE_SQLROC+' GO '
 SELECT @UPDATE_PARAMETER=@UPDATE_PARAMETER+SPACE(4)+'@'+LTRIM(NAME)+SPACE(15-LEN(NAME))+
     CASE WHEN xtype=34 THEN 'image'
       WHEN xtype=35 THEN 'text'
       WHEN xtype=36 THEN 'uniqueidentifier'
       WHEN xtype=48 THEN 'tinyint'
       WHEN xtype=52 THEN 'smallint'
       WHEN xtype=56 THEN 'int'
       WHEN xtype=58 THEN 'smalldatetime'
       WHEN xtype=59 THEN 'real'
       WHEN xtype=60 THEN 'money'
       WHEN xtype=61 THEN 'datetime'
       WHEN xtype=62 THEN 'float'
       WHEN xtype=98 THEN 'sql_variant'
       WHEN xtype=99 THEN 'ntext'
       WHEN xtype=104 THEN 'bit'
       WHEN xtype=106 THEN 'decimal'
       WHEN xtype=108 THEN 'numeric'
       WHEN xtype=122 THEN 'smallmoney'
       WHEN xtype=127 THEN 'bigint'
       WHEN xtype=165 THEN 'varbinary'
          WHEN xtype=167 THEN 'varchar'+'('+LTRIM(length)+')'
       WHEN xtype=173 THEN 'binary'
       WHEN xtype=175 THEN 'char'+'('+LTRIM(length)+')'
       WHEN xtype=189 THEN 'timestamp'
       WHEN xtype=231 THEN 'nvarchar'+'('+LTRIM(length)+')'
       WHEN xtype=239 THEN 'nchar'+'('+LTRIM(length)+')'
       WHEN xtype=241 THEN 'xml'
       WHEN xtype=231 THEN 'sysname' END+','+CHAR(10)
 FROM SYSCOLUMNS A
    WHERE ID=OBJECT_ID(''+@TABLENAME+'')
 SET NOCOUNT ON
 CREATE TABLE #(TABLE_QUALIFIER VARCHAR(100),
 TABLE_OWNER  VARCHAR(100),
 TABLE_NAME   VARCHAR(100),
 COLUMN_NAME  VARCHAR(100),
 KEY_SEQ      VARCHAR(50),
 PK_NAME   VARCHAR(100))
 INSERT INTO # EXEC SP_PKEYS @TABLENAME
 SELECT @UPDATE_REMARK=@UPDATE_REMARK+','+COLUMN_NAME+'=@'+COLUMN_NAME
 FROM #
 SELECT @UPDATE_KEY_COLUMN=@UPDATE_KEY_COLUMN+','+NAME+'=@'+NAME
 FROM SYSCOLUMNS A
    WHERE ID=OBJECT_ID(''+@TABLENAME+'')
 AND NAME NOT IN (SELECT COLUMN_NAME FROM #)
 DROP TABLE #
 SET NOCOUNT OFF
  IF DATALENGTH(@UPDATE_PARAMETER)>0
  BEGIN
   SET @UPDATE_PARAMETER=LEFT(@UPDATE_PARAMETER,LEN(@UPDATE_PARAMETER)-2)
   SET @UPDATE_DESCRIPTION=@UPDATE_DESCRIPTION+'/*+--------------------------------------+'+CHAR(10)
   SET @UPDATE_DESCRIPTION=@UPDATE_DESCRIPTION+'| 过程名称:SP_'+@TABLENAME+'_Update'+CHAR(10)
   SET @UPDATE_DESCRIPTION=@UPDATE_DESCRIPTION+'| 功能说明:更新表'+@TABLENAME+'的存储过程'+CHAR(10)
   SET @UPDATE_DESCRIPTION=@UPDATE_DESCRIPTION+'| 入口参数:'+STUFF(@UPDATE_REMARK,1,1,'')+''+CHAR(10)
   SET @UPDATE_DESCRIPTION=@UPDATE_DESCRIPTION+'| 过程返回:无返回记录'+CHAR(10)
   SET @UPDATE_DESCRIPTION=@UPDATE_DESCRIPTION+'| 维护记录:Y/A'+CHAR(10)
   SET @UPDATE_DESCRIPTION=@UPDATE_DESCRIPTION+'| 使用案例:SP_'+@TABLENAME+'_Update'+CHAR(10)
   SET @UPDATE_DESCRIPTION=@UPDATE_DESCRIPTION+'| 工作站名:'+@HOST_NAME+''+CHAR(10)
   SET @UPDATE_DESCRIPTION=@UPDATE_DESCRIPTION+'| 联系方式:[email protected]'+CHAR(10)
   SET @UPDATE_DESCRIPTION=@UPDATE_DESCRIPTION+'| 创建日期:'+CONVERT(VARCHAR(20),@GET_DATE,120)+''+CHAR(10) 
   SET @UPDATE_DESCRIPTION=@UPDATE_DESCRIPTION+'+--------------------------------------+*/'+CHAR(10)
   SELECT @UPDATE_SQLROC=@UPDATE_SQLROC+CHAR(10)+@UPDATE_DESCRIPTION+'CREATE PROC SP_'+@TABLENAME+'_Update'
   SET @UPDATE_SQLROC=@UPDATE_SQLROC+CHAR(13)+CHAR(10)+@UPDATE_PARAMETER+CHAR(10)
   SET @UPDATE_SQLROC=@UPDATE_SQLROC+'AS'+CHAR(10)+'BEGIN'
   SET @UPDATE_SQLROC=@UPDATE_SQLROC+CHAR(10)+SPACE(4)+'SET NOCOUNT ON'+CHAR(10)
   SET @UPDATE_SQL=SPACE(8)+'UPDATE '+@TABLENAME+CHAR(10)+SPACE(8)+'SET '
   SET @UPDATE_SQLROC=@UPDATE_SQLROC+@UPDATE_SQL+STUFF(@UPDATE_KEY_COLUMN,1,1,'')
   SET @UPDATE_SQLROC=@UPDATE_SQLROC+CHAR(10)+SPACE(8)+'FROM '+@TABLENAME+CHAR(10)+SPACE(8)+'WHERE '+STUFF(@UPDATE_REMARK,1,1,'')+''
   SET @UPDATE_SQLROC=@UPDATE_SQLROC+CHAR(10)+SPACE(8)+'SELECT _ROWCOUNT=@@ROWCOUNT'
   SET @UPDATE_SQLROC=@UPDATE_SQLROC+CHAR(10)+SPACE(4)+'SET NOCOUNT OFF'
   SET @UPDATE_SQLROC=@UPDATE_SQLROC+CHAR(10)+'END' 
   PRINT @UPDATE_SQLROC+CHAR(13)+CHAR(10)+'GO'
   --EXECUTE(@UPDATE_SQLROC+' GO ')
  END
 END
 IF @FLAT=3 --删除
 BEGIN
 DECLARE @DELETE_SQLROC VARCHAR(8000)
 DECLARE @DELETE_SQL VARCHAR(8000) 
 DECLARE @DELETE_PARAMETER VARCHAR(1000)
 DECLARE @DELETE_DESCRIPTION VARCHAR(1000)
 DECLARE @DELETE_REMARK VARCHAR(1000)
 DECLARE @DELETE_KEY_COLUMN VARCHAR(1000)
 SELECT  @DELETE_SQLROC='',@DELETE_SQL='',@DELETE_PARAMETER='',@DELETE_DESCRIPTION='',
   @DELETE_REMARK='',@DELETE_KEY_COLUMN=''
 SET  @DELETE_SQLROC=@DELETE_SQLROC+'IF EXISTS (SELECT 1 FROM SYSOBJECTS WHERE ID=OBJECT_ID(''SP_'+@TABLENAME+'_Remove'') AND XTYPE IN (N''P''))'+CHAR(10)
 SET  @DELETE_SQLROC=@DELETE_SQLROC+SPACE(5)+'DROP PROC SP_'+@TABLENAME+'_Remove'+CHAR(10)
 SET  @DELETE_SQLROC=@DELETE_SQLROC+' GO '
 SET NOCOUNT ON
 CREATE TABLE #_#(TABLE_QUALIFIER VARCHAR(100),
 TABLE_OWNER  VARCHAR(100),
 TABLE_NAME   VARCHAR(100),
 COLUMN_NAME  VARCHAR(100),
 KEY_SEQ      VARCHAR(50),
 PK_NAME   VARCHAR(100))
 INSERT INTO #_# EXEC SP_PKEYS @TABLENAME
 SELECT @DELETE_KEY_COLUMN=@DELETE_KEY_COLUMN+','+COLUMN_NAME+'=@'+COLUMN_NAME,
   @DELETE_REMARK=@DELETE_REMARK+',@'+COLUMN_NAME
 FROM #_#
 SELECT @DELETE_PARAMETER=@DELETE_PARAMETER+SPACE(4)+'@'+LTRIM(NAME)+SPACE(15-LEN(NAME))+
     CASE WHEN xtype=34 THEN 'image'
       WHEN xtype=35 THEN 'text'
       WHEN xtype=36 THEN 'uniqueidentifier'
       WHEN xtype=48 THEN 'tinyint'
       WHEN xtype=52 THEN 'smallint'
       WHEN xtype=56 THEN 'int'
       WHEN xtype=58 THEN 'smalldatetime'
       WHEN xtype=59 THEN 'real'
       WHEN xtype=60 THEN 'money'
       WHEN xtype=61 THEN 'datetime'
       WHEN xtype=62 THEN 'float'
       WHEN xtype=98 THEN 'sql_variant'
       WHEN xtype=99 THEN 'ntext'
       WHEN xtype=104 THEN 'bit'
       WHEN xtype=106 THEN 'decimal'
       WHEN xtype=108 THEN 'numeric'
       WHEN xtype=122 THEN 'smallmoney'
       WHEN xtype=127 THEN 'bigint'
       WHEN xtype=165 THEN 'varbinary'
          WHEN xtype=167 THEN 'varchar'+'('+LTRIM(length)+')'
       WHEN xtype=173 THEN 'binary'
       WHEN xtype=175 THEN 'char'+'('+LTRIM(length)+')'
       WHEN xtype=189 THEN 'timestamp'
       WHEN xtype=231 THEN 'nvarchar'+'('+LTRIM(length)+')'
       WHEN xtype=239 THEN 'nchar'+'('+LTRIM(length)+')'
       WHEN xtype=241 THEN 'xml'
       WHEN xtype=231 THEN 'sysname' END+','+CHAR(10)
 FROM SYSCOLUMNS A
    WHERE ID=OBJECT_ID(''+@TABLENAME+'') 
 AND NAME IN (SELECT COLUMN_NAME FROM #_#)
 DROP TABLE #_#
 SET NOCOUNT OFF
  IF DATALENGTH(@DELETE_PARAMETER)>0
  BEGIN
   SET @DELETE_PARAMETER=LEFT(@DELETE_PARAMETER,LEN(@DELETE_PARAMETER)-2)
   SET @DELETE_DESCRIPTION=@DELETE_DESCRIPTION+'/*+--------------------------------------+'+CHAR(10)
   SET @DELETE_DESCRIPTION=@DELETE_DESCRIPTION+'| 过程名称:SP_'+@TABLENAME+'_Remove'+CHAR(10)
   SET @DELETE_DESCRIPTION=@DELETE_DESCRIPTION+'| 功能说明:删除表'+@TABLENAME+'的存储过程'+CHAR(10)
   SET @DELETE_DESCRIPTION=@DELETE_DESCRIPTION+'| 入口参数:'+STUFF(@DELETE_REMARK,1,1,'')+''+CHAR(10)
   SET @DELETE_DESCRIPTION=@DELETE_DESCRIPTION+'| 过程返回:无返回记录'+CHAR(10)
   SET @DELETE_DESCRIPTION=@DELETE_DESCRIPTION+'| 维护记录:Y/A'+CHAR(10)
   SET @DELETE_DESCRIPTION=@DELETE_DESCRIPTION+'| 使用案例:SP_'+@TABLENAME+'_Remove'+CHAR(10)
   SET @DELETE_DESCRIPTION=@DELETE_DESCRIPTION+'| 工作站名:'+@HOST_NAME+''+CHAR(10)
   SET @DELETE_DESCRIPTION=@DELETE_DESCRIPTION+'| 联系方式:[email protected]'+CHAR(10)
   SET @DELETE_DESCRIPTION=@DELETE_DESCRIPTION+'| 创建日期:'+CONVERT(VARCHAR(20),@GET_DATE,120)+''+CHAR(10) 
   SET @DELETE_DESCRIPTION=@DELETE_DESCRIPTION+'+--------------------------------------+*/'+CHAR(10)
   SELECT @DELETE_SQLROC=@DELETE_SQLROC+CHAR(10)+@DELETE_DESCRIPTION+'CREATE PROC SP_'+@TABLENAME+'_Remove'
   SET @DELETE_SQLROC=@DELETE_SQLROC+CHAR(13)+CHAR(10)+@DELETE_PARAMETER+CHAR(10)
   SET @DELETE_SQLROC=@DELETE_SQLROC+'AS'+CHAR(10)+'BEGIN'
   SET @DELETE_SQLROC=@DELETE_SQLROC+CHAR(10)+SPACE(4)+'SET NOCOUNT ON'
   SET @DELETE_SQL='DELETE '+@TABLENAME+CHAR(10)+SPACE(8)+'WHERE '
   SET @DELETE_SQLROC=@DELETE_SQLROC+CHAR(10)+SPACE(8)+@DELETE_SQL+STUFF(@DELETE_KEY_COLUMN,1,1,'')
   SET @DELETE_SQLROC=@DELETE_SQLROC+CHAR(10)+SPACE(8)+'SELECT _ROWCOUNT=@@ROWCOUNT'
   SET @DELETE_SQLROC=@DELETE_SQLROC+CHAR(10)+SPACE(4)+'SET NOCOUNT OFF'
   SET @DELETE_SQLROC=@DELETE_SQLROC+CHAR(10)+'END' 
   PRINT @DELETE_SQLROC+CHAR(13)+CHAR(10)+'GO'  
   --EXECUTE(@DELETE_SQLROC+' GO ') 
  END
 END
END

GO


CREATE PROC #SP_Generation_IUD
@TABLENAMES VARCHAR(8000)
AS
BEGIN
DECLARE @I INT
DECLARE @TABLENAME VARCHAR(100)
SET @I=CHARINDEX(',',@TABLENAMES)
WHILE @I>0
BEGIN
 SET @TABLENAME=LEFT(@TABLENAMES,@I-1)
 EXEC #AutoGeneration_IUD_P @TABLENAME,1
 EXEC #AutoGeneration_IUD_P @TABLENAME,2
 EXEC #AutoGeneration_IUD_P @TABLENAME,3
 SET @TABLENAMES=RIGHT(@TABLENAMES,LEN(@TABLENAMES)-@I)
 SET @I=CHARINDEX(',',@TABLENAMES)
END
IF LEN(@TABLENAMES)>0
BEGIN
 EXEC #AutoGeneration_IUD_P @TABLENAMES,1
 EXEC #AutoGeneration_IUD_P @TABLENAMES,2
 EXEC #AutoGeneration_IUD_P @TABLENAMES,3
END
END

GO


--调用方法如下: exec  #SP_Generation_IUD '表1,表2,表3'

--Example:

exec  #SP_Generation_IUD 'ICR_PermittedUrl,ICR_ForbiddenUrl'
GO

DROP PROC  #SP_Generation_IUD
GO

DROP PROC #AutoGeneration_IUD_P

GO

删除表ICR_ForbiddenUrl的存储过程
/*+--------------------------------------+
| 过程名称:SP_ICR_ForbiddenUrl_Remove
| 功能说明:删除表ICR_ForbiddenUrl的存储过程
| 入口参数:@ID
| 过程返回:无返回记录
| 维护记录:Y/A
| 使用案例:SP_ICR_ForbiddenUrl_Remove
| 工作站名:XXX
| 联系方式:[email protected]
| 创建日期:2010-07-12 19:18:39
+--------------------------------------+*/
CREATE PROC SP_ICR_ForbiddenUrl_Remove
    @ID             int
AS
BEGIN
    SET NOCOUNT ON
        DELETE ICR_ForbiddenUrl
        WHERE ID=@ID
        SELECT _ROWCOUNT=@@ROWCOUNT
    SET NOCOUNT OFF
END
GO



更新表ICR_ForbiddenUrl的存储过程
/*+--------------------------------------+
| 过程名称:SP_ICR_ForbiddenUrl_Update
| 功能说明:更新表ICR_ForbiddenUrl的存储过程
| 入口参数:ID=@ID
| 过程返回:无返回记录
| 维护记录:Y/A
| 使用案例:SP_ICR_ForbiddenUrl_Update
| 工作站名:XXX
| 联系方式:[email protected]
| 创建日期:2010-07-12 09:18:39
+--------------------------------------+*/
CREATE PROC SP_ICR_ForbiddenUrl_Update
    @ID             int,
    @URL            varchar(100),
    @State          tinyint,
    @CategoryID     int,
    @Name           varchar(50),
    @Description    varchar(200),
    @DateCreated    datetime
AS
BEGIN
    SET NOCOUNT ON
        UPDATE ICR_ForbiddenUrl
        SET URL=@URL,State=@State,CategoryID=@CategoryID,Name=@Name,Description=@Description,DateCreated=@DateCreated
        FROM ICR_ForbiddenUrl
        WHERE ID=@ID
        SELECT _ROWCOUNT=@@ROWCOUNT
    SET NOCOUNT OFF
END
GO
IF EXISTS (SELECT 1 FROM SYSOBJECTS WHERE ID=OBJECT_ID('SP_ICR_ForbiddenUrl_Remove') AND XTYPE IN (N'P'))
     DROP PROC SP_ICR_ForbiddenUrl_Remove
 GO 


插入表ICR_ForbiddenUrl的存储过程
/*+--------------------------------------+
| 过程名称:SP_ICR_ForbiddenUrl_Remove
| 功能说明:删除表ICR_ForbiddenUrl的存储过程
| 入口参数:@ID
| 过程返回:无返回记录
| 维护记录:Y/A
| 使用案例:SP_ICR_ForbiddenUrl_Remove
| 工作站名:XXX
| 联系方式:[email protected]
| 创建日期:2010-07-12 19:18:39
+--------------------------------------+*/
CREATE PROC SP_ICR_ForbiddenUrl_AddNew
    @ID             int,
    @URL            varchar(100),
    @State          tinyint,
    @CategoryID     int,
    @Name           varchar(50),
    @Description    varchar(200),
    @DateCreated    datetime
AS
BEGIN
    SET NOCOUNT ON
         INSERT INTO ICR_ForbiddenUrl(ID,URL,State,CategoryID,Name,Description,DateCreated)
         SELECT @ID,@URL,@State,@CategoryID,@Name,@Description,@DateCreated
         SELECT _ROWCOUNT=@@ROWCOUNT
    SET NOCOUNT OFF
END
GO 
IF EXISTS (SELECT 1 FROM SYSOBJECTS WHERE ID=OBJECT_ID('SP_ICR_ForbiddenUrl_Update') AND XTYPE IN (N'P'))
     DROP PROC SP_ICR_ForbiddenUrl_Update
 GO 

你可能感兴趣的:(procedure)