Opening a batch file using SQL Server procedure....
For this, I took the example of opening a notepad. And able to open it with the following code :
start "c:\windows\system32" notepad.exe
--- saved as : note.bat
I tried to open it with following procedure :
ALTER procedure [dbo].[executebat]
as
begin
EXEC master.dbo.xp_CMDShell 'D:\ducont\note.bat'
end
also I enabled the xp_cmdshell option with following code ::
-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO
But when I try to execute the procedure, it showing continuously
" Query Executing..."
and no output !!
Guide me if I went wrong anywhere .
Simple answer. When you run xp_cmdshell it is creating a shell to do any work you ask it to. Your bat file is running inside that shell, and will open notepad, again inside the shell. You just won't be able to see it. Since it's expecting input (and can't get any) you are stuck with the " Query Executing..."
There really isn't a good way to do what you are asking for other than possibly using CLR. Unfortunately I don't know enough about it to say for certain one way or another. There may be an easier way in SQL 2012 or 2014 but I haven't seen it yet.
Related
I'm desperately trying to activate FileStream only with queries. By following the MSDN procedure everything is ok, but I would like to enable it without using SQL Server Configuration Manager or SQL Server Management Studio.
On several website I read that this query was enough to activate this feature (this query is done in the MSDN procedure but after some configuration in SQL Server Configuration Manager ) :
EXEC sp_configure 'filestream_access_level', 2;
GO
RECONFIGURE
GO
The message L option de configuration filestream access level est passée de 2 à 2. Pour installer, exécutez l'instruction RECONFIGURE." is gave but if I check the service's proprieties Filestream is steal disabled, even if I execute RECONFIGURE again. If I try to open a connection to a base featuring Filestream columns i get a message confirming Filestream is not enabled.
I also tried this query :
USE master
GO
EXEC sp_configure 'show advanced options'
GO
EXEC sp_configure filestream_access_level, 3
GO
EXEC sp_filestream_configure
#enable_level = 3
, #share_name = N'FS';
GO
RECONFIGURE WITH OVERRIDE
GO
Without succes, althougth the sp_filestream_configure seems to not exist.
This website tell the issue could be the default language of my server, but after change the default language to English nothing was better.
https://stackoverflow.com/questions/1...tream-access-l
I hope someone will be able to help me. Thanks. Sorry for my bad english, I'm french ;)
You have to do both. One is enabling the feature in the configuration manager. The other is setting the access level.
Here's the MSDN guide to walk through:
https://msdn.microsoft.com/en-us/library/cc645923.aspx
You're only doing step 11, you need to do the first 10 steps as well.
I am working on Development Server Named as SQLDEV01 and the db name is University and the table name is cse.students. During my work on table cse.students I lost some rows so I need to get the all the exact data from Production server.production Server Name is SQLPROD01.
How can I query to get the production data without using SSIS?
Setup a linked server from the development to production servers before doing the following from SSMS. All code should be executed on the development server.
http://technet.microsoft.com/en-us/library/ms188279.aspx
-- On Development server [SQLDEV01]
TRUNCATE TABLE [University].[cse].[students];
GO
-- Use link server to move data
INSERT INTO
[University].[cse].[students]
SELECT
*
FROM
[SQLPROD01].[University].[cse].[students]
GO
This assumes there are no identity columns on the target.
If you do have identity columns, turn on/off allow inserts before/after executing the above insert.
-- Before Insert, execute this statement
SET IDENTITY_INSERT [University].[cse].[students] ON
GO
-- After Insert, execute this statement
SET IDENTITY_INSERT [University].[cse].[students] OFF
GO
You can also create an ad-hoc connection using the OPENROWSET command.
http://technet.microsoft.com/en-us/library/ms190312.aspx
INSERT INTO
[University].[cse].[students]
SELECT
PRD.*
FROM
OPENROWSET('SQLNCLI', 'Server=SQLPROD01;Trusted_Connection=yes;',
'SELECT * FROM [University].[cse].[students]') AS PRD;
I did not check the syntax for your environment, please check.
If add hoc queries are set off (0), have the DBA turn them on (1) temporarily. Execute the following on the production server.
http://msdn.microsoft.com/en-us/library/ms187569.aspx
-- Show all settings
sp_configure 'show advanced options', 1;
RECONFIGURE;
GO
-- What is the current setting?
sp_configure
GO
-- Allow add hoc queries
sp_configure 'Ad Hoc Distributed Queries', 1;
RECONFIGURE;
GO
If (s)he does not allow that, you are stuck with a physical linked server.
If you are using delegation - choice #3, you have to watch out for the double hop issue.
http://blogs.msdn.com/b/sql_protocols/archive/2006/08/10/694657.aspx
I would configure the linked server with a specific account on production that has rights to select the data. See choice #4.
There are a few ways to do this. One that is pretty much failsafe is the following:
In Microsoft SQL Management Studio expand the server node for the source server - Then expand the Databases node. Right click on the source database and select Tasks -> Generate Scripts. When the dialog pops up click next. Select the "Select specific database objects" radio button. Expand the Tables node as check the table you want to copy. Click the next button at the bottom. Click the advanced button. In the options that pop up for Script DROP and CREATE select Script DROP and CREATE on the right. For the option Type of data to script select schema and data on the right. Click OK. Now back on the main dialog you need to select "Save to Clipboard" or "Save to new query window". I usually select clipboard because I am usually going to a different server but select what works best for you. Click next. Click next again and the script will generate according to your selections. Now just run that script on the destination database.
please use this query generalized query by replacing your entries
select * into targetTable from [sourceserver].[sourcedatabase].[dbo].[sourceTable]
I've got a problem whereby I need to read data from a .txt file into a variable in SQL Server. The read needs to be performed programmatically, as it's going to form part of a stored procedure, and it needs not to utilise the BULK method, as I don't have permissions to use the BULK method on the database in question. Is this possible?
Thanks in advance :)
Can you get them to allow Ad Hoc Distributed Queries? Then you can use OpenRowset or OpenDatasource.
SELECT *
FROM OPENROWSET('MSDASQL',
'Driver={Microsoft Text Driver (*.txt; *.csv)};DefaultDir=c:\users\graham\desktop;',
'SELECT * FROM [data.txt];'
Here's the recofiguring code, if you need it:
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'Ad Hoc Distributed Queries', 1;
RECONFIGURE;
go
This is a laborious technique, though -- you sure you can't use client code? Even, I dunno, VBA in Excel or something?
g.
There is no other way to read the contents of a file without these permissions in SQL Server, or without setting up a link to the file. Otherwise, you have to do this through SSIS or using programming.
The solution is to get the permissions to solve your issue.
We're using Visual Studio Database Professional and it makes heavy use of SQLCMD variables to differentiate between environments while deploying.
I know there's several directives available for setting context (like :connect for server name). Is there a way within the script itself to force SQLCMD mode for execution? Part of our deployment process is to have DBA's examine and execute the scripts and it would be a nice safety net (so I don't have to remind them to set their execution mode to SQLCMD).
Not a solution, but as a work-around, you could embed your script in some warning. This post inspired me to this code:
SET NOEXEC OFF; -- previous execution may have toggled it
:setvar IsSqlCmdEnabled "True"
GO
IF ('$(IsSqlCmdEnabled)' = '$' + '(IsSqlCmdEnabled)')
BEGIN
PRINT('Use SqlCmd-mode!!');
SET NOEXEC ON;
-- RAISERROR ('This script must be run in SQLCMD mode.', 20, 1) WITH LOG
END
ELSE
BEGIN
PRINT('Using SqlCmd-mode')
-- insert the code you really want to execute:
-- ...
END
SET NOEXEC OFF; -- do not disable next execution in this session
This does not seem to be possible. I even checked the SSMS project mode.
However, if you create a database project in BIDS, the pre-deploy & post-deploy scripts run in SQLCMD mode by default.
I know that is not the answer you want, but it is the best I can give you w/o resorting creating a custom SSMS plugin that would do it for you based on some text in the script file.
How can i resolve a hostname in t-sql? a 2000 compatible method is preferred. Although something that works on 2005/2008 would also be helpful.
eg. If i have the hostname stackoverflow.com i want to return 69.59.196.211
Well, I suppose you could use xp_cmdshell to execute nslookup and parse the results. Seems like a really awkward thing for SQL Server to be doing, though.
exec master..xp_cmdshell 'nslookup intel.com'
.. then you'll probably want to stuff that in a temp table and walk through the results.
You could also, if you can get access to SQL Server 2005 or 2008, build a stored procedure or function in .NET and do a simple call to Dns.GetHostAddresses().
SQL Server may block access to procedure 'sys.xp_cmdshell' as part of the security configuration. As a system administrator you can enable the use of 'xp_cmdshell' as follows:
-- Allow advanced options.
EXEC sp_configure 'show advanced options', 1;
GO
-- Update the currently configured value for advanced options.
RECONFIGURE;
GO
-- Then, enable the feature.
EXEC sp_configure 'xp_cmdshell', 1;
GO
-- Update the currently configured value for this feature.
RECONFIGURE;
GO
-- Then you can go ahead and run ...
exec master..xp_cmdshell 'nslookup microsoft.com'
GO