SQL – Pro9ramming https://pro9ramming.com Software craftsman's blog Wed, 15 Apr 2020 17:51:05 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.3 Import SQL Server 2016 database to SQL Server 2014 https://pro9ramming.com/import-sql-server-2016-database-to-sql-server-2014/ Tue, 20 Dec 2016 07:50:17 +0000 http://pro9ramming.com/blog/?p=382 Continue reading Import SQL Server 2016 database to SQL Server 2014]]> If database is exported to .bak file (Right click on db in SSMS>Tasks>Back Up…), importing it in any previous/new version of SQL Server might not work, because of compatibility issues (for example, importing SQL Server 2016 database to SQL Server 2014). That’s why plain old SQL (or T-SQL) that can be used to overcome these backward compatibility issues.

SQL Scripts can be generated by Right click on db>Tasks>Generate Scripts…

Select an option to “Script entire database and all database options”, after that, “Save scripts to specific location”. Select file destination. In “Advanced” don’t forget to enable scripting Triggers or Full-Text Indexes, if you have any. The most important thing is to set “Types of data to script” to “Schema and data”.

After .sql file is created, data can be imported in SQL Server 2014 by using SQL Server Management Studio. But SSMS has a limitation. It doesn’t work with large files because of Memory Overflow. The solution is to use sqlcmd utility to import large files. The pattern is:

sqlcmd -S serverName\instanceName -i C:\scriptName.sql

-S option is server specification. If server is localhost, “.” can be used instead of serverName\instanceName. -i option is script name and location and -E option is for trusted connection.

For example, connecting to local SQL Server instance would look like:

sqlcmd -S . -E -i C:\scriptName.sql

]]>
Detach, Take offline, set read-only SQL Server database https://pro9ramming.com/detach-take-offline-set-read-only-sql-server-database/ Fri, 05 Aug 2016 14:23:46 +0000 http://pro9ramming.com/blog/?p=404 Continue reading Detach, Take offline, set read-only SQL Server database]]> SQL Server has different operations that can be done on a database. These operations may seem similar. Differences between them are presented below.

Detaching a database removes it from the instance of SQL Server but leaves the database intact within its data files and transaction log files. These files can then be used to attach the database to any instance of SQL Server, including the server from which the database was detached. Detach can be done in SSMS (Right click on db > Tasks > Detach).

Taking a database offline would not detach it from SQL Server, but database wouldn’t be usable anymore. It would be marked as offline in SSMS. Bringing the db offline can be done in SSMS (Right click on db > Tasks > Take Offline).

A readonly database doesn’t allow any writes (that includes insert/update commands). But queries would work fine. Database can be set as readonly using the following T-SQL command:

USE [master]
GO
ALTER DATABASE [DbName] SET READ_ONLY WITH NO_WAIT
GO

And setting the db back to read/write mode:

USE [master]
GO
ALTER DATABASE [DbName] SET READ_WRITE WITH NO_WAIT
GO

 

References

https://msdn.microsoft.com/en-us/library/ms190794.aspx

http://www.blackwasp.co.uk/sqloffline.aspx

http://www.blackwasp.co.uk/SQLReadOnly.aspx

]]>
Drop all objects from SQL Server database https://pro9ramming.com/drop-all-objects-from-sql-server-database/ Sun, 25 Oct 2015 14:58:55 +0000 http://pro9ramming.com/blog/?p=414 This scripts removes/drops all stored procedures, views, primary keys, foreign keys, tables and functions from SQL Server database. Just execute this in SSMS:

/* Drop all non-system stored procs */
DECLARE @name VARCHAR(128)
DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'P' AND category = 0 ORDER BY [name])

WHILE @name is not null
BEGIN
    SELECT @SQL = 'DROP PROCEDURE [dbo].[' + RTRIM(@name) +']'
    EXEC (@SQL)
    PRINT 'Dropped Procedure: ' + @name
    SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'P' AND category = 0 AND [name] > @name ORDER BY [name])
END
GO

