Solved

ExecuteScalar Assistance

Posted on 2014-02-03
2
282 Views
Last Modified: 2014-02-04
When executing the below command, the value (Machine Name) isn't being returned and passed into the next command (strQuery2).

Please assist


                strQuery = "SELECT SERVERPROPERTY ('MachineName') "
                'strQuery2 = "ALTER DATABASE " & database & "  & MachineName & "  "
 



                Dim cmdr As SqlCommand
                Dim cmd As SqlCommand


                cmdr = New SqlCommand(strQuery, con)
                MachineName = cmdr.ExecuteScalar()

                'cmd = New SqlCommand(strQuery2, con)
                'cmd.ExecuteNonQuery()
0
Comment
Question by:LeVette
2 Comments
 
LVL 22

Accepted Solution

by:
Snarf0001 earned 250 total points
ID: 39830385
You're assigning "MachineName" to the string before you've filled it with the query.
Move it underneath the first execute:

strQuery = "SELECT SERVERPROPERTY ('MachineName') "

Dim cmdr As SqlCommand
Dim cmd As SqlCommand


cmdr = New SqlCommand(strQuery, con)
MachineName = cmdr.ExecuteScalar()

'strQuery2 = "ALTER DATABASE " & database & "  & MachineName & "  "
'cmd = New SqlCommand(strQuery2, con)
'cmd.ExecuteNonQuery()
0
 
LVL 16

Assisted Solution

by:Surendra Nath
Surendra Nath earned 250 total points
ID: 39830391
you have to write it as below

            strQuery = "SELECT SERVERPROPERTY ('MachineName') "
 



                Dim cmdr As SqlCommand
                Dim cmd As SqlCommand


                cmdr = New SqlCommand(strQuery, con)
                MachineName = cmdr.ExecuteScalar()

                strQuery2 = "ALTER DATABASE " & database & "  & MachineName & "  "

                cmd = New SqlCommand(strQuery2, con)
                cmd.ExecuteNonQuery()

Open in new window


The reason why your previous code didn't work is, by the time the strQuery2 = assignment stuff executed the MachineName variable didn't have any value.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question