Any performance hit by xp_cmdshell repeatedly enable and disable in a stored procedure? - sql

I am using some dos commands to do file operations & bcp command in my sql server stored procedure. So I am setting xp_cmdshell on and off many times. I am doing this multiple times because, if I just enable once at the start of SP and disables at the end of SP, many times I get the error saying xp_cmdshell not enabled.
SO I am enabling and disabling at each dos command in my SP. By doing this, is there any load on sql server or any performance issues?
Also I want a way to set this xp_cmdshell enabled always. Is there any way to do this?

Related

Is there a way to execute a SQL command on a ADO.NET connection like the SQLCMD -q option?

We have multiple applications writing to our MS SQL 2008 R2 database and there is a need to capture information about these applications during these writes. The database code (triggers, stored procedures etc) are under my control and can be modified but not the application code.
I am leaning towards using CONTEXT_INFO to store application specific information and retrieving it in the triggers. The 1M$ question is "How can I get the applications to set the CONTEXT_INFO if they are loath to change application code ?".
I DO have access to the connection strings that these applications use as these reside in config files.
I was not able to find any properties on the connection string that I can use to set CONTEXT_INFO (or execute any TSQL at initialization). Basically I was hoping to simulate the behaviour of SQLCMD -q option e.g SQLCMD -q "SET CONTEXT_INFO 0x1244".
Is there any way to simulate this behavior in ADO.NET via configuration ? Or a different way to do this ? Any pointers appreciated.
If there is no way this can be done, I can take the message back and tell "management" that application code has to be changed.

Calling procedure on linked server without distributed transaction

I have a SQL Server 2005 procedure that needs to be modified to call a procedure that's on a linked server. My local procedure is called by several other procedures, always within a transaction.
If I add the call to the linked server, then at run time I get a message saying that the OLE DB provider was unable to begin a distributed transaction.
That's fine with me: as far as I know, the remote procedure doesn't do any database changes. I don't need it to be inside the transaction and therefore don't need a distributed transaction.
But I suspect that I don't have that option, that there's no way to turn off the extension of the transaction to the linked server. Is this correct?
(I could get around the problem by refactoring these procedures so that the call to the linked server takes place in a parent procedure, outside all transactions. But logically it belongs within this child procedure, so I'd like to keep it there.)
Thanks, all.
It turns out that, if you are on SQL Server 2008 or above, you have a server option that controls this:
EXEC sp_serveroption 'servername', 'remote proc transaction promotion', 'false'
(See sp_serveroption (Transact-SQL)).
But I was on SQL Server 2005, which doesn't have that option. So I investigated further and discovered that MSDTC had network transactions disabled on the machine I was using, which was why the remote transaction was failing. I was able to get this enabled by a DBA.

After using SSMS's Generate Scripts sqlcmd won't run it

I tried backing up my database using SSMS Generate Scripts options. This produces a rather large file that SSMS won't run (memmory limit).
I tried then running this script using sqlcmd, but I get a syntax error.
I read that sqlcmd mode is different and I do not wish to manually remove the errors (there are potentially alot of them).
Is there a way to generate the script so it will obey the rules of sqlcmd or vice versa?
If you are doing a backup with data, why don't you just right-click database - select tasks and backup and backup to a file?
Other than that, it would be hard to tell you what the error is without knowing what error you are getting.

Creating multiple stored procedures from SQL executed by powershell issue

Ok, so I've got a bit of a SQL and Powershell problem. There are 2 SQL scripts, one to setup 4 different global stored procedures. Another to execute them and manipulate data before returning it to PS to be placed in a CSV file. The reason I'm not putting them into a single file is for readability. The procs are enclosing huge chunks of sql and I cannot create permanent procs in our production environment.
The problem I'm running into is the script runs fine in SQL Mgmt Studio but when ran by PS, I get several errors around the 'go's in the script.
I'm pretty sure this is a problem with the format that PS and the .NET classes expect when executing and returning data sets but...I'm at a loss.
I'm running SQL Server 2005 btw.
Any ideas or similar experiences?
What errors do you get? How are you executing each file? GO is a batch separator understood only by certain tools (e.g. Management Studio); PowerShell doesn't know what GO means. Have you tried executing the separate CREATE PROCEDURE scripts without issuing a GO command between them? If they are separate commands this shouldn't be an issue.
"GO" is a delimiter used by SQL Management Studio. It is not a valid SQL keyword. You can configure SQL Management Studio and change "GO" to "ENGAGE" if you wanted to.
Just remove "GO" from the scripts.

SQL 2000 single mode, related to database dbcc checkdb

I am trying to hack opening a MDF file in a new database. We lost the backup.
After reading numerous articles, I came to the point where the only thing that separates me from a successful solution is setting the db into a single user mode.
I issue "sp_dboption 'MyDbName',single,true"
SQL Server reports that "The command(s) completed successfully."
Then I run "DBCC CHECKDB ('MyDbName',REPAIR_ALLOW_DATA_LOSS)"
and it complains that "Repair statement not processed. Database needs to be in single user mode."
This is frustrating to no end. It's like an intentional convoluted torture by MSFT developers.
I am not a DB admin, I am a programmer that needs to make this database restored, at least the stored procedures that we lost.
Thanks for your help!
You could try getting into single user mode by using the -m switch on the command line, as opposed to your sp_dboption command:
http://msdn.microsoft.com/en-us/library/aa178018(SQL.80).aspx