锘??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 --濡傛灉闈炴眽瀛楀瓧絎︼紝榪斿洖鍘熷瓧絎? 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'絀?
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'綾?
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. 灞炴?
灞炴у悕 綾誨瀷 鎻忚堪
--------------------------------- ------------------- --------------------
CopyAllDefaults Boolean 鎵鏈夐粯璁ゅ?BR>CopyAllObjects Boolean 鎵鏈夊璞?BR>CopyAllRules Boolean 鎵鏈夎鍒?BR>CopyAllStoredProcedures Boolean 鎵鏈夊瓨鍌ㄨ繃紼?BR>CopyAllTables Boolean 鎵鏈夎〃
CopyAllTriggers Boolean 鎵鏈夎Е鍙戝櫒
CopyAllUserDefinedDatatypes Boolean 鎵鏈夌敤鎴瘋嚜瀹氫箟綾誨瀷
CopyAllViews Boolean 鎵鏈夎鍥?BR>CopyData Boolean 鎵鏈夋暟鎹?BR>DestDatabase String 鐩爣瀵硅薄鏁版嵁搴?BR>DestLogin String 鐩爣鏁版嵁搴撶櫥闄嗙敤鎴峰悕
DestPassword String 鐩爣鏁版嵁搴撶櫥闄嗗瘑鐮?BR>DestServer String 鐩爣鏈嶅姟鍣?BR>DestUseTrustedConnection Boolean 鐢ㄦ埛淇′換榪炴帴
DropDestObjectsFirst Boolean 鏄惁鍏堝垹闄ょ洰鏍囧璞?BR>IncludeDependencies Boolean 鏄惁鍖呭惈渚濋潬瀵硅薄
ScriptType Boolean 鑴氭湰綾誨瀷
-- 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 涓嬌鐢⊿QLDMO.Transfer 瀹炵幇鏁版嵁榪佺Щ
瀛樺偍榪囩▼瀹炵幇婧愭暟鎹簱鍒扮洰鏍囨暟鎹簱鐨勫璞″拰鏁版嵁鐨勫鍒?BR> 瑕佹眰婧愭暟鎹簱鍜岀洰鏍囨暟鎹簱鍦ㄥ悓涓鏈嶅姟鍣?BR> 濡傛灉鏄瀹炵幇涓嶅悓鏈嶅姟鍣ㄤ箣闂寸殑澶嶅埗錛屽垯闇瑕佸鍔犻獙璇佷俊鎭?BR>--閭瑰緩 2005.07(寮曠敤璇蜂繚鐣欐淇℃伅)--*/
/*--璋冪敤紺轟緥
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'',--澶嶅埗鐨勫璞$被鍨嬶紝鍙互鏄笅鍒楀瓧絎︿覆鍒楄〃錛?BR> -- O 鎵鏈夊璞★紝D 榛樿鍊鹼紝R 瑙勫垯錛孭 瀛樺偍榪囩▼
-- T 琛紝TR 瑙﹀彂鍣紝DT 鐢ㄦ埛瀹氫箟鏁版嵁綾誨瀷
-- V 瑙嗗浘錛孌ATA 鏁版嵁錛孌EL 鍒犻櫎鐩爣瀵硅薄
@Source_DB sysname=N'', --婧愭暟鎹簱
@ServerName sysname=N'', --鏈嶅姟鍣ㄥ悕
@UserName sysname=N'', --鐢ㄦ埛鍚嶏紝涓嶆寚瀹氬垯琛ㄧず浣跨敤 Windows 韜喚鐧誨綍
@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
--榪炴帴鏈嶅姟鍣?BR>IF ISNULL(@UserName,N'')=N'' --浣跨敤 Windows 韜喚鐧誨綍
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
--璁劇疆榪炴帴鐢ㄦ埛
IF ISNULL(@UserName,N'')=N'' --浣跨敤 Windows 韜喚鐧誨綍
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
--緇撴潫
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