壹佰网|ERP100 - 企业信息化知识门户

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 3721|回复: 10

SQL存储过程汇总

    [复制链接]
发表于 2010/3/20 23:20:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。如果您注册时有任何问题请联系客服QQ: 83569622  。

您需要 登录 才可以下载或查看,没有帐号?注册

x
常用存储过程集锦,都是一些mssql常用的一些,大家可以根据需要选择使用。

  =================分页==========================


  /*分页查找数据*/
  CREATE PROCEDURE [dbo].[GetRecordSet]
  @strSql varchar(8000),--查询sql,如select * from [user]
  @PageIndex int,--查询当页号
  @PageSize int--每页显示记录
  AS
  set nocount on
  declare @p1 int
  declare @currentPage int
  set @currentPage = 0
  declare @RowCount int
  set @RowCount = 0
  declare @PageCount int
  set @PageCount = 0
  exec sp_cursoropen @p1 output,@strSql,@scrollopt=1,@ccopt=1,@rowcount=@rowCount output --得到总记录数
  select @PageCount=ceiling(1.0*@rowCount/@pagesize) --得到总页数
  ,@currentPage=(@PageIndex-1)*@PageSize+1
  select @RowCount,@PageCount
  exec sp_cursorfetch @p1,16,@currentPage,@PageSize
  exec sp_cursorclose @p1
  set nocount off
  GO



======================================================

--列出SQL SERVER 所有表,字段名,主键,类型,长度,小数位数等信息

--在查询分析器里运行即可,可以生成一个表,导出到EXCEL中

-- ======================================================

SELECT

(case when a.colorder=1 then d.name else '' end)表名,

a.colorder 字段序号,

a.name 字段名,

(case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) 标识,

(case when (SELECT count(*)

FROM sysobjects

WHERE (name in

(SELECT name

FROM sysindexes

WHERE (id = a.id) AND (indid in

(SELECT indid

FROM sysindexkeys

WHERE (id = a.id) AND (colid in

(SELECT colid

FROM syscolumns

WHERE (id = a.id) AND (name = a.name))))))) AND

(xtype = 'PK'))>0 then '√' else '' end) 主键,

b.name 类型,

a.length 占用字节数,

COLUMNPROPERTY(a.id,a.name,'PRECISION') as 长度,

isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as 小数位数,

(case when a.isnullable=1 then '√'else '' end) 允许空,

isnull(e.text,'') 默认值,

isnull(g.[value],'') AS 字段说明   


FROM  syscolumns  a left join systypes b

on  a.xtype=b.xusertype

inner join sysobjects d

on a.id=d.id  and  d.xtype='U' and  d.name<>'dtproperties'

left join syscomments e

on a.cdefault=e.id

left join sysproperties g

on a.id=g.id AND a.colid = g.smallid  

order by a.id,a.colorder

-------------------------------------------------------------------------------------------------







列出SQL SERVER 所有表、字段定义,类型,长度,一个值等信息

并导出到Excel 中

-- ======================================================

-- Export all user tables definition and one sample value

-- jan-13-2003,Dr.Zhang

-- ======================================================

在查询分析器里运行:

SET ANSI_NULLS OFF

GO

SET NOCOUNT ON

GO


SET LANGUAGE 'Simplified Chinese'

go

DECLARE @tbl nvarchar(200),@fld nvarchar(200),@sql nvarchar(4000),@maxlen int,@sample nvarchar(40)


SELECT d.name TableName,a.name FieldName,b.name TypeName,a.length Length,a.isnullable IS_NULL INTO #t

FROM  syscolumns  a,  systypes b,sysobjects d  

WHERE  a.xtype=b.xusertype  and  a.id=d.id  and  d.xtype='U'


DECLARE read_cursor CURSOR

FOR SELECT TableName,FieldName FROM #t


SELECT TOP 1 '_TableName                     ' TableName,

'FieldName                      ' FieldName,'TypeName             ' TypeName,

'Length' Length,'IS_NULL' IS_NULL,

'MaxLenUsed' AS MaxLenUsed,'Sample Value          ' Sample,

'Comment   ' Comment INTO #tc FROM #t


OPEN read_cursor


FETCH NEXT FROM read_cursor INTO @tbl,@fld

WHILE (@@fetch_status <> -1)  --- failes

BEGIN

IF (@@fetch_status <> -2) -- Missing

BEGIN

SET @sql=N'SET @maxlen=(SELECT max(len(cast('+@fld+' as nvarchar))) FROM '+@tbl+')'

