PDA

View Full Version : Sửa dùm mình câu SQL này - Select Case



IT_girl
17-10-2011, 10:52
Bạn nào sửa dùm mình câu lệnh này, cám ơn bạn nhiều nha.

select
name as 'Data and Log Filename'
, filename as 'Path to Data and Log file'
, case

when convert(varchar(1),growth=0) like '0' then 'No Autogrowth Enabled'

when (maxsize=-1 and status=2) then 'Growth by ' + convert(varchar(3),growth) + ' MB until disk full'

when (maxsize=-1 and status=1048578) then 'Growth by ' + convert(varchar(3),growth) + ' % until disk full'

when maxsize <> -1 and status=2 then 'Growth by ' + convert(varchar(3),growth/128.000) + ' MB until' + convert(varchar(10),maxsize/128.000) +' MB'

when maxsize <> -1 and status=1048578 then 'Growth by ' + convert(varchar(3),growth) + ' % until' + convert(varchar(10),maxsize/128.000) +' MB'

when maxsize <> -1 and status=66 then 'Growth by ' + convert(varchar(3),growth/128.000) + ' MB until' + convert(varchar(10),maxsize/128.000) +' MB'

when maxsize <> -1 and status=1048642 then 'Growth by ' + convert(varchar(3),growth) + ' % until' + convert(varchar(10),maxsize/128.000) +' MB'

end as Growth

from sysfiles

hoangnnm
17-10-2011, 11:09
Dòng này :


when convert(varchar(1),growth=0) like '0' then 'No Autogrowth Enabled'


Phải là :


when convert(varchar(1),growth) like '0' then 'No Autogrowth Enabled'

IT_girl
17-10-2011, 12:14
Bạn ơi, mình cám ơn bạn đã trả lời dùm, mình vẫn chưa làm được, mình tách nhỏ hơn thì thấy lỗi như sau, nó không thể convert giá trị âm.

select
name as 'Data and Log Filename'
, filename as 'Path to Data and Log file'
, case
when (convert (varchar(20),[maxsize]) <> '-1') then 'Growth by ' + convert(varchar(3),growth/128.000) + ' MB until' + convert(varchar(10),maxsize/128.000) +' MB'
end as Growth

from sysfiles

Thì bị lỗi như vầy:

Msg 8115, Level 16, State 5, Line 1
Arithmetic overflow error converting numeric to data type varchar.

Còn nếu làm:

select
name as 'Data and Log Filename'
, filename as 'Path to Data and Log file'
, case
when (convert(varchar(1),[status]) like '66') then 'Growth by ' + convert(varchar(3),growth/128.000) + ' MB until' + convert(varchar(10),maxsize/128.000) +' MB'
end as Growth

from sysfiles

thì nó trả về NULL, trong khi nó thật sự có giá trị

Cám ơn bạn nhiều!!!!

hoangnnm
17-10-2011, 13:17
Mình hiểu là bạn muốn xem thông tin của data, log và độ growth , sao không dùng sp_helpdb cho tiện ?

IT_girl
17-10-2011, 22:25
Vì mình phải in ra kết quả này theo dạng report cho nhiều databases. Mình nghĩ mình sai gì đó đơn giản lắm mà mình sửa hoài không được, nếu bạn hoặc ai biết thì chỉ dùm mình.

hoangnnm
18-10-2011, 09:05
Thử xem :



if object_id('tempdb..#helpdb') is not null drop table #helpdb

create table #helpdb
(
name varchar(128),
fileid int,
filename varchar(256),
filegroup varchar(24),
size varchar(24),
maxsize varchar(24),
growth varchar(4) ,
usage varchar(128)
)


insert into #helpdb exec sp_helpfile

select * from #helpdb

IT_girl
19-10-2011, 00:23
Hoàng à, cám ơn bạn reply again. Mình làm được rồi, dùng cursor, nhưng khi chạy trong 'New Query' trong SQL server thì được kết quả tốt, nhưng khi đưa vào script input thì output nó báo lỗi, mình muốn post lên nhưng forum lại báo vì chưa gởi 10 tin nên không thể gởi link, mà sql script text của mình đâu có link, nó parse sao đó. Làm sao mình gởi lên được?