Powershell
Simple method to query a SQL database
# Method 1 # Pros: Does not require installation of additional items other than SMO. # Cons: A bit more complex than other methods. # Recommendation: Use SMO ExecuteWithResults when you want to build # scripts/functions which are not dependent on additional installations. [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") $srvr = new-object ("Microsoft.SqlServer.Management.Smo.Server") "CYG7007" $db = $srvr.Databases["ICT"] $db.ExecuteWithResults("SELECT * FROM tbl_ServerUptimeData") # Method 2 # Pros: Simple syntax, supports a wide range of options including XQuery # Cons: Requires installation of SQL 2008 Powershell host (sqlps.exe) # Recommendation: Use Invoke-SqlCmd if you have SQL Server 2008 Powershell # host (sqlps.exe) installed. Invoke-Sqlcmd -Query "SELECT * FROM [ICT].[DBO].[tbl_ServerUptimeData]" -ServerInstance "SQLSERVER"
Last Updated (Wednesday, 20 May 2009 19:13)