/* Drop all views */
DECLARE @name VARCHAR(128)
DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'V' AND category = 0 ORDER BY [name])

WHILE @name IS NOT NULL
BEGIN
    SELECT @SQL = 'DROP VIEW [dbo].[' + RTRIM(@name) +']'
    EXEC (@SQL)
    PRINT 'Dropped View: ' + @name
    SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'V' AND category = 0 AND [name] > @name ORDER BY [name])
END
GO

/* Drop all functions */
DECLARE @name VARCHAR(128)
DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] IN (N'FN', N'IF', N'TF', N'FS', N'FT') AND category = 0 ORDER BY [name])

WHILE @name IS NOT NULL
BEGIN
    SELECT @SQL = 'DROP FUNCTION [dbo].[' + RTRIM(@name) +']'
    EXEC (@SQL)
    PRINT 'Dropped Function: ' + @name
    SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] IN (N'FN', N'IF', N'TF', N'FS', N'FT') AND category = 0 AND [name] > @name ORDER BY [name])
END
GO

/* Drop all Foreign Key constraints */
DECLARE @name VARCHAR(128)
DECLARE @constraint VARCHAR(254)
DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' ORDER BY TABLE_NAME)

WHILE @name is not null
BEGIN
    SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)
    WHILE @constraint IS NOT NULL
    BEGIN
        SELECT @SQL = 'ALTER TABLE [dbo].[' + RTRIM(@name) +'] DROP CONSTRAINT [' + RTRIM(@constraint) +']'
        EXEC (@SQL)
        PRINT 'Dropped FK Constraint: ' + @constraint + ' on ' + @name
        SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' AND CONSTRAINT_NAME <> @constraint AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)
    END
SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' ORDER BY TABLE_NAME)
END
GO

/* Drop all Primary Key constraints */
DECLARE @name VARCHAR(128)
DECLARE @constraint VARCHAR(254)
DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' ORDER BY TABLE_NAME)

WHILE @name IS NOT NULL
BEGIN
    SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)
    WHILE @constraint is not null
    BEGIN
        SELECT @SQL = 'ALTER TABLE [dbo].[' + RTRIM(@name) +'] DROP CONSTRAINT [' + RTRIM(@constraint)+']'
        EXEC (@SQL)
        PRINT 'Dropped PK Constraint: ' + @constraint + ' on ' + @name
        SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' AND CONSTRAINT_NAME <> @constraint AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)
    END
SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' ORDER BY TABLE_NAME)
END
GO

/* Drop all tables */
DECLARE @name VARCHAR(128)
DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'U' AND category = 0 ORDER BY [name])

WHILE @name IS NOT NULL
BEGIN
    SELECT @SQL = 'DROP TABLE [dbo].[' + RTRIM(@name) +']'
    EXEC (@SQL)
    PRINT 'Dropped Table: ' + @name
    SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'U' AND category = 0 AND [name] > @name ORDER BY [name])
END
GO

 

]]>
Drop database connections in SQLServer https://pro9ramming.com/drop-database-connections-in-sqlserver/ Tue, 23 Jun 2015 14:21:54 +0000 http://pro9ramming.com/blog/?p=387 Sometimes executing ALTER commands in T-SQL is not possible because other users are using the database. This should be used with caution.

USE master
GO

SET NOCOUNT ON
DECLARE @DBName varchar(50)
DECLARE @spidstr varchar(8000)
DECLARE @ConnKilled smallint
SET @ConnKilled=0
SET @spidstr = ''

Set @DBName = 'DB_NAME'
IF db_id(@DBName) < 4
BEGIN
PRINT 'Connections to system databases cannot be killed'
RETURN
END
SELECT @spidstr=coalesce(@spidstr,',' )+'kill '+convert(varchar, spid)+ '; '
FROM master..sysprocesses WHERE dbid=db_id(@DBName)

IF LEN(@spidstr) > 0
BEGIN
EXEC(@spidstr)
SELECT @ConnKilled = COUNT(1)
FROM master..sysprocesses WHERE dbid=db_id(@DBName)
END

 

]]>