--PRINT @sql

EXEC SP_EXECUTESQL @sql,N'@maxlen int OUTPUT',@maxlen OUTPUT

--print @maxlen

SET @sql=N'SET @sample=(SELECT TOP 1 cast('+@fld+' as nvarchar) FROM '+@tbl+' WHERE len(cast('+@fld+' as nvarchar))='+convert(nvarchar(5),@maxlen)+')'

EXEC SP_EXECUTESQL @sql,N'@sample varchar(30) OUTPUT',@sample OUTPUT

--for quickly   

--SET @sql=N'SET @sample=convert(varchar(20),(SELECT TOP 1 '+@fld+' FROM '+

--@tbl+' order by 1 desc ))'  

PRINT @sql

print @sample

print @tbl

EXEC SP_EXECUTESQL @sql,N'@sample nvarchar(30) OUTPUT',@sample OUTPUT

INSERT INTO #tc SELECT *,ltrim(ISNULL(@maxlen,0)) as MaxLenUsed,

convert(nchar(20),ltrim(ISNULL(@sample,' '))) as Sample,' ' Comment FROM #t where TableName=@tbl and FieldName=@fld

END

FETCH NEXT FROM read_cursor INTO @tbl,@fld

END


CLOSE read_cursor

DEALLOCATE read_cursor

GO


SET ANSI_NULLS ON

GO

SET NOCOUNT OFF

GO

elect count(*)  from #t

DROP TABLE #t

GO


elect count(*)-1  from #tc


elect * into ##tx from #tc order by tablename

DROP TABLE #tc


--select * from ##tx


declare @db nvarchar(60),@sql nvarchar(3000)

et @db=db_name()

--请修改用户名和口令 导出到Excel 中

et @sql='exec master.dbo.xp_cmdshell ''bcp ..dbo.##tx out c:\'+@db+'_exp.xls -w -C936 -Usa -Psa '''

rint @sql

exec(@sql)

GO

DROP TABLE ##tx

GO




-- ======================================================

--根据表中数据生成insert语句的存储过程

--建立存储过程,执行 spGenInsertSQL 表名

--感谢playyuer

-- ======================================================

CREATE   proc spGenInsertSQL (@tablename varchar(256))


as

egin

declare @sql varchar(8000)

declare @sqlValues varchar(8000)

set @sql =' ('

set @sqlValues = 'values (''+'

select @sqlValues = @sqlValues + cols + ' + '','' + ' ,@sql = @sql + '[' + name + '],'

from

(select case

when xtype in (48,52,56,59,60,62,104,106,108,122,127)                                

then 'case when '+ name +' is null then ''NULL'' else ' + 'cast('+ name + ' as varchar)'+' end'

when xtype in (58,61)

then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast('+ name +' as varchar)'+ '+'''''''''+' end'

when xtype in (167)

then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'

when xtype in (231)

then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'

when xtype in (175)

then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as Char(' + cast(length as varchar)  + '))+'''''''''+' end'

when xtype in (239)

then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as Char(' + cast(length as varchar)  + '))+'''''''''+' end'

else '''NULL'''

end as Cols,name

from syscolumns  

where id = object_id(@tablename)

) T

set @sql ='select ''INSERT INTO ['+ @tablename + ']' + left(@sql,len(@sql)-1)+') ' + left(@sqlValues,len(@sqlValues)-4) + ')'' from '+@tablename

--print @sql

exec (@sql)

end


GO




-- ======================================================

--根据表中数据生成insert语句的存储过程

--建立存储过程,执行 proc_insert 表名

--感谢Sky_blue

-- ======================================================


CREATE proc proc_insert (@tablename varchar(256))

as

egin

set nocount on

declare @sqlstr varchar(4000)

declare @sqlstr1 varchar(4000)

declare @sqlstr2 varchar(4000)

select @sqlstr='select ''insert '+@tablename

select @sqlstr1=''

select @sqlstr2=' ('

select @sqlstr1= ' values ( ''+'

select @sqlstr1=@sqlstr1+col+'+'',''+' ,@sqlstr2=@sqlstr2+name +',' from (select case

--     when a.xtype =173 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end'

when a.xtype =104 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(1),'+a.name +')'+' end'

when a.xtype =175 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'

when a.xtype =61  then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end'

when a.xtype =106 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end'

when a.xtype =62  then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end'

when a.xtype =56  then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(11),'+a.name +')'+' end'

when a.xtype =60  then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end'

