Archive
SQL DBA – Change RECOVERY mode & SHRINK all databases at once in SQL Server
There are times when you are left with no or very less space in you SQL Server VM disks, and the main cause of this is not only the lot amount of data coming in but the heavy processing your database Server is doing, and thus filling up the entire log space.
SQL Server provides you some effective commands to shrink the database files which got inflated due to heavy processing and to make room for new data.
ALTER DATABASE <db_name> SET RECOVERY SIMPLE; DBCC SHRINKFILE (N'<log_file_name>' , 100);
But this works with one database at a time, so what if you have lots of databases? It will be tiring and time consuming to visit each DB, get the database file names and apply the shrink command. Its even slow to do the same via SSMS GUI.
Note: Do not change the recovery mode in a PROD environment unless it is really required.
With the below T-SQL statements you can generate scripts for all the databases and can run to change all database settings at once:
-- Generate SQL Script to change Recovery mode to 'SIMPLE' for all DBs: SELECT 'ALTER DATABASE [' + name + '] SET RECOVERY SIMPLE;' as SimpleRecovery4AllDBs FROM sys.databases WHERE recovery_model_desc <> 'SIMPLE' -- Generate SQL Script to Shrink log files of all DBs: SELECT 'USE ' + DB_Name(database_id) + '; DBCC SHRINKFILE (N''' + name + ''' , 100);' as ShrinkAllDBs FROM sys.master_files WHERE database_id > 4 and [type] = 1
Please let me know if you have any other way to do the same (and in more effective way) !!!
Migrate your SQL Server database to Azure by using SysTools “SQL to Azure Migration” Tool
Microsoft SQL Server is the most popular database management system, which provides flexibility to the database administrator to manage their database. It is a full-featured database and designed for use in corporate applications. But, many times, many users need to migrate their SQL Server database to the Azure SQL Database. Because Azure is an intelligent and fully managed relational cloud database that offers the broadest SQL Server engine compatibility.
However, at the time of SQL database to Azure migration, there are chances that we can miss some important steps or information such as trustworthy property, dependent Jobs, Linked Server, Logins etc. The main headache of any conversion is figuring out how to get the information from database and import into the server. Therefore, users need to use a professional tool to transfer SQL Server Database to Azure SQL Database.
One of the best third-party solutions I came up is SysTools SQL to Azure Migration Tool, which allows users to move all the SQL database objects like Tables, Triggers, Stored Procedures etc. to Azure SQL Database. Apart from this, there are many other features of this tool that are discussed below:
Key Features of SQL Server to Azure Migration Tool
1. Easily Transfer SQL Database to Azure
With the help of this tool, users can perform direct migration from SQL to Azure SQL database. For this, you need to provide all the credentials like server name, database name, username and password etc. After that, one can easily transfer SQL Server database to Azure.
2. Option to Add MDF/NDF Files
The tool provides an option to add MDF & associated NDF files to the software. You can also migrate all the database objects like Tables, Triggers, and Stored Procedures etc. from SQL to Azure.
3. Migrate Corrupted Database
The best part of this utility is that it can convert the corrupted database as well as the healthy file from local SQL server to Microsoft Azure SQL Database. Before migration, it scans the corrupted database files and repair them using either quick or advanced scan options.
4. Quick and Advance Scan Option
While working with this tool, the user can choose the required scan mode depending on the level of corruption out of the following modes:
– Quick Scan: This mode scans minor corrupted SQL files
– Advance Scan: This mode scan major corrupted SQL files
5. Auto Detect SQL Server File Versions
The SQL to Azure migration tool has an option to detect the versions of added MDF or NDF files automatically. For this, you just need to click on the checkbox next to this option.
6. Export Schema Option
The tool provides a feature to save the SQL database schema into Azure in any of the following ways:
– With the Only Schema: It migrates only schema of tables, views etc.
– With Schema & Date: It allows users to transfer both schema and data of all database objects.
Free and Licensed versions of SysTools SQL Server to Azure Migration Tool
The tool can be availed in the following two versions:
1. Demo Version: Users can freely download the trial version of software from the SysTools official website. It is available to understand the working of the software in a much better way.
2. Licensed Version: The licensed version of the tool can migrate SQL server database to Azure from MDF or NDF files. Also, it allows you to transfer the schema and schema & data.
System Requirements
The licensed version of SQL to Azure migration tool has been tested by the SQL experts. It will evaluate the performance of the software in terms of quality, reliability, security etc.
However, the testing has been performed in below-mentioned environment:
– Operating System: compatible with Windows 10 and all below versions
– Processor: Intel Pentium 1 GHz processor or any equivalent processor
– RAM: Around 2 GB of RAM is necessary
– Hard Disk: At least 100 MB space is required for installation
Advantages:
– It can also migrate corrupted or inaccessible SQL database to the Azure database.
– The software can preview all database components before migrating to Azure.
– The SQL Server to Azure migration tool has a simple and user-friendly interface.
Disadvantages:
– It is required to have database created on Azure SQL Server Database before migration.
– The Demo version only exports 25 records of each table.
Observational Verdict
After considering all the features of SQL Server to Azure Migration tool, I can say that it is a reliable and effective tool for DBAs and Developers to transfer SQL data to Azure without causing any data loss.
SQL Python Error – ‘sp_execute_external_script’ is disabled on this instance of SQL Server. Use sp_configure ‘external scripts enabled’ to enable it.
You are running a Python script by using sp_execute_external_script SP but its throwing this error:
Msg 39023, Level 16, State 1, Procedure sp_execute_external_script, Line 1 [Batch Start Line 27]
‘sp_execute_external_script’ is disabled on this instance of SQL Server. Use sp_configure ‘external scripts enabled’ to enable it.
You can refer to my blog post on setting up ML with Python with SQL Server, link: https://sqlwithmanoj.com/2018/08/10/get-started-with-python-on-sql-server-run-python-with-t-sql-on-ssms/
This fix will also work with R support with SQL Server.
Get started with Python on SQL Server – Run Python with T-SQL on SSMS
With SQL Server 2016 Microsoft added Machine Learning support with R Language in SQL engine itself and called it SQL Server R Services.
Going ahead with the new SQL Server 2017 version Microsoft added Python too as part of Machine Learning with existing R Language, and thus renamed it to SQL Server Machine Learning Services.
Installation/Setup
Here are few steps to get you started with Python programming in SQL Server, so that you can run Python scripts with T-SQL scripts within SSMS:
1. Feature Selection: While installing SQL Server 2017 make sure you’ve selected below highlighted services
2. Configure “external scripts enabled”: Post installation run below SQL statements to enable this option
sp_configure 'external scripts enabled' GO sp_configure 'external scripts enabled', 1; GO RECONFIGURE; GO sp_configure 'external scripts enabled' GO
3. Restart SQL Server service: by “services.msc” program from command prompt, and run below SQL statement, this should show run _value = 1
sp_configure 'external scripts enabled' GO
If still you don’t see run _value = 1, then try restarting the Launchpad service mentioned below in Step #4.
4. Launchpad Service: Make sure this service is running, in “services.msc” program from command prompt. Restart the service, and it should be in Running state.
Its a service to launch Advanced Analytics Extensions Launchpad process that enables integration with Microsoft R Open using standard T-SQL statements. Disabling this service will make Advanced Analytics features of SQL Server unavailable.
Post restarting this service, it should return run _value = 1 on running the query mentioned at Step #3
Run Python from SSMS
So as you’ve installed SQL Server with ML services with Python & R, and enabled the components, now you can try running simple “Hello World!” program to test it:
EXEC sp_execute_external_script @language = N'Python', @script = N'print(''Hello Python !!! from T-SQL'')'
Let’s do simple math here:
EXEC sp_execute_external_script @language = N'Python', @script = N' x = 5 y = 6 a = x * y b = x + y print(a) print(b)'
If you still face issues you can go through addition configuration steps mentioned in [MSDN Docs link].
SQL Job creation failing (having VBScript step) after SQL Server 2016/2017 upgrade
Yesterday my friend pinged me and told that he is facing some issues while executing a SQL Job DDL script. They had upgraded their SQL Server version from 2008 to 2016, and while creating SQL Jobs they were facing below error:
Msg 14234, Level 16, State 1, Procedure sp_verify_subsystem, Line 28 [Batch Start Line 2]
The specified ‘@subsystem’ is invalid (valid values are returned by sp_enum_sqlagent_subsystems).
I checked the code and on inquiring for a moment I saw that one of the SQL Job step was configured for a VB Script, as shown below:
... EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'xyz VBScript', @step_id=3, @cmdexec_success_code=0, @on_success_action=4, @on_success_step_id=0, @on_fail_action=5, @on_fail_step_id=0, @retry_attempts=0, @retry_interval=0, @os_run_priority=0, @subsystem=N'ActiveScripting', ...
On quickly checking on net I came to know that this feature has been discontinued and should not be used.
** Important *\* This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature.
MS BoL link
This MS BoL link also mentions about the discontinued feature:
ActiveX subsytem is discontinued. Use command line or PowerShell scripts instead.