求教:sql server 2005 中取得系统盘符的方法

技术小牛 2008-09-03 02:56:36
如题:就是在sql server 2005 中用脚本或自带存储过程能否 检测出当前安装sql server的系统的系统盘符是什么?
...全文
166 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
fcuandy 2008-09-03
  • 打赏
  • 举报
回复
如果你是想执行,而不是在sql中得到的话,确实直接用系统变量名代替就可以了,不必如此麻烦。

系统变量名,可以打开我的电脑属性,高级,那里查看变量。
技术小牛 2008-09-03
  • 打赏
  • 举报
回复
长见识了。
问题得到圆满解决,感谢fcuandy,anovice以及热心回复的朋友们。
最后采用答案:
exec master.dbo.xp_cmdshell '%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql.exe -S (local) -U sa -P 123 -d test8 -ed'

fcuandy 2008-09-03
  • 打赏
  • 举报
回复
exec xp_cmdshell要求后面为常量,如果用变量套的话,那么exec嵌套就可以了。
即,在第一个exec执行时,内部语句已是常量



declare @t table(path varchar(1000))
insert @t exec master..xp_cmdshell 'echo %windir%'
declare @path varchar(1000)
select top 1 @path=path from @t where path is not null
select @path
exec ('exec master.dbo.xp_cmdshell ''' + @path + '\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql.exe -S (local) -U sa -P 123 -d test8 -ed''')
anovice 2008-09-03
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 fcuandy 的回复:]
如果是获取操作系统安装路径或盘符的话,那么读取系统环境变量就可以了。

windir 就是变量名。



SQL codeexec master..xp_cmdshell 'echo path %windir%'
[/Quote]
anovice 2008-09-03
  • 打赏
  • 举报
回复
好像直接用%windir%替换就行了
技术小牛 2008-09-03
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 fcuandy 的回复:]
如果是获取操作系统安装路径或盘符的话,那么读取系统环境变量就可以了。

windir 就是变量名。


SQL codeexecmaster..xp_cmdshell'echo path %windir%'
[/Quote]
欲将红色部分用windir变量代替,该如何写sql句,谢谢。
exec master.dbo.xp_cmdshell 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql.exe -S (local) -U sa -P 123 -d test8 -ed'
fcuandy 2008-09-03
  • 打赏
  • 举报
回复
如果要将结果存在变量中,那么这样就可以了。

declare @t table(path varchar(1000))
insert @t exec master..xp_cmdshell 'echo %windir%'
declare @path varchar(1000)
select top 1 @path=path from @t where path is not null
select @path
/*
C:\WINDOWS
*/
fcuandy 2008-09-03
  • 打赏
  • 举报
回复
如果是获取操作系统安装路径或盘符的话,那么读取系统环境变量就可以了。

windir 就是变量名。


exec master..xp_cmdshell 'echo path %windir%'
技术小牛 2008-09-03
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 confei 的回复:]
EXEC master..xp_fixeddrives
[/Quote]

运行结果:
C 7950
D 3864
E 1567
F 496
confei,还是不知那个是系统盘。
confei 2008-09-03
  • 打赏
  • 举报
回复
EXEC master..xp_fixeddrives
技术小牛 2008-09-03
  • 打赏
  • 举报
回复
可能是我误导了fcuandy,在解释一下,我要取得是操作系统的系统盘盘符。
技术小牛 2008-09-03
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 fcuandy 的回复:]
2005下可以试试

select left(filename,charindex(':',filename)) from sys.sysfiles
[/Quote]

fcuandy的结果也有局限(仅仅是得到数据库的安装盘符),如果数据库没建在系统盘下,结果就错了。
技术小牛 2008-09-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wufeng4552 的回复:]
exec  master..xp_availablemedia  2
exec  xp_availablemedia  2
[/Quote]
wufeng4552 结果:
C:\ -253972480 1 2
D:\ -242536448 0 2
E:\ 1643225088 0 2
F:\ 520936448 0 2
wufeng4552 的运行结果是系统的可用盘,不符要求,我要的是判断系统盘(比如我当前是C盘)
fcuandy 2008-09-03
  • 打赏
  • 举报
回复
2005下可以试试

select left(filename,charindex(':',filename)) from sys.sysfiles
fcuandy 2008-09-03
  • 打赏
  • 举报
回复
引用邹老大的贴

--得到SQL中的各种目录   
declare @path varchar(8000)

--得到当前数据库的数据文件路径
select @path=rtrim(reverse(filename)) from sysfiles
select @path=reverse(substring(@path,charindex('\',@path),8000))
select 数据文件目录=@path

--得到SQL安装时设置的数据文件路径
select @path=rtrim(reverse(filename)) from master..sysfiles where name='master'
select @path=reverse(substring(@path,charindex('\',@path),8000))
select SQL数据文件目录=@path

--根据安装路径得到默认的备份目录:
select @path=rtrim(reverse(filename)) from master..sysfiles where name='master'
select @path=substring(@path,charindex('\',@path)+1,8000),
@path=reverse(substring(@path,charindex('\',@path),8000))+'BACKUP\'
select SQL默认备份目录=@path
水族杰纶 2008-09-03
  • 打赏
  • 举报
回复
exec master..xp_availablemedia 2
exec xp_availablemedia 2

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