Simple MSSQL WHERE Sample
04/13/07
Simple MSSQL WHERE Sample
Every so often, I need to put together a script to do some simple database queries or updates and I can never remember the proper format of the connection string and I have to go look it up somewhere. So this post is an attempt to solve that for myself. I figure I will always be able to find this sample, regardless of how I organize or reorgnize my local storage.
This is a simple MSSQL sample that performs a count based on a where clause.
'--------------------------------------------------------------------
'-- define the userid, password, server, and table that will be used
'-- for the query
'--------------------------------------------------------------------
dim dbUID, dbPswd, dbServer, dbTable
dbUID = "Your Database User ID"
dbPswd = "Your Database Password"
dbServer = "The Machine where the Database lives"
dbTable = "The Database Table that will be queried"
'--------------------------------------------------------------------
'-- create the connection object
'--------------------------------------------------------------------
dim objCXN
Set objCXN = createobject("ADODB.Connection")
'--------------------------------------------------------------------
'-- set up the connection string, we are using not using a dsn in
'-- this example
'--------------------------------------------------------------------
dim strCXN
strCXN = "Provider=SQLOLEDB.1;User ID='" & dbUID & "';Password='" & dbPswd & _
"';Data Source=" & dbServer & ";Initial Catalog=" & dbTable & ";"
'--------------------------------------------------------------------
'-- try to open the connection to the database
'--------------------------------------------------------------------
objCXN.Open strCXN
'--------------------------------------------------------------------
'-- try to open the connection to the database
'--------------------------------------------------------------------
dim RS
set RS = CreateObject("ADODB.RecordSet")
'--------------------------------------------------------------------
'-- build the query to issue against the database
'-- replace [TABLE_NAME], [SOMETHING], && [SOMEVALUE] with the appropriate
'-- value for your query
'-- for example:
'-- SELECT count(*) as count FROM dbo.MyTestTable WHERE LastName='smith'
'--------------------------------------------------------------------
dim strQuery
strQuery ="SELECT count(*) as count FROM [TABLE_NAME] WHERE [SOMETHING]='[SOME VALUE]'"
'--------------------------------------------------------------------
'-- execute the query
'--------------------------------------------------------------------
set RS = objCXN.execute(strQuery)
'--------------------------------------------------------------------
'-- just a simple echo of the count we performed during the SQL query
'--------------------------------------------------------------------
wscript.echo RS.Fields("count")
'--------------------------------------------------------------------
'-- destroy the recordset object
'--------------------------------------------------------------------
set RS = nothing
'--------------------------------------------------------------------
'-- close the connection object and destroy it
'--------------------------------------------------------------------
objCXN.Close
Set objCXN = Nothing
I hope this post helped you out. If it didn't, I am always looking for new scripts to add so submit a request for your question or need and I will see if I can answer it.
Pingbacks:
No Pingbacks for this post yet...
This post has 1 feedback awaiting moderation...
Scripts
This is somewhere I can post interesting snippets as I come across them. Hopefully some folks out there will find this helpful.
Search
Follow Me:
Categories
- All
- Web Technologies (2)
- PHP (1)
- Windows Scripting (37)
- Batch Scripts (13)
- Wsh Scripts (23)
Archives
- December 2009 (2)
- March 2009 (2)
- February 2009 (3)
- January 2009 (2)
- December 2008 (4)
- November 2008 (1)
- October 2008 (1)
- February 2008 (1)
- December 2007 (3)
- July 2007 (1)
- April 2007 (1)
- February 2007 (2)
- More...
Misc
Who's Online?
- Guest Users: 1




