??xml version="1.0" encoding="utf-8" standalone="yes"?> Create function fun_getPY declare @word nchar(1),@PY nvarchar(4000) set @PY='' while len(@str)>0 --如果非汉字字W,q回原字W? return @PY end
(
@str nvarchar(4000)
)
returns nvarchar(4000)
as
begin
begin
set @word=left(@str,1)
set @PY=@PY+(case when unicode(@word) between 19968 and 19968+20901
then (
select top 1 PY
from
(
select 'A' as PY,N'? as word
union all select 'B',N'?
union all select 'C',N'?
union all select 'D',N'?
union all select 'E',N'?
union all select 'F',N'?
union all select 'G',N'?
union all select 'H',N'?
union all select 'J',N'?
union all select 'K',N'I?
union all select 'L',N'?
union all select 'M',N'旀'
union all select 'N',N'?
union all select 'O',N'?
union all select 'P',N'?
union all select 'Q',N'?
union all select 'R',N'?
union all select 'S',N'?
union all select 'T',N'c?
union all select 'W',N'?
union all select 'X',N'?
union all select 'Y',N'?
union all select 'Z',N'?
) T
where word>=@word collate Chinese_PRC_CS_AS_KS_WS
order by PY ASC
)
else @word
end)
set @str=right(@str,len(@str)-1)
end
]]>
This stored procedure can be used to insert the result set of the
particular select statement into Excel file (c:\ImportToExcel.xls,
by default).
You can pass the server name, user name, user password, the select
statement to execute, and the file name to store the results set,
as in the example below:
EXEC ExportToExcel @server = '.',
@uname = 'sa',
@QueryText = 'SELECT au_fname FROM pubs..authors',
@filename = 'c:\ImportToExcel.xls'
/*
Version: SQL Server 7.0/2000
Created by: Alexander Chigrik
- all about MS SQL
(SQL Server Articles, FAQ, Scripts, Tips and Test Exams).
This stored procedure can be used to insert the result set of the
particular select statement into Excel file (c:\ImportToExcel.xls,
by default).
You can pass the server name, user name, user password, the select
statement to execute, and the file name to store the results set,
as in the example below:
EXEC ExportToExcel @server = '.',
@uname = 'sa',
@QueryText = 'SELECT au_fname FROM pubs..authors',
@filename = 'c:\ImportToExcel.xls'
*/
IF OBJECT_ID('ExportToExcel') IS NOT NULL DROP PROC ExportToExcel
GO
CREATE PROCEDURE ExportToExcel (
@server sysname = null,
@uname sysname = null,
@pwd sysname = null,
@QueryText varchar(200) = null,
@filename varchar(200) = 'c:\ImportToExcel.xls'
)
AS
DECLARE @SQLServer int,
@QueryResults int,
@CurrentResultSet int,
@object int,
@WorkBooks int,
@WorkBook int,
@Range int,
@hr int,
@Columns int,
@Rows int,
@indColumn int,
@indRow int,
@off_Column int,
@off_Row int,
@code_str varchar(100),
@result_str varchar(255)
IF @QueryText IS NULL
BEGIN
PRINT 'Set the query string'
RETURN
END
-- Sets the server to the local server
IF @server IS NULL SELECT @server = @@servername
-- Sets the username to the current user name
IF @uname IS NULL SELECT @uname = SYSTEM_USER
SET NOCOUNT ON
EXEC @hr = sp_OACreate 'SQLDMO.SQLServer', @SQLServer OUT
IF @hr <> 0
BEGIN
PRINT 'error create SQLDMO.SQLServer'
RETURN
END
-- Connect to the SQL Server
IF @pwd IS NULL
BEGIN
EXEC @hr = sp_OAMethod @SQLServer, 'Connect', null, @server, @uname
IF @hr <> 0
BEGIN
PRINT 'error Connect'
RETURN
END
END
ELSE
BEGIN
EXEC @hr = sp_OAMethod @SQLServer, 'Connect', null, @server, @uname, @pwd
IF @hr <> 0
BEGIN
PRINT 'error Connect'
RETURN
END
END
SELECT @result_str = 'ExecuteWithResults("' + @QueryText + '")'
EXEC @hr = sp_OAMethod @SQLServer, @result_str, @QueryResults OUT
IF @hr <> 0
BEGIN
PRINT 'error with method ExecuteWithResults'
RETURN
END
EXEC @hr = sp_OAMethod @QueryResults, 'CurrentResultSet', @CurrentResultSet OUT
IF @hr <> 0
BEGIN
PRINT 'error get CurrentResultSet'
RETURN
END
EXEC @hr = sp_OAMethod @QueryResults, 'Columns', @Columns OUT
IF @hr <> 0
BEGIN
PRINT 'error get Columns'
RETURN
END
EXEC @hr = sp_OAMethod @QueryResults, 'Rows', @Rows OUT
IF @hr <> 0
BEGIN
PRINT 'error get Rows'
RETURN
END
EXEC @hr = sp_OACreate 'Excel.Application', @object OUT
IF @hr <> 0
BEGIN
PRINT 'error create Excel.Application'
RETURN
END
EXEC @hr = sp_OAGetProperty @object, 'WorkBooks', @WorkBooks OUT
IF @hr <> 0
BEGIN
PRINT 'error create WorkBooks'
RETURN
END
EXEC @hr = sp_OAGetProperty @WorkBooks, 'Add', @WorkBook OUT
IF @hr <> 0
BEGIN
PRINT 'error with method Add'
RETURN
END
EXEC @hr = sp_OAGetProperty @object, 'Range("A1")', @Range OUT
IF @hr <> 0
BEGIN
PRINT 'error create Range'
RETURN
END
SELECT @indRow = 1
SELECT @off_Row = 0
SELECT @off_Column = 1
WHILE (@indRow <= @Rows)
BEGIN
SELECT @indColumn = 1
WHILE (@indColumn <= @Columns)
BEGIN
EXEC @hr = sp_OAMethod @QueryResults, 'GetColumnString', @result_str OUT, @indRow, @indColumn
IF @hr <> 0
BEGIN
PRINT 'error get GetColumnString'
RETURN
END
EXEC @hr = sp_OASetProperty @Range, 'value', @result_str
IF @hr <> 0
BEGIN
PRINT 'error set value'
RETURN
END
EXEC @hr = sp_OAGetProperty @Range, 'Offset', @Range OUT, @off_Row, @off_Column
IF @hr <> 0
BEGIN
PRINT 'error get Offset'
RETURN
END
SELECT @indColumn = @indColumn + 1
END
SELECT @indRow = @indRow + 1
SELECT @code_str = 'Range("A' + LTRIM(str(@indRow)) + '")'
EXEC @hr = sp_OAGetProperty @object, @code_str, @Range OUT
IF @hr <> 0
BEGIN
PRINT 'error create Range'
RETURN
END
END
SELECT @result_str = 'exec master..xp_cmdshell ''del ' + @filename + ''', no_output'
EXEC(@result_str)
SELECT @result_str = 'SaveAs("' + @filename + '")'
EXEC @hr = sp_OAMethod @WorkBook, @result_str
IF @hr <> 0
BEGIN
PRINT 'error with method SaveAs'
RETURN
END
EXEC @hr = sp_OAMethod @WorkBook, 'Close'
IF @hr <> 0
BEGIN
PRINT 'error with method Close'
RETURN
END
EXEC @hr = sp_OADestroy @object
IF @hr <> 0
BEGIN
PRINT 'error destroy Excel.Application'
RETURN
END
EXEC @hr = sp_OADestroy @SQLServer
IF @hr <> 0
BEGIN
PRINT 'error destroy SQLDMO.SQLServer'
RETURN
END
GO
--用法
use master
exec killspid '数据库名'
-- Transfer对象的重要属?
-- 1. 属?
属性名 cd 描述
--------------------------------- ------------------- --------------------
CopyAllDefaults Boolean 所有默认?BR>CopyAllObjects Boolean 所有对?BR>CopyAllRules Boolean 所有规?BR>CopyAllStoredProcedures Boolean 所有存储过E?BR>CopyAllTables Boolean 所有表
CopyAllTriggers Boolean 所有触发器
CopyAllUserDefinedDatatypes Boolean 所有用戯定义cd
CopyAllViews Boolean 所有视?BR>CopyData Boolean 所有数?BR>DestDatabase String 目标对象数据?BR>DestLogin String 目标数据库登陆用户名
DestPassword String 目标数据库登陆密?BR>DestServer String 目标服务?BR>DestUseTrustedConnection Boolean 用户信Qq接
DropDestObjectsFirst Boolean 是否先删除目标对?BR>IncludeDependencies Boolean 是否包含依靠对象
ScriptType Boolean 脚本cd
-- 2. 重要Ҏ:
Ҏ名称 功能描述
--------------------------- --------------------------
AddObject 增加对象
AddObjectByName 通过对象名称增加对象
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[P_CopyDB]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[P_CopyDB]
GO
/*-- ?nbsp;SQLServer 中用SQLDMO.Transfer 实现数据q移
存储q程实现源数据库到目标数据库的对象和数据的复?BR> 要求源数据库和目标数据库在同一服务?BR> 如果是要实现不同服务器之间的复制Q则需要增加验证信?BR>--邹徏 2005.07(引用请保留此信息)--*/
/*--调用CZ
CREATE DATABASE test
EXEC P_CopyDB @Source_DB='northwind',@Des_DB='test'
DROP DATABASE test
--*/
CREATE PROCEDURE P_CopyDB
@Des_DB sysname, --目标数据?BR>@Obj_Type nvarchar(4000)=N'',--复制的对象类型,可以是下列字W串列表Q?BR> -- O 所有对象,D 默认|R 规则QP 存储q程
-- T 表,TR 触发器,DT 用户定义数据cd
-- V 视图QDATA 数据QDEL 删除目标对象
@Source_DB sysname=N'', --源数据库
@ServerName sysname=N'', --服务器名
@UserName sysname=N'', --用户名,不指定则表示使用 Windows w䆾d
@pwd sysname=N'' --密码
AS
SET NOCOUNT ON
DECLARE @srvid int,@Dbid int,@S_dbid int,@D_dbid int,@TransferID int,
@err int,@src varchar(255), @desc varchar(255)
IF ISNULL(@ServerName,N'')=N'' SET @ServerName=@@SERVERNAME
IF ISNULL(@Source_DB,N'')=N'' SET @Source_DB=DB_NAME()
--创徏sqldmo对象·
EXEC @err=sp_oacreate 'sqldmo.sqlserver',@srvid OUT
IF @err<>0 GOTO lb_Err
--q接服务?BR>IF ISNULL(@UserName,N'')=N'' --使用 Windows w䆾d
BEGIN
EXEC @err=sp_oasetproperty @srvid,'loginsecure',-1
IF @err<>0 GOTO lb_Err
EXEC @err=sp_oamethod @srvid,'connect',NULL,@servername
END
ELSE
EXEC @err=sp_oamethod @srvid,'connect',NULL,@servername,@UserName,@pwd
IF @err<>0 GOTO lb_Err
--获取数据库集
EXEC @err=sp_oagetproperty @srvid,'databases',@Dbid OUT
IF @err<>0 GOTO lb_Err
--选择源数据库
EXEC @err=sp_oamethod @Dbid,'item',@S_dbid OUT,@Source_DB
IF @err<>0 GOTO lb_Err
--选择目标数据?nbsp;
EXEC @err=sp_oamethod @Dbid,'item',@D_dbid OUT,@Des_DB
IF @err<>0 GOTO lb_Err
--讄复制的对?BR>EXEC @err=sp_oacreate 'SQLDMO.Transfer',@TransferID OUT
IF @err<>0 GOTO lb_Err
--讄目标服务器信?BR>EXEC @err=sp_oasetproperty @TransferID,'DestServer',@ServerName
IF @err<>0 GOTO lb_Err
--讄q接用户
IF ISNULL(@UserName,N'')=N'' --使用 Windows w䆾d
BEGIN
EXEC @err=sp_oasetproperty @TransferID,'DestUseTrustedConnection',1
IF @err<>0 GOTO lb_Err
END
ELSE
BEGIN
EXEC @err=sp_oasetproperty @TransferID,'DestLogin',@UserName
IF @err<>0 GOTO lb_Err
EXEC @err=sp_oasetproperty @TransferID,'DestPassword',@pwd
IF @err<>0 GOTO lb_Err
END
--讄复制对象信息
EXEC @err=sp_oasetproperty @TransferID,'DestDatabase',@Des_DB
IF @err<>0 GOTO lb_Err
DECLARE tb CURSOR FAST_FORWARD LOCAL
FOR
SELECT Name FROM(
SELECT KeyWord=N',D,', Name=N'CopyAllDefaults' UNION ALL
SELECT KeyWord=N',O,', Name=N'CopyAllObjects' UNION ALL
SELECT KeyWord=N',R,', Name=N'CopyAllRules' UNION ALL
SELECT KeyWord=N',P,', Name=N'CopyAllStoredProcedures' UNION ALL
SELECT KeyWord=N',T,', Name=N'CopyAllTables' UNION ALL
SELECT KeyWord=N',TR,', Name=N'CopyAllTriggers' UNION ALL
SELECT KeyWord=N',DT,', Name=N'CopyAllUserDefinedDatatypes' UNION ALL
SELECT KeyWord=N',V,', Name=N'CopyAllViews' UNION ALL
SELECT KeyWord=N',DATA,',Name=N'CopyData' UNION ALL
SELECT KeyWord=N',DEL,', Name=N'DropDestObjectsFirst'
)A WHERE CHARINDEX(KeyWord,
CASE WHEN ISNULL(@Obj_Type,N'')='' THEN ',O,DATA,' ELSE @Obj_Type END)>0
OPEN tb
FETCH tb INTO @src
WHILE @@FETCH_STATUS=0
BEGIN
EXEC @err=sp_oasetproperty @TransferID,@src,1
IF @err<>0 GOTO lb_Err
FETCH tb INTO @src
END
CLOSE tb
DEALLOCATE tb
--复制对象
EXEC @err=sp_oamethod @S_dbid,'Transfer',null,@TransferID
IF @err<>0 GOTO lb_Err
--l束
SET @err=0
GOTO lb_Exit
--错误处理
lb_Err:
EXEC sp_oageterrorinfo NULL, @src OUT, @desc OUT
RAISERROR(N'错误~号 %#x, 错误?nbsp;"%s", 错误描述 "%s"',16,1,@err,@src,@desc)
RETURN -1
lb_Exit:
EXEC sp_OADestroy @Dbid
EXEC sp_OADestroy @srvid
EXEC sp_OADestroy @TransferID
RETURN @err
GO
This stored procedure can be used to insert the result set of the
particular select statement into Excel file (c:\ImportToExcel.xls,
by default).
You can pass the server name, user name, user password, the select
statement to execute, and the file name to store the results set,
as in the example below:
EXEC ExportToExcel @server = '.',
@uname = 'sa',
@QueryText = 'SELECT au_fname FROM pubs..authors',
@filename = 'c:\ImportToExcel.xls'
/*
Version: SQL Server 7.0/2000
Created by: Alexander Chigrik
- all about MS SQL
(SQL Server Articles, FAQ, Scripts, Tips and Test Exams).
This stored procedure can be used to insert the result set of the
particular select statement into Excel file (c:\ImportToExcel.xls,
by default).
You can pass the server name, user name, user password, the select
statement to execute, and the file name to store the results set,
as in the example below:
EXEC ExportToExcel @server = '.',
@uname = 'sa',
@QueryText = 'SELECT au_fname FROM pubs..authors',
@filename = 'c:\ImportToExcel.xls'
*/
IF OBJECT_ID('ExportToExcel') IS NOT NULL DROP PROC ExportToExcel
GO
CREATE PROCEDURE ExportToExcel (
@server sysname = null,
@uname sysname = null,
@pwd sysname = null,
@QueryText varchar(200) = null,
@filename varchar(200) = 'c:\ImportToExcel.xls'
)
AS
DECLARE @SQLServer int,
@QueryResults int,
@CurrentResultSet int,
@object int,
@WorkBooks int,
@WorkBook int,
@Range int,
@hr int,
@Columns int,
@Rows int,
@indColumn int,
@indRow int,
@off_Column int,
@off_Row int,
@code_str varchar(100),
@result_str varchar(255)
IF @QueryText IS NULL
BEGIN
PRINT 'Set the query string'
RETURN
END
-- Sets the server to the local server
IF @server IS NULL SELECT @server = @@servername
-- Sets the username to the current user name
IF @uname IS NULL SELECT @uname = SYSTEM_USER
SET NOCOUNT ON
EXEC @hr = sp_OACreate 'SQLDMO.SQLServer', @SQLServer OUT
IF @hr <> 0
BEGIN
PRINT 'error create SQLDMO.SQLServer'
RETURN
END
-- Connect to the SQL Server
IF @pwd IS NULL
BEGIN
EXEC @hr = sp_OAMethod @SQLServer, 'Connect', null, @server, @uname
IF @hr <> 0
BEGIN
PRINT 'error Connect'
RETURN
END
END
ELSE
BEGIN
EXEC @hr = sp_OAMethod @SQLServer, 'Connect', null, @server, @uname, @pwd
IF @hr <> 0
BEGIN
PRINT 'error Connect'
RETURN
END
END
SELECT @result_str = 'ExecuteWithResults("' + @QueryText + '")'
EXEC @hr = sp_OAMethod @SQLServer, @result_str, @QueryResults OUT
IF @hr <> 0
BEGIN
PRINT 'error with method ExecuteWithResults'
RETURN
END
EXEC @hr = sp_OAMethod @QueryResults, 'CurrentResultSet', @CurrentResultSet OUT
IF @hr <> 0
BEGIN
PRINT 'error get CurrentResultSet'
RETURN
END
EXEC @hr = sp_OAMethod @QueryResults, 'Columns', @Columns OUT
IF @hr <> 0
BEGIN
PRINT 'error get Columns'
RETURN
END
EXEC @hr = sp_OAMethod @QueryResults, 'Rows', @Rows OUT
IF @hr <> 0
BEGIN
PRINT 'error get Rows'
RETURN
END
EXEC @hr = sp_OACreate 'Excel.Application', @object OUT
IF @hr <> 0
BEGIN
PRINT 'error create Excel.Application'
RETURN
END
EXEC @hr = sp_OAGetProperty @object, 'WorkBooks', @WorkBooks OUT
IF @hr <> 0
BEGIN
PRINT 'error create WorkBooks'
RETURN
END
EXEC @hr = sp_OAGetProperty @WorkBooks, 'Add', @WorkBook OUT
IF @hr <> 0
BEGIN
PRINT 'error with method Add'
RETURN
END
EXEC @hr = sp_OAGetProperty @object, 'Range("A1")', @Range OUT
IF @hr <> 0
BEGIN
PRINT 'error create Range'
RETURN
END
SELECT @indRow = 1
SELECT @off_Row = 0
SELECT @off_Column = 1
WHILE (@indRow <= @Rows)
BEGIN
SELECT @indColumn = 1
WHILE (@indColumn <= @Columns)
BEGIN
EXEC @hr = sp_OAMethod @QueryResults, 'GetColumnString', @result_str OUT, @indRow, @indColumn
IF @hr <> 0
BEGIN
PRINT 'error get GetColumnString'
RETURN
END
EXEC @hr = sp_OASetProperty @Range, 'value', @result_str
IF @hr <> 0
BEGIN
PRINT 'error set value'
RETURN
END
EXEC @hr = sp_OAGetProperty @Range, 'Offset', @Range OUT, @off_Row, @off_Column
IF @hr <> 0
BEGIN
PRINT 'error get Offset'
RETURN
END
SELECT @indColumn = @indColumn + 1
END
SELECT @indRow = @indRow + 1
SELECT @code_str = 'Range("A' + LTRIM(str(@indRow)) + '")'
EXEC @hr = sp_OAGetProperty @object, @code_str, @Range OUT
IF @hr <> 0
BEGIN
PRINT 'error create Range'
RETURN
END
END
SELECT @result_str = 'exec master..xp_cmdshell ''del ' + @filename + ''', no_output'
EXEC(@result_str)
SELECT @result_str = 'SaveAs("' + @filename + '")'
EXEC @hr = sp_OAMethod @WorkBook, @result_str
IF @hr <> 0
BEGIN
PRINT 'error with method SaveAs'
RETURN
END
EXEC @hr = sp_OAMethod @WorkBook, 'Close'
IF @hr <> 0
BEGIN
PRINT 'error with method Close'
RETURN
END
EXEC @hr = sp_OADestroy @object
IF @hr <> 0
BEGIN
PRINT 'error destroy Excel.Application'
RETURN
END
EXEC @hr = sp_OADestroy @SQLServer
IF @hr <> 0
BEGIN
PRINT 'error destroy SQLDMO.SQLServer'
RETURN
END
GO
Set NoCount On
Begin Tran
open curAlterInfo
Fetch curAlterInfo Into @AlterFieldName, @Length, @IsNullable, @AlterTableName
While @@Fetch_Status=0
Begin
print @AlterTableName
--檢查修改的表是否有主?BR> If Exists(Select Name From SysObjects Where xType = 'PK'
and Parent_Obj = (Select id From SysObjects Where Name = @AlterTableName))
Begin
Set @TmpTableName = @AlterTableName
-- 取得主鍵?BR> Select @PkName = Name From SysObjects Where xType = 'PK'
and Parent_Obj = (Select id From SysObjects Where Name = @AlterTableName)
Set @PkFieldName = ''
-- 主鍵字段
Declare curPkFieldName Cursor For Select b.Name From SysIndexKeys a, SysColumns b
Where a.id = (Select id From SysIndexes Where Name = @PkName)
and a.indid = 1 and a.colid = b.colid and a.id = b.id
-- 取得所有的主鍵字段
Open curPkFieldName
Fetch curPkFieldName Into @TmpFieldName
While @@fetch_status = 0
Begin
Set @PkFieldName = @PkFieldName + @TmpFieldName + ','
Fetch curPkFieldName Into @TmpFieldName
End
Close curPkFieldName
Deallocate curPkFieldName
-- 刪除舊主?BR> Set @Sql = 'ALTER TABLE '+ @AlterTableName + ' DROP CONSTRAINT ' + @PkName
Print @Sql
Exec(@Sql)
end
-- 修改字段
Set @Sql = 'ALTER TABLE ' + @AlterTableName + ' ALTER COLUMN ' + @AlterFieldName
+ ' NVARCHAR( ' + CAST(@Length AS NVARCHAR) + ')'
-- 是否允許為空
if @IsNullable = 0
Set @Sql = @Sql + ' NOT NULL'
Print @sql
Exec(@sql)
Fetch curAlterInfo Into @AlterFieldName, @Length, @IsNullable, @AlterTableName
-- 創徏主鍵
If (@AlterTableName <> @TmpTableName or @@fetch_status <> 0) and @PkFieldName <> ''
Begin
Set @PkFieldName = Left(@PkFieldName, Len(@PkFieldName) - 1)
Set @Sql = ' ALTER TABLE ' + @TmpTableName + ' ADD CONSTRAINT ' + @PkName
+ ' PRIMARY KEY CLUSTERED(' + @PkFieldName + ') ON [PRIMARY]'
Print @Sql
Exec(@Sql)
print '-----------------------------'
Set @PkFieldName = ''
End
End
Close curAlterInfo
Deallocate curAlterInfo
If @@Error > 0
Rollback Tran
Else
Commit Tran
Set NoCount Off