刷新SP到一数据库方便制作XSD文件
发表于:2007-07-02来源:作者:点击数:
标签:
---------------------------------------------------------------------------------- --刷新SP到 数据库 -- ---- --Old来源数据库名-- --New目标数据库名-- ---- --目标数据库中表名为来源数据库中的用户自定义SP、FN等(可按提示添加--详细提示以后加)--
----------------------------------------------------------------------------------
-- 刷新SP到
数据库 --
-- --
-- &Old& 来源数据库名 --
-- &New& 目标数据库名 --
-- --
-- 目标数据库中表名为来源数据库中的用户自定义SP、FN等(可按提示添加--详细提示以后加) --
-- 表中的列名则为其参数 --
-- --
-- 黄宗银 --
-- 2005.01.19 --
----------------------------------------------------------------------------------
ALTER PROCEDURE dbo.P_Ref
AS
DECLARE @SQL nvarchar(4000)
-- 不存在数据库则创建
IF NOT EXISTS (
SELECT [name]
FROM master.dbo.sysdatabases
WHERE [name] = @#&New&@#
)
BEGIN
CREATE DATABASE &New&
END
-- 取出SP、FN、TF其name、id
DECLARE @Tbl CURSOR
SET @Tbl = CURSOR LOCAL SCROLL FOR
SELECT [name], [id]
FROM &Old&.dbo.sysobjects
-- 要增加刷新类型请修改这里
WHERE ([name] LIKE @#P%@# OR [name] LIKE @#F%@# OR [name] LIKE @#TF%@#)
AND (type = @#P@# OR type = @#FN@# OR type = @#TF@# )
DECLARE @TblName nvarchar(100)
DECLARE @TblID int
-- 以@TblName为名创建表
OPEN @Tbl
FETCH NEXT FROM @Tbl INTO @TblName, @TblID
WHILE( @@FETCH_STATUS = 0 )
BEGIN
-- 已存在该表则删除
IF EXISTS
(
SELECT [name] FROM &New&.dbo.sysobjects
WHERE [name] = @TblName
AND type = @#U@#
)
BEGIN
SET @SQL = @#DROP TABLE @# + @#&New&@# + @#.dbo.@# + @TblName
EXEC SP_ExecuteSQL @SQL
IF( @@ERROR <> 0 )
BEGIN
RAISERROR( @#删除已存在的表%s失败!@#, 11, 1, @TblName )
RETURN
END
END
-- 如果没有参数则跳过
IF( (SELECT Count(*) FROM dbo.syscolumns WHERE [name] LIKE @#@%@# AND [id] = @TblID) = 0 )
BEGIN
FETCH NEXT FROM @Tbl INTO @TblName, @TblID
CONTINUE
END
-- 取出列名及其类型
DECLARE @Col CURSOR
SET @Col = CURSOR LOCAL SCROLL FOR
SELECT &Old&.dbo.syscolumns.[name], &Old&.dbo.systypes.[name]
FROM &Old&.dbo.syscolumns LEFT OUTER JOIN
&Old&.dbo.systypes ON &Old&.dbo.syscolumns.xtype = &Old&.dbo.systypes.xtype
WHERE &Old&.dbo.syscolumns.[name] LIKE @#@%@#
AND &Old&.dbo.syscolumns.[id] = @TblID
ORDER BY &Old&.dbo.syscolumns.colorder
DECLARE @ColName nvarchar(50)
DECLARE @ColType nvarchar(20)
-- 构造SQL语句
SET @SQL = @#CREATE TABLE &New&.dbo.@# + @TblName + @#(@#
OPEN @Col
FETCH NEXT FROM @Col INTO @ColName, @ColType
DECLARE @ColNameLast nvarchar(50)
SET @ColNameLast = @#@#
WHILE( @@FETCH_STATUS = 0 )
BEGIN
SET @ColName = SubString( @ColName, 2, Len( @ColName )-1 )
-- 跳过重复的列
IF( @ColName <> @ColNameLast )
BEGIN
SET @SQL = @SQL + @ColName + @# @# + @ColType + @#,@#
SET @ColNameLast = @ColName
END
FETCH NEXT FROM @Col INTO @ColName, @ColType
END
SET @SQL = SubString( @SQL, 1, Len( @SQL )-1 )
SET @SQL = @SQL + @#)@#
-- 执行SQL语句
EXEC SP_ExecuteSQL @SQL
IF( @@ERROR <> 0 )
BEGIN
RAISERROR( @#创建表%s失败!@#, 11, 1, @TblName )
RETURN
END
-- 创建下一个表
FETCH NEXT FROM @Tbl INTO @TblName, @TblID
END
RETURN @@ERROR
原文转自:http://www.ltesting.net