2013-07-03 115 views
1

我有一個SQL問題,並嘗試了多種組合無濟於事。我已經嘗試過逗號和分號,在前兩個之後),所有三個),所有這些都只是最後一個)。沒有這樣的運氣。請幫我修復查詢。提前致謝!sql修改文件語句

我已經做了盡職調查,並在多個網站來(我只能職位,因爲reputatoin的兩個鏈接),這些: Create database using script at the default path? http://www.sqlteam.com/forums/topic.asp?topic_id=148732

USE master 
GO 
ALTER DATABASE location_cust_db 
MODIFY FILE 
(
    NAME = location_cust_db_data1, 
    MAXSIZE = UNLIMITED, 
) 
(
    NAME = location_cust_db_data2, 
    MAXSIZE = UNLIMITED, 
) 
(
    NAME = location_cust_db_data3, 
    MAXSIZE = UNLIMITED, 
) 
GO 

USE master 
GO 
ALTER DATABASE location_cust_db 
MODIFY FILE (NAME = location_cust_db_data1, MAXSIZE = UNLIMITED) 
MODIFY FILE (NAME = location_cust_db_data2, MAXSIZE = UNLIMITED) 
MODIFY FILE (NAME = location_cust_db_data3, MAXSIZE = UNLIMITED) 
GO 

這工作,但做20個文件非常繁瑣。

USE master 
GO 
ALTER DATABASE location_cust_db 
MODIFY FILE 
(NAME = location_cust_db_data1, 
MAXSIZE = UNLIMITED) 


USE master 
GO 
ALTER DATABASE location_cust_db 
MODIFY FILE 
(NAME = location_cust_db_data2, 
MAXSIZE = UNLIMITED) 
GO 

USE master 
GO 
ALTER DATABASE location_cust_db 
MODIFY FILE 
(NAME = location_cust_db_data3, 
MAXSIZE = UNLIMITED) 
GO 

回答

1

你的第三個選項是最接近的 - 每次修改必須是在一個單獨的ALTER DATABASE命令,但你可以離開了USE語句,因爲你總是從MASTER運行:

USE master 
GO 

ALTER DATABASE location_cust_db 
MODIFY FILE (NAME = location_cust_db_data1, MAXSIZE = UNLIMITED) 
GO 

ALTER DATABASE location_cust_db 
MODIFY FILE (NAME = location_cust_db_data2, MAXSIZE = UNLIMITED) 
GO 

ALTER DATABASE location_cust_db 
MODIFY FILE (NAME = location_cust_db_data3, MAXSIZE = UNLIMITED) 
GO 

..etc 
0

如果我有問題吧,所以這一塊會幫助你

begin 
use master; 
declare @com nvarchar(1024); 
set @com='ALTER DATABASE location_cust_db MODIFY FILE (NAME = location_cust_db_data'; 
declare @i int; 
set @i=1; 
while(@i<20) begin 
     set @[email protected]+str(@i)+' ,MAXSIZE = UNLIMITED);' 
     exec (@com); 
     set @[email protected]+1; 
    end 
end 
+1

bewa exec()命令的執行將會很危險並且非常有用 – 2013-07-03 19:49:04

0
use [location_cust_db] 
GO 

declare csDBFiles cursor local fast_forward for 
select 
    'ALTER DATABASE [' + DB_NAME() + '] MODIFY FILE (NAME = [' + name + '], MAXSIZE = UNLIMITED)' 
from 
    sys.database_files 

open csDBFiles 

declare @stmt varchar(2000) 

fetch next from csDBFiles into @stmt 
while @@fetch_status = 0 
begin 
    print @stmt 
    print 'GO' 
    exec(@stmt) 

    fetch next from csDBFiles into @stmt 
end 

close csDBFiles 
deallocate csDBFiles 
GO 
+0

可能'其中type_desc ='ROWS'在遊標聲明處或其他任何條件(如有必要)。 –