java,php,asp.net,linux,javascript,mysql,mssql,oracle,编程

SQL几个函数

转载请注明:http://www.pmjava.com/Article/ShowInfo.asp?ID=56720
USE [MES]
GO
/****** Object:  StoredProcedure [dbo].[H_SqlToExcel]    Script Date: 11/24/2008 13:22:07 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
ALTER PROC [dbo].[H_SqlToExcel]
(
@Path varchar(100),--文件存放路径
@Fname varchar(100),--文件名字
@SheetName varchar(80),---工作表名字
@SqlStr varchar(8000)--查询语句,如果查询语句中使用了order by ,请加上top 100 percent,注意,如果导出表/视图,用上面的存储过程
)
AS
SET NOCOUNT ON

declare @sql varchar(8000)
declare @obj int--OLE对象
declare @constr varchar(8000)
declare @err int
declare @out int
declare @fdlist varchar(8000)
declare @tbname sysname--临时表
declare @Src nvarchar(200)
declare @Desc nvarchar(200)

set @tbname='##tmp_'+convert(varchar(38),newid())

exec('select * into ['+@tbname +'] from '+'('+@sqlStr+') A')

select @fdlist = ''

set @sql= @path+@fname
set @constr='DRIVER={Microsoft Excel Driver (*.xls)};DSN='''';READONLY=FALSE'
+';CREATE_DB="'+@sql+'";DBQ='+@sql

--生成Excel的列
set @sql = ''
select @sql = @sql+','+'['+a.name+'] '+ case when b.name like '%char' then case when a.length >255 then 'memo' else 'text('+cast(a.length as varchar)+')' end
when b.name like '%int' or b.name='bit' then 'int'
when b.name like '%datetime' then 'datetime'
when b.name like '%money' then 'money'
when b.name like '%text' then 'memo'
else b.name
end,
@fdlist = @fdlist+','+'['+a.name+']'
from tempdb..syscolumns a join tempdb..systypes b on a.xtype = b.xusertype
where b.name not in('image','uniqueidentifier','sql_variant','varbinary','binary','timestamp')
and id in(select id from tempdb..sysobjects where name = @tbname) order by colorder
if @@rowcount=0 return

set @fdlist = substring(@fdlist,2,8000)

--连接数据库
exec @err=sp_oacreate 'adodb.connection',@obj out
if @err <> 0 goto lberror
exec @err=sp_oamethod @obj,'open',null,@constr
if @err <> 0 goto lberror
--创建工作薄
select @sql='create table ['+@sheetname
+']('+substring(@sql,2,8000)+')'

exec @err=sp_oamethod @obj,'execute',@out out,@sql--@sql为excute方法提供参数

if @err <> 0 goto lberror

exec @err=sp_oadestroy @obj

--导入数据
set @sql='openrowset(''MICROSOFT.JET.OLEDB.4.0'',''Excel 8.0;HDR=YES
;DATABASE=
'+@path+@fname+''',['+@sheetname+'$])'
--print @sql
exec ('insert into '+@sql+'('+@fdlist+') select '+@fdlist+' from ['+@tbname+']')

exec('drop table ['+@tbname+']')
return


lberror:
exec sp_oageterrorinfo 0,@src out,@desc out

lbexit:
select cast(@err as varbinary(4)) as 错误号
,
@src as 错误源,@desc as 错误描述
select @sql,@constr,@fdlist
USE [MES]
GO
/****** Object:  StoredProcedure [dbo].[p_backupdb]    Script Date: 11/24/2008 13:24:53 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER proc [dbo].[p_backupdb]
@dbname sysname='', --要备份的数据库名称,不指定则备份当前数据库
@bkpath nvarchar(260)='', --备份文件的存放目录,不指定则使用SQL默认的备份目录
@bkfname nvarchar(260)='', --备份文件名,文件名中可以用\DBNAME\代表数据库名,\DATE\代表日期,\TIME\代表时间
@bktype nvarchar(10)='DB', --备份类型:'DB'备份数据库,'DF' 差异备份,'LOG' 日志备份
@appendfile bit=1 --追加/覆盖备份文件
as
declare @sql varchar(8000)
if isnull(@dbname,'')='' set @dbname=db_name()
if isnull(@bkpath,'')='' set @bkpath=dbo.f_getdbpath(null)
if isnull(@bkfname,'')='' set @bkfname='\DBNAME\_\DATE\_\TIME\.BAK'
set @bkfname=replace(replace(replace(@bkfname,'\DBNAME\',@dbname)
,
'\DATE\',convert(varchar,getdate(),112))
,
'\TIME\',replace(convert(varchar,getdate(),108),':',''))
set @sql='backup '+case @bktype when 'LOG' then 'log ' else 'database ' end +@dbname
+' to disk='''+@bkpath+@bkfname
+''' with '+case @bktype when 'DF' then 'DIFFERENTIAL,' else '' end
+case @appendfile when 1 then 'NOINIT' else 'INIT' end
print @sql
exec(@sql)

                                                                  

posted on 2009-06-12 18:20 rrong_m 阅读(155) 评论(0)  编辑  收藏

<2009年6月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

随笔档案

文章分类

文章档案

java编程

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