Sunday, March 25, 2012
Calling several sql script from a master sql script.
Currently I have several scripts that do the following.
1) Create tables
2) Create Stored proc
3) Setup Security
4) Load Default Data
I want to create an Install script that will call all the above scripts for me.
I will be running this script from SQL Query Analyzer. Also, note that all the above scripts will not be in the same directory as the install script.
Appreciate any help.If you don'need to run your scripts from SQL query analyzer you could run the ISQL command line utility, that accept as parameter also an input file (option -i )
Take a look to SQL Servel Books online.
Bye
Thursday, March 22, 2012
Calling of Store Procs on SQL Server from MS Access
This is my first cut-through in developing store procs on SQL server, i've
got them created and put in the master db on SQL server. on the Access front
end I have code that passes in an update statement which calls the store pro
c
on SQL server, i've checked the syntax and there are no errors. I've
checked the SQL statement being passed in to the store proc and there are no
errors too, using SQL query analyzer.
When I try to run the code, it does not update the records I want on SQL
server, I'm thinking there's something wrong with the connectionstring? Can
anybody give me some insight or alternative to get around this problem?
AlbertAlbert
I don't know that you want to put stored procedures in the master database.
You should build an application database.
If you are using ADPs in Access, does the interactive Test Connection button
work? If so it is not your connection string. Even if you are using
MDBs, you should be able to test your connection interactively to check the
string (say, if you are using an ODBC object).
Other than that we would need to see your code (including the DDL) to
understand what the problem might be.
Joe|||I still can't pin point the problem, but here is some more information to
provide you on my problem.
On the SQL server enterprise, i've created an store proc the following in
the master db:
CREATE PROCEDURE SP_SQLSTRING
@.sqlstring varchar(50)
AS
DECLARE @.InsertString NVARCHAR(500)
if exists(select * from sysobjects where name = @.sqlstring)
Set @.InsertString = @.sqlstring
execute sp_executesql @.InsertString
GO
Now in the front end access, the code i have supplies an query as an
parameter...
eg strSQL = "Update..."
then i make a call to the function that is suppose to call the store proc:
SP_SQLSTR (strSQL)
Within the function SP_SQLSTR it has the following partial code:
cn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User
ID=xxxxx;Initial Catalog=" xxxxxxxxx ";Data Source=xxxxxx"
cn.Open
Dim rstSQLStr As ADODB.Command
Set rstSQLStr = New ADODB.Command
rstSQLStr.ActiveConnection = cn
rstSQLStr.CommandText = "SP_SQLSTRING" 'name of the store proc on SQL
server
rstSQLStr.CommandType = adCmdStoredProc
SP = xSQLstr
DoCmd.SetWarnings False
'This is where I execute it, but i don't get any errors, and it does
nothing to the database.
rstSQLStr.Execute , Array(SP)
I don't know where i went wrong, and there are no errors from the system.
So i'm a bit
to where to approach? If you can provide some guidance
that would be very greatful..
Albert
"J. M. De Moor" wrote:
> Albert
> I don't know that you want to put stored procedures in the master database
.
> You should build an application database.
> If you are using ADPs in Access, does the interactive Test Connection butt
on
> work? If so it is not your connection string. Even if you are using
> MDBs, you should be able to test your connection interactively to check th
e
> string (say, if you are using an ODBC object).
> Other than that we would need to see your code (including the DDL) to
> understand what the problem might be.
> Joe
>
>|||I urge you to take a good look at the Books Online, but not before you get a
good book on the basics of SQL.
ML|||On Fri, 12 Aug 2005 08:19:13 -0700, "Albert Chan" <Albert
Chan@.discussions.microsoft.com> wrote:
>Hi everyone,
>This is my first cut-through in developing store procs on SQL server, i've
>got them created and put in the master db on SQL server. on the Access fron
t
>end I have code that passes in an update statement which calls the store pr
oc
>on SQL server, i've checked the syntax and there are no errors. I've
>checked the SQL statement being passed in to the store proc and there are n
o
>errors too, using SQL query analyzer.
>When I try to run the code, it does not update the records I want on SQL
>server, I'm thinking there's something wrong with the connectionstring? Ca
n
>anybody give me some insight or alternative to get around this problem?
>Albert
Hi Albert,
1. Create the procedure in the application database, not in the master
database.
2. Test the procedure first by calling it from Query Analyzer. That must
works without errors before you even attempt to call the procedure from
Access (or any other client).
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||On Fri, 12 Aug 2005 13:25:04 -0700, Albert Chan wrote:
>CREATE PROCEDURE SP_SQLSTRING
>@.sqlstring varchar(50)
>AS
>DECLARE @.InsertString NVARCHAR(500)
>if exists(select * from sysobjects where name = @.sqlstring)
>Set @.InsertString = @.sqlstring
> execute sp_executesql @.InsertString
>GO
Hi Albert,
First of all, I fail to see the point of this procedure. If the
procedure does nothing but dynamically executing the string passed to
it, why not simply execute the SQL from your front end? Why use a stored
procedure at all?
>Now in the front end access, the code i have supplies an query as an
>parameter...
>eg strSQL = "Update..."
That means that the first statement will test for the existance of a
system object in the master database with the name "Update...". I guess
(and hope!) that you have no tables, triggers, or procedures with that
name, so the next statement is not executed.
The last statement will be executed (the conditional execution is only
for the first statement after an IF, unless you use a BEGIN / END block)
but since @.InsertString is still NULL, it won't do anything.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)sql
Tuesday, March 20, 2012
Calling Executables from SQL Server and getting return value.
I have a requirement of calling a Executable from SQL Server.
I know we can use -
EXEC @.result = Master..xp_cmdshell @.<command_string
However I want to get the return value (int) from the executable.
xp_cmdshell always returns 0 or 1. How can the executable return code
say 99 be obtained in SQL Server?
Thanks & Regards,
Chandra MohanThis is a bit of a kludge, but one method is to execute your application
via a CMD file and ECHO the %ERRORLEVEL% so that it is returned in the
xp_cmdshell resultset for subsequent interrogation. For example
MyApp.cmd
@.MyApplication.EXE
@.ECHO APPLICATION RETURN CODE=%ERRORLEVEL%
SQL script:
SET NOCOUNT ON
DECLARE @.ReturnCode int
DECLARE @.ApplicationReturnCode int
CREATE TABLE #Messages(Message nvarchar(255))
INSERT INTO #Messages
EXEC @.ReturnCode = master..xp_cmdshell 'c:\MyApp\MyApp.cmd'
SELECT @.ApplicationReturnCode =
CAST(SUBSTRING(Message, 25, 10) AS int)
FROM #Messages
WHERE LEFT(Message, 24) = 'APPLICATION RETURN CODE='
SELECT
@.ReturnCode,
@.ApplicationReturnCode
DROP TABLE #Messages
GO
--
Hope this helps.
Dan Guzman
SQL Server MVP
--------
SQL FAQ links (courtesy Neil Pike):
http://www.ntfaq.com/Articles/Index...epartmentID=800
http://www.sqlserverfaq.com
http://www.mssqlserver.com/faq
--------
"Chandra Mohan" <bschandramohan@.yahoo.com> wrote in message
news:bb0ef6.0308250314.77157f14@.posting.google.com ...
> Hi All,
> I have a requirement of calling a Executable from SQL Server.
> I know we can use -
> EXEC @.result = Master..xp_cmdshell @.<command_string>
> However I want to get the return value (int) from the executable.
> xp_cmdshell always returns 0 or 1. How can the executable return code
> say 99 be obtained in SQL Server?
> Thanks & Regards,
> Chandra Mohan
Calling an application from SQL (xp_cmdshell)
I am trying to run an executable from MS SQL Server 2000. I tried using the commands:
EXEC master..xp_cmdshell '"\\comp1\c\test.exe"', NO_OUTPUT
and
EXEC master..xp_cmdshell 'Start Test "\\comp1\c\test.exe" , NO_OUTPUT
When I go to task manager on comp1 I see that the process is listed as running only under Processes, not under Application. In addition the application doesnt seem to execute properly and the SQL Query doesnt terminate. For testing processes I have just a small VB project that pops up a message box and then exits (it works fine by running it from windows). I never see the message box, nor does it exit the process.
If I try to manually cancel the SQL Query, it still will not stop until I kill the process for test.exe from the task manager.
Does anyone have any ideas on how I could get the query to terminate properly and how to get the executable to do its thing?
Sincerely,
Andrei Girenkovyeah...don't call gui apps from xp_cmdshell unless you want to hang your server...
Only use batch executables that don't require ANY prompting...|||Well I wasn't aware of that, however the executable I'm trying to launch actually doesnt have any sort of GUI...all it does is checks some output from another program and updates some DB tables.
I found that after launching the executable through double clicking it worked fine, but when launching it from SQL it didn't update the DB. I made the small project with the message box as an easier way of testing it rather than having to go to SQL every time and checking whether or not a table got updated.
~Andrei|||Can you be more specific as to what this executable is doing ?|||[i]I made the small project with the message box
~Andrei
You don't think a message box is a gui?|||I mean the actual executable I am trying to run has no GUI. The messagebox program is only for testing. I now scrapped the message box project because of the GUI and am trying to just run/test with the actual executable, however I am not seeing any results either.
My program does the following: I am trying to record the Volume Serial of the C drive of the machine the instance of the SQL server is running. I have an executable which reads the volume serial, scrambles it, records it into the DB, and then exits out. It takes as a command line argument the name of the instance of SQL server on that machine.
I am trying to write a stored procedure that I could call which would run this executable and pass it the instance name as an argument. The call in the stored procedure is the following:
EXEC master..xp_cmdshell '"C:\volumeserial.exe" -' + (select @.@.SERVERNAME), NO_OUTPUT
Right now the select @.@.SERVERNAME part is hardcoded to the server name because I was getting an syntax error at the + before the select statement, and the comma after it. It actually looks like this:
EXEC master..xp_cmdshell '"C:\volumeserial.exe" -InstanceName', NO_OUTPUT
When I call this stored procedure I can see in the task manager under processes that Volumeserial.exe is running, however the DB doesnt get updated, the process never terminates, and the stored procedure never gets past that line. Like I mentioned earlier, the program works fine if I call it from outside SQL, so I think it's a SQL issue.
~Andrei Girenkov|||One thing to double check would be the connection string to the database. If you are using a trusted connection, remember that the application will be running under the identity of the SQL Server Service account.
Another thing to check would be permissions on the executable. Again, be sure that the SQL Server Service account as execute permissions on the application executable.
Make sure that you test you application executable while logged in as the SQL Server service account.
Regards,
hmscott|||My SQL Server Service account is the Administrator. I can run the executable just fine when logged in as Administrator.
~Andrei|||how about making the executable just return the scrambled serial number without trying to connect to the server and do the update? you can do those things in your stored procedure.
create table #tmp ([output] nvarchar(4000) null)
insert #tmp EXEC master..xp_cmdshell 'C:\volumeserial.exe'
delete #tmp where [output] is null
update y set scrambled_serial = [output]
from your_table y, #tmp where y.drive_letter = 'C'|||Thank you everyone for all your help. I had a hunch that it was not connecting to SQL properly so instead of writing the value back to the DB I had the program write it out to a file along with the command line arguments. It turned out that the problem was caused because in SQL I was passing the InstanceName parameter to the program with a dash (-). The program wasn't expecting that.
The other problem of not being able to select @.@.ServerName was fixed by just declaring the whole string as a varchar() and passing that variable to xp_cmdshell instead of the concatinated string.
Thanks again everybody who helped
~Andreisql