Tuesday, March 7, 2017

Simple Dynamic SQL to get system tables information

Simple Dynamic SQL to get all tables row count from the database.
You have many number of ways to get this information.

SELECT 'SELECT COUNT(*) AS '+ name +' FROM ' + name AS TableName
FROM sys.tables


You can also apply the condition to the sort the data...
WHERE name like 's%'

Thursday, January 12, 2017

Restore/Attach the database with out Log(ldf) file.

-- You can attach a database to the server just with mdf even though you dont have ldf.

USE [master] GO
EXEC sp_attach_single_file_db @dbname='AdventureWorksDW2012',
@physname=N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\AdventureWorksDW2012_Data.mdf'
GO

-- You can also use the following method..
USE [master] GO
CREATE DATABASE AdventureWorksDW2012_New ON
(FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\AdventureWorksDW2012_New_Data.mdf')
FOR ATTACH_REBUILD_LOG
GO

-- You can also use the following method.
USE [master] GO
CREATE DATABASE AdventureWorksDW2012_New1 ON
( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\AdventureWorksDW2012_New1_Data.mdf')
FOR ATTACH
GO

In all the above cases the ldf file created by itself in default log files folder.