Microsoft SQL Server – show total size allocated in MB per table

To get an overview of total disk space allocated per table in MB, use this SQL script:

WITH TableSizes AS
(
    SELECT 
        sch.name AS SchemaName,
        t.name AS TableName,
        SUM(p.rows) AS RowCounts,
        SUM(a.total_pages) * 8 AS TotalSpaceKB, 
        SUM(a.used_pages) * 8 AS UsedSpaceKB,
        (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB
    FROM 
        sys.tables t
    INNER JOIN 
        sys.indexes i ON t.object_id = i.object_id
    INNER JOIN 
        sys.partitions p ON i.object_id = p.object_id AND i.index_id = p.index_id
    INNER JOIN 
        sys.allocation_units a ON p.partition_id = a.container_id
    INNER JOIN 
        sys.schemas sch ON t.schema_id = sch.schema_id
    WHERE 
        t.type = 'U' -- Only include user tables
    GROUP BY 
        sch.name, t.name
)
SELECT 
    SchemaName,
    TableName,
    RowCounts,
    --TotalSpaceKB,
    --UsedSpaceKB,
    --UnusedSpaceKB,
    CAST(TotalSpaceKB / 1024.0 AS DECIMAL(10, 0)) AS TotalSpaceMB
    --CAST(UsedSpaceKB / 1024.0 AS DECIMAL(10, 2)) AS UsedSpaceMB,
    --CAST(UnusedSpaceKB / 1024.0 AS DECIMAL(10, 2)) AS UnusedSpaceMB
FROM 
    TableSizes
ORDER BY 
    TotalSpaceKB DESC;

 

Oracle SQL-Developer: Show timestamp in date columns

You can decide how SQL-Developer display date and timestamp columns.

  1. Go to the “Tools” menu and open “Preferences…”
  2. In the tree on the left open the “Database” branch and select “NLS”
  3. Now change the entries “Date Format”, “Timestamp Format” and “Timestamp TZ Format” as you wish!

ISO formated output with date and time

  • Date Format: YYYY-MM-DD HH24:MI:SS
  • Timestamp Format: YYYY-MM-DD HH24:MI:SSXFF
  • Timestamp TZ Format: YYYY-MM-DD HH24:MI:SSXFF TZR

Source: SQL-Developer: How to change the date format of the result grid – “Why does my date column have no time?” | Oracle Deli

Import a BACPAC File to Create a New Database

I used a SQL Express 2016 to import a bacpac file from Azure.
(Export the db from the Azure Portal first). And the SQL Server Management studio 17.

Initially imported into SQL Express 2014 but got some warnings/errors regarding the import, so SQL 2016 works better.

Steps:
1. Access to a bacpac file locally / on azure storage
2. Use the UI wizard in Management Studio. (See below)

To launch the wizard, use the following steps:
  1. Connect to the instance of SQL Server, whether on-premise or in SQL Database.
  2. In Object Explorer, right-click on Databases, and then select the Import Data-tier Application menu item to launch the wizard.
  3. Complete the wizard dialogs: Introduction Page. Import Settings Page.

    Source: Import a BACPAC File to Create a New User Database | Microsoft Docs

I got this error when starting up the website:
System.Data.SqlClient.SqlException (0x80131904): A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 – The certificate chain was issued by an authority that is not trusted.

Solved the error by setting TrustServerCertificate=false in the connectionstring.

More info here:
https://stackoverflow.com/questions/17615260/the-certificate-chain-was-issued-by-an-authority-that-is-not-trusted-when-conn

Import existing EPiServer users into an empty database – EPiServer 7.5

Here is a short guide for importing users from an existing EPiServer site into an empty EPiServer site. And then setup the default user groups and access rights. This is for EPiServer 7.5 using the standard Sql Membership but will probably work fine with newer EPiServer versions as well.

Create empty db

Run deployment center as administrator -> select create Sql db.

Importing users from old/other db:

How to copy table data, see this guide: https://www.sqlshack.com/how-to-copy-tables-from-one-database-to-another-in-sql-server/
The tables are named like: aspnet_Applications

Marked in bold are the tables that was used by my EPiServer 7.5 installation (the other tables where just empty). This is the order used, (see the link above), making sure the Users table is uploaded early on is important.

1. Application
2. Users
3.Membership
4.Paths
5.PersonalizationAllUsers
6.PersonalizationPerUser
7.Profile
8.Roles
9.Events
10.UsersInRoles (watch out for FK constraints to Users and Roles)
11.SchemaVersions (identical between same EPiServer db versions)

Create users and roles from scratch

Override the access protection to EPiServer admin until valid admin user and user groups are created:

Find location elements in web.config and comment out:

<location path="epi">

Comment out the <authorization> element entirely (removes the access protection). The same for location:

<location path="epi/CMS/admin">

Goto http://mysite.local/epi/CMS/Admin/Default.aspx or similar for epi admin area.

Create new roles in admin: (these are EPiServer defaults)
WebEditors
WebAdmins

Add your admin user  to WebEditors and WebAdmins group.

Goto admin “Set access rights”

Set correct access rights for editors and admin groups.
Check lower checkbox to make descendant content inherit the rights.
Also “Everyone” group should have Read access on root and downwards.

Set this in web.config:

<siteSettings pageRootId="1" pageStartId="1"

PagestartId should be root id.

Uncomment the authorization for /epi and /epi/cms/admin (enable the authorization again).

Login with you admin user
Create a start page or import an episerver export xml file.
Run  [mysite.local]/epi/CMS/Admin/IndexContent.aspx to update search index if imported content.

Goto site settings in admin and point out the page as start page. And/or set in web.config

<siteSettings pageRootId="1" pageStartId="4"

(usually becomes 4)

Goto site settings in admin and point out the all site hosts (dev, test, prod etc).
Add more users such as editors and adminstrators that need access to site.

How to fetch the row count for all tables in a SQL SERVER database

The following SQL will get you the row count of all tables in a database:

CREATE TABLE #counts
(
    table_name varchar(255),
    row_count int
)

EXEC sp_MSForEachTable @command1='INSERT #counts (table_name, row_count) SELECT ''?'', COUNT(*) FROM ?'
SELECT table_name, row_count FROM #counts ORDER BY table_name, row_count DESC
DROP TABLE #counts

The output will be a list of tables and their row counts.

If you just want the total row count across the whole database, appending:

SELECT SUM(row_count) AS total_row_count FROM #counts

will get you a single value for the total number of rows in the whole database.

Source: How to fetch the row count for all tables in a SQL SERVER database – Stack Overflow

How to migrate a SQL Server database to a lower version

…there are a few options that can help us to downgrade the database from a higher version of SQL Server to a lower version SQL Server. These options include:

  • Generate Scripts wizard of SQL Server Management Studio
  • SQL Server Integration Services
  • Custom scripting and BCP

In this tip we will use the Generate Scripts wizard of SQL Server Management Studio.

Source: How to migrate a SQL Server database to a lower version