when a.xtype =239 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'

when a.xtype =108 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end'

when a.xtype =231 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'

when a.xtype =59  then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end'

when a.xtype =58  then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end'

when a.xtype =52  then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(12),'+a.name +')'+' end'

when a.xtype =122 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end'

when a.xtype =48  then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(6),'+a.name +')'+' end'

--     when a.xtype =165 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end'

when a.xtype =167 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'

else '''NULL'''

end as col,a.colid,a.name

from syscolumns a where a.id = object_id(@tablename) and a.xtype <>189 and a.xtype <>34 and a.xtype <>35 and  a.xtype <>36

)t order by colid


select @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+')'' from '+@tablename

--  print @sqlstr

exec( @sqlstr)

set nocount off

end

GO




  =========================用户注册============================
  /*
  用户注册,也算是添加吧
  */
  Create proc [dbo].[UserAdd]
  (
  @loginID nvarchar(50),     --登录帐号
  @password nvarchar(50), --密码
  @email nvarchar(200) --电子信箱
  )
  as
  declare @userID int --用户编号
  --登录账号已经被注册
  if exists(select loginID from tableName where loginID = @loginID)
  begin
  return -1;
  end
  --邮箱已经被注册
  else if exists(select email from tableName where email = @email)
  begin
  return -2;
  end
  --注册成功
  else
  begin
  select @userID = isnull(max(userID),100000)+1 from tableName
  insert into tableName
  (userID,loginID,[password],userName,linkNum,address,email,createTime,status)
  values
  (@userID,@loginID,@password,'','','',@email,getdate(),1)
  return @userID
  end



  –1.给表中字段添加描述信息
  Create table T2 (id int , name char (20))
  GO
  EXEC sp_addextendedproperty 'MS_Description', 'Employee ID', 'user', dbo, 'table', T2, 'column', id
  EXEC sp_updateextendedproperty 'MS_Description', 'this is a test', 'user', dbo, 'table', T2, 'column', id
  –2.修改数据库名称
  EXEC sp_renamedb 'old_db_name', 'new_db_name'
  –3.修改数据表名称和字段名称
  EXEC sp_rename 'old_table_name', 'new_table_name'–修改数据表名称
  EXEC sp_rename 'table_name.[old_column_name]', 'new_column_name', 'COLUMN'–修改字段名称
  –4.给定存储过程名,获取存储过程内容
  exec sp_helptext sp_name
  /*以下是有关安全控制的系统存储过程或 SQL 语句,详细语法查阅《联机丛书》相关内容*/
  –创建新的 SQL Server 登录,使用户得以连接使用 SQL Server 身份验证的 SQL Server。
  EXEC sp_addlogin @loginame = '', @passwd = '', @defdb = '', @deflanguage = NULL, @sid = NULL, @encryptopt = NULL
  –使 Windows NT 用户或组帐户得以使用 Windows 身份验证连接到 SQL Server。
  EXEC sp_grantlogin @loginame = ''
  –删除 SQL Server 登录,以阻止使用该登录名访问 SQL Server。
  EXEC sp_droplogin @loginame = ''
  –阻止 Windows NT 用户或组连接到 SQL Server。
  EXEC sp_denylogin @loginame = ''
  –从 SQL Server 中删除用 sp_grantlogin 或 sp_denylogin 创建的 Windows NT 用户或组的登录项。
  EXEC sp_revokelogin @loginame = ''
  –更改登录的默认数据库。
  EXEC sp_defaultdb @loginame = '', @defdb = ''
  –更改登录的默认语言。
  EXEC sp_defaultlanguage @loginame = '', @language = ''
  –添加或更改 SQL Server 登录密码。
  EXEC sp_password @old = '', @new = '', @loginame = ''
  –添加服务器角色新成员。
  EXEC sp_addsrvrolemember @loginame = '', @rolename = ''
  –添加服务器角色某成员。
  EXEC sp_dropsrvrolemember @loginame = '' , @rolename = ''
  –为 SQL Server 登录或 Windows NT 用户或组在当前数据库中添加一个安全帐户,并使其能够被授予在数据库中执行活动的权限(授予默认的“public”数据库角色)。
  EXEC sp_grantdbaccess @loginame = '', @name_in_db = NULL
  –或
  EXEC sp_adduser @loginame = '', @name_in_db = NULL, @grpname = ''
  –从当前数据库中删除安全帐户。
  EXEC sp_revokedbaccess @name_in_db = ''
  –或
  EXEC sp_dropuser @name_in_db = ''
  –在当前数据库创建新数据库角色。
  EXEC sp_addrole @rolename = '', @ownername = ''
  –在当前数据库删除某数据库角色。
  EXEC sp_droprole @rolename = ''
  –在当前数据库中添加数据库角色新成员。
  EXEC sp_addrolemember @rolename = '', @membername = ''
  –在当前数据库中删除数据库角色某成员。
  EXEC sp_droprolemember @rolename = '', @membername = ''
  –权限分配给数据库角色、表、存储过程等对象
  –1、授权访问
  GRANT
  –2、拒绝访问
  DENY
  –3、取消授权或拒绝
  REVOKE
  –4、Sample(pubs):
  GRANT SELECT ON authors TO Limperator
  DENY SELECT ON authors TO Limperator
  REVOKE SELECT ON authors TO Limperator


