advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Partners & Affiliates
advertisement
advertisement
Tip of the Day
Average Rating: 1.2/5 | Rate this item | 18 users have rated this item.
Expertise: Intermediate
Language: SQL
March 28, 2011
How to Read Environment Variables in SQL Server Using T-SQL

To read Environment variables in T-SQL, you can use the xp_cmdshell extended stored procedure in SQL Server.

The following example shows how to read the %windir% environment variable, which gives the Windows directory path on SQL Server using xp_cmdshell:

DECLARE @windir nvarchar(255)
CREATE TABLE #Tmp
(
EnvVar nvarchar(255)
)
INSERT INTO #Tmp exec xp_cmdshell 'echo %windir%'
SET @windir = (SELECT TOP 1 EnvVar from #Tmp)
SELECT @windir as 'Windows Directory'

NOTE:To run this command, you need to be a member of the sysadmin fixed server. If you want others to be able to execute this command, you will have to explicitly grant them permission to execute the xp_cmdshell stored procedure.

Find more information about this stored procedure at MSDN.

Deepak Choudhari
If you have a hot tip and we publish it, we'll pay you. However, due to accounting overhead we no longer pay $10 for a single tip submission. You must accumulate 10 acceptable tips to receive payment. Be sure to include a clear explanation of what the technique does and why it's useful. If it includes code, limit it to 20 lines if possible. Submit your tip here.
Please rate this item (5=best)
 1  2  3  4  5
advertisement
advertisement