淡泊明志 宁静致远
宠辱不惊 闲看庭前花开花落 去留无意 漫随天外云卷云舒

if not exists (select name from master.dbo.sysdatabases where (name ='AAA_SYS'))
begin
   
    if  exists (select name from master.dbo.sysdatabases where (name ='temp____AAA____Test'))
     DROP DATABASE [temp____AAA____Test]
    CREATE DATABASE [temp____AAA____Test]
 
    declare @fileName nvarchar(255)
    select   @fileName=[filename] from   master.dbo.sysdatabases   where   [name]   =   'temp____AAA____Test'
    print @fileName

    set @FileName = replace(@FileName,'temp____AAA____Test.mdf','AAA_SYS.mdf')

    if   exists   (select   *   from   tempdb.dbo.sysobjects   where   id   =   object_id(N'tempdb.dbo.#tmp')   and   type='U')  
        DROP TABLE #tmp

    CREATE TABLE #tmp
    (
        a bit,/*文件是否存在*/
        b bit,/*文件0,文件夹1*/
        c bit /*父目录*/
    )
    INSERT INTO #tmp
    EXEC master..xp_fileexist @filename

    select * from #tmp

 declare @fileName1 nvarchar(255)
 declare @fileName2 nvarchar(255)

 set @fileName1= @fileName
 set @fileName2= replace(@fileName,'AAA_SYS.mdf','AAA_SYS_log.ldf')
  if Exists(SELECT a FROM #tmp where a<>0)
  begin
   EXEC sp_attach_db N'AAA_SYS',
         @filename1 ,
         @filename2
  end
  else begin
   CREATE DATABASE [AAA_SYS] COLLATE Chinese_PRC_CI_AS
   ALTER DATABASE [AAA_SYS] MODIFY FILE (NAME=N'AAA_SYS_log', MAXSIZE=UNLIMITED)
  end
     if  exists (select name from master.dbo.sysdatabases where (name ='temp____AAA____Test'))
      DROP DATABASE [temp____AAA____Test]

end

 

posted on 2008-10-07 10:06  RainWaterLily  阅读(987)  评论(0编辑  收藏  举报