====================数据库还原的存储过程============


  SQL code
  create proc killspid (@dbname varchar(20))
  as
  begin
  declare @sql nvarchar(500)
  declare @spid int
  set @sql='declare getspid cursor for
  select spid
  from sysprocesses
  where dbid=db_id('''+@dbname+''')'
  exec (@sql)
  open getspid
  fetch next from getspid
  into @spid
  while @@fetch_status <>-1
  begin
  exec('kill '+@spid)
  fetch next from getspid
  into @spid
  end
  close getspid
  deallocate getspid
  end
  GO

 作用:杀掉传入数据库中的活动进程以进行备份还原等独占操作

  ===================阿拉伯数字转大写中文=============

  例:输入12345,程序给出:壹万贰仟叁佰肆拾伍

  例:输入10023040,程序给出:壹仟另贰万叁仟另肆拾

  解决方案之一(在SqlServer2000中测试通过):



  SQL code
  CREATE FUNCTION fun_cgnum
  (@num INT)
  RETURNS VARCHAR(100)
  AS
  BEGIN
  DECLARE @temp INT,@res INT,@i TINYINT
  DECLARE @str VARCHAR(100),@no VARCHAR(20),@unit VARCHAR(16)
  SELECT @str='',@no='另壹贰叁肆伍陆柒捌玖',@unit='拾佰仟万拾佰仟亿'
  SET @temp=@num



  SELECT @i=0,@res=@temp%10,@temp=@temp/10
  WHILE @temp>0
  BEGIN
  IF @i=0
  SET @str=SUBSTRING(@no,@res+1,1)
  ELSE
  SET @str=SUBSTRING(@no,@res+1,1)+SUBSTRING(@unit,@i,1)+@str
  SELECT @res=@temp%10,@temp=@temp/10
  SET @i=@i+1
  END
  SET @str=SUBSTRING(@no,@res+1,1)+SUBSTRING(@unit,@i,1)+@str
  SET @str=REPLACE(@str,'另拾','另')
  SET @str=REPLACE(@str,'另佰','另')
  SET @str=REPLACE(@str,'另仟','另')
  SET @str=REPLACE(@str,'另拾','另')
  SET @str=REPLACE(@str,'另万','万')
  WHILE @i>0
  BEGIN
  SET @str=REPLACE(@str,'另另','另')
  SET @i=CHARINDEX('另另',@str)
  END
  SET @str=REPLACE(@str,'另万','万')
  SET @str=REPLACE(@str,'亿万','亿')
  IF RIGHT(@str,1)='另'
  SET @str=LEFT(@str,LEN(@str)-1)
  RETURN @str
  END
  GO

  --测试:有0和没有0的情况

  SELECT dbo.fun_cgnum(900000000),dbo.fun_cgnum(903002051),dbo.fun_cgnum(903002050)

  PS:有兴趣的朋友可以继续考虑有小数点以及添加单位(元/角/分)的情况



------------------------------------------------------------------
------------------------------------------------------------------
存储过程、存储函数的加密:WITH ENCRYPTION

 存储过程、存储函数的加密:WITH ENCRYPTION

<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->

CREATE procedure dbo.sp_XML_main

@table_name nvarchar(260)='',

@dirname nvarchar(20)=''

WITH ENCRYPTION

as

begin

....................

end

go



  存储过程、存储函数的解密(以下是一位绝世高人编写的代码)


if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_decrypt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)

drop procedure [dbo].[sp_decrypt]

GO

/*--破解函数,过程,触发器,视图.仅限于SQLSERVER2000

--作者:J9988-- All rights reserved*/

