Re: Executing a SQL script on server
- From: "Richard Mueller" <rlmueller-NOSPAM@xxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 5 Jan 2006 20:21:48 -0600
David De Bono wrote:
> Hi,
>
> How can I using ADO send a SQL script to the server to be executed there ?
>
> Example script:
>
> USE [e-Lector]
>
> GO
>
> EXEC sp_addrolemember N'db_datareader', N'eanew'
>
> GO
>
> USE [e-Lector]
>
> GO
>
> EXEC sp_addrolemember N'db_datawriter', N'eanew'
>
> GO
>
> ---------------------------------
>
> I have tride to send it via the Execute method, but it fails on "GO"
>
Hi,
An example VBScript program to execute a stored procedure:
=============================
Option Explicit
Dim adoCommand
Dim strConnect
Dim strServer
Dim strDatabase
' Specify the SQL Server and Instance (if any).
' If the default instance, just specify the server name.
strServer = "MyServer\MyInstance"
' Specify the database.
strDatabase = "e-Lector"
' Connection string for SQL database.
strConnect = "DRIVER=SQL Server;Trusted_Connection=Yes;" _
& "DATABASE=" & strDatabase & ";" _
& "SERVER=" & strServer
' Open connection with an ADO Command object.
Set adoCommand = CreateObject("ADODB.Command")
adoCommand.ActiveConnection = strConnect
' Execute a system stored procedure.
adoCommand.CommandText = "EXEC sp_addrolemember N'db_datareader', N'eanew'"
adoCommand.Execute
============================
The tricky part is usually the connection string. Also, all SQL statements
must be enclosed in quotes. The GO is not used. I generally run such scripts
at a command prompt with the cscript host. The script can be run from any
machine with connectivity to the SQL Server. Of course, the above assumes
Windows Authentication and that you have sufficient privileges. If you use
SQL Logins, the connection string needs credentials. I'd have to lookup how
to specify userID and password.
--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
.
- References:
- Executing a SQL script on server
- From: David De Bono
- Executing a SQL script on server
- Prev by Date: How to SELECT UNION data from two Excel files???
- Next by Date: Access 97 vs 2000
- Previous by thread: Re: Executing a SQL script on server
- Next by thread: how to run SQL Script
- Index(es):
Relevant Pages
|