/*--调用示例

--解密指定存储过程

exec sp_decrypt 'AppSP_test'

--对所有的存储过程解密

declare tb cursor for

select name from sysobjects where xtype='P' and status>0 and name<>'sp_decrypt'


declare @name sysname

open tb

fetch next from tb into @name

while @@fetch_status=0

begin

print '/*-------存储过程 ['+@name+'] -----------*/'

exec sp_decrypt @name

fetch next from tb into @name

end

close tb

deallocate tb

--*/


if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SP_DECRYPT]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)

drop procedure [dbo].[SP_DECRYPT]

GO

CREATE PROCEDURE sp_decrypt(@objectName varchar(50))

AS

begin

set nocount on

--破解字节不受限制,适用于SQLSERVER2000存储过程,函数,视图,触发器

--修正上一版视图触发器不能正确解密错误

--发现有错,请E_MAIL:CSDNj9988@tom.com

begin tran

declare @objectname1 varchar(100),@orgvarbin varbinary(8000)

declare @sql1 nvarchar(4000),@sql2 varchar(8000),@sql3 nvarchar(4000),@sql4 nvarchar(4000)

DECLARE @OrigSpText1 nvarchar(4000), @OrigSpText2 nvarchar(4000) , @OrigSpText3 nvarchar(4000), @resultsp nvarchar(4000)

declare @i int,@status int,@type varchar(10),@parentid int

declare @colid int,@n int,@q int,@j int,@k int,@encrypted int,@number int

select @type=xtype,@parentid=parent_obj from sysobjects where id=object_id(@ObjectName)



create table #temp(number int,colid int,ctext varbinary(8000),encrypted int,status int)

insert #temp SELECT number,colid,ctext,encrypted,status FROM syscomments WHERE id = object_id(@objectName)

select @number=max(number) from #temp

set @k=0



while @k<=@number

begin

if exists(select 1 from syscomments where id=object_id(@objectname) and number=@k)

begin

if @type='P'

set @sql1=(case when @number>1 then 'ALTER PROCEDURE '+ @objectName +';'+rtrim(@k)+' WITH ENCRYPTION AS '

else 'ALTER PROCEDURE '+ @objectName+' WITH ENCRYPTION AS '

end)



if @type='TR'

begin

declare @parent_obj varchar(255),@tr_parent_xtype varchar(10)

select @parent_obj=parent_obj from sysobjects where id=object_id(@objectName)

select @tr_parent_xtype=xtype from sysobjects where id=@parent_obj

if @tr_parent_xtype='V'

begin

set @sql1='ALTER TRIGGER '+@objectname+' ON '+OBJECT_NAME(@parentid)+' WITH ENCRYPTION INSTERD OF INSERT AS PRINT 1 '

end

else

begin

set @sql1='ALTER TRIGGER '+@objectname+' ON '+OBJECT_NAME(@parentid)+' WITH ENCRYPTION FOR INSERT AS PRINT 1 '

end



end

if @type='FN' or @type='TF' or @type='IF'

set @sql1=(case @type when 'TF' then

'ALTER FUNCTION '+ @objectName+'(@a char(1)) returns @b table(a varchar(10)) with encryption as begin insert @b select @a return end '

when 'FN' then

'ALTER FUNCTION '+ @objectName+'(@a char(1)) returns char(1) with encryption as begin return @a end'

when 'IF' then

'ALTER FUNCTION '+ @objectName+'(@a char(1)) returns table with encryption as return select @a as a'

end)



if @type='V'

set @sql1='ALTER VIEW '+@objectname+' WITH ENCRYPTION AS SELECT 1 as f'



set @q=len(@sql1)

set @sql1=@sql1+REPLICATE('-',4000-@q)

select @sql2=REPLICATE('-',8000)

set @sql3='exec(@sql1'

select @colid=max(colid) from #temp where number=@k

set @n=1

while @n<=CEILING(1.0*(@colid-1)/2) and len(@sQL3)<=3996

begin

set @sql3=@sql3+'+@'

set @n=@n+1

end

set @sql3=@sql3+')'

exec sp_executesql @sql3,N'@sql1 nvarchar(4000),@ varchar(8000)',@sql1=@sql1,@=@sql2



end

set @k=@k+1

end



set @k=0

while @k<=@number

begin



if exists(select 1 from syscomments where id=object_id(@objectname) and number=@k)

begin

select @colid=max(colid) from #temp where number=@k

set @n=1



while @n<=@colid

begin

select @OrigSpText1=ctext,@encrypted=encrypted,@status=status FROM #temp WHERE colid=@n and number=@k



SET @OrigSpText3=(SELECT ctext FROM syscomments WHERE id=object_id(@objectName) and colid=@n and number=@k)

if @n=1

begin

if @type='P'

SET @OrigSpText2=(case when @number>1 then 'CREATE PROCEDURE '+ @objectName +';'+rtrim(@k)+' WITH ENCRYPTION AS '

else 'CREATE PROCEDURE '+ @objectName +' WITH ENCRYPTION AS '

end)



if @type='FN' or @type='TF' or @type='IF'

SET @OrigSpText2=(case @type when 'TF' then

'CREATE FUNCTION '+ @objectName+'(@a char(1)) returns @b table(a varchar(10)) with encryption as begin insert @b select @a return end '

when 'FN' then

'CREATE FUNCTION '+ @objectName+'(@a char(1)) returns char(1) with encryption as begin return @a end'

when 'IF' then

'CREATE FUNCTION '+ @objectName+'(@a char(1)) returns table with encryption as return select @a as a'

end)



if @type='TR'

begin



if @tr_parent_xtype='V'

begin

set @OrigSpText2='CREATE TRIGGER '+@objectname+' ON '+OBJECT_NAME(@parentid)+' WITH ENCRYPTION INSTEAD OF INSERT AS PRINT 1 '

end

else

begin

set @OrigSpText2='CREATE TRIGGER '+@objectname+' ON '+OBJECT_NAME(@parentid)+' WITH ENCRYPTION FOR INSERT AS PRINT 1 '

end



end



if @type='V'

set @OrigSpText2='CREATE VIEW '+@objectname+' WITH ENCRYPTION AS SELECT 1 as f'



set @q=4000-len(@OrigSpText2)

set @OrigSpText2=@OrigSpText2+REPLICATE('-',@q)

end

else

begin

SET @OrigSpText2=REPLICATE('-', 4000)

end

SET @i=1



SET @resultsp = replicate(N'A', (datalength(@OrigSpText1) / 2))



WHILE @i<=datalength(@OrigSpText1)/2

BEGIN



SET @resultsp = stuff(@resultsp, @i, 1, NCHAR(UNICODE(substring(@OrigSpText1, @i, 1)) ^

(UNICODE(substring(@OrigSpText2, @i, 1)) ^

UNICODE(substring(@OrigSpText3, @i, 1)))))

SET @i=@i+1

END

set @orgvarbin=cast(@OrigSpText1 as varbinary(8000))

set @resultsp=(case when @encrypted=1

then @resultsp

else convert(nvarchar(4000),case when @status&2=2 then uncompress(@orgvarbin) else @orgvarbin end)

end)

print @resultsp



set @n=@n+1

end



end

set @k=@k+1

end



drop table #temp

rollback tran

end

评分

参与人数 1努力值 +20 收起 理由
Admin + 20 非常好的资料!

查看全部评分

发表于 2010/3/27 07:50:14 | 显示全部楼层
全是英文?
偶没有基础啊,26个英文母偶认识,放在一起就不认识了,
编程,太难了点,语法到是有一些了解,
看来ERP真不是一般人能玩了的,
幸好我不是一般人,
一般起来不是人。
发表于 2010/3/30 19:36:01 | 显示全部楼层
好东西,学习一下哈
发表于 2010/5/13 17:27:02 | 显示全部楼层
能整理成文档就好了。这个太乱
发表于 2011/4/5 10:41:24 | 显示全部楼层
OK ERP 100
发表于 2011/5/10 09:17:52 | 显示全部楼层
回复 monya 的帖子

感谢分享,挺好的东西
发表于 2011/5/13 20:40:59 | 显示全部楼层
俺喜欢,收下了
发表于 2011/5/13 20:41:48 | 显示全部楼层
谢谢楼主,顶下
发表于 2011/7/19 12:35:19 | 显示全部楼层
哎呀,好资料,收藏了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|手机版|壹佰网 ERP100 ( 京ICP备19053597号-2 )

Copyright © 2005-2012 北京海之大网络技术有限责任公司 服务器托管由互联互通
手机:13911575376
网站技术点击发送消息给对方83569622   广告&合作 点击发送消息给对方27675401   点击发送消息给对方634043306   咨询及人才点击发送消息给对方138011526

GMT+8, 2025/11/30 06:18 , Processed in 0.025366 second(s), 17 queries , File On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表