A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations

Data assigning and Data retrival can not be used in a same statement.

/*Creating a table*/
If OBJECT_ID('Tb_Sample1') Is Null
Create Table Tb_Sample1
(
Id Int Identity(1,1),
Col1 Varchar(10),
Col2 DateTime
)
Go


/*Inserting one record*/
Insert Tb_Sample1(Col1, Col2) Values('SQL Server',CURRENT_TIMESTAMP)
Go


/*Using Data assigning and Data retrival in same statement*/
Declare @Column1 Varchar(10)
select Id, Col2,@Column1 = Col1 from Tb_Sample1
Select @Column1


Id, Col2 - is the retrival part
@Column1 = Col1 - is the assigning data to variable, These both operations can not be used in a single statement

It'll throw an error...
Msg 141, Level 15, State 1, Line 2
A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.

INSERT into an identity column not allowed on table variables.

Normally, Inserting data explicitly in IDENTITY column in Table variable is not allowed.

Declare @Table1 Table(ID Int Identity(1,1), Column1 Varchar(10))
Insert @Table1(Id, Column1) Values(1,'Sql Server')
Go


It'll throws an error
Msg 1077, Level 16, State 1, Line 2
INSERT into an identity column not allowed on table variables.

DEFAULT or NULL are not allowed as explicit identity values.

Normally, an IDENTITY column can not be a Nullable.

When we try to insert some explicit data into an IDENTITY column the following error occurred.

Create Table Tb_Table1
(
Id Int Identity(1,1),
Column1 int
)
Go

Set identity_insert Tb_Table1 on
Insert Tb_Table1 (Id,Column1) values(Null,1)
Set identity_insert Tb_Table1 off


It'll throw an error
Msg 339, Level 16, State 1, Line 2
DEFAULT or NULL are not allowed as explicit identity values.

UNION operator is not eliminating duplicate data. When ? and Why ?


Normally, UNION operator will elimate the duplicate data from the result sets.

But, In some situation, It'll not eliminate the duplicate data.

When ?
Incase, If we have used any Case-Sensitive collations on columns, database, server.

Why ?
The UNION operation depend on the collation what we have used in columns, Database, Server.

Scenario...
/*One table created, column with case-sensitive(CS) collation*/
If OBJECT_ID('Tb_Sample1') Is Null
Create Table Tb_Sample1
(
Id Int Identity(1,1),
Column1 Varchar(10) Collate SQL_Latin1_General_CP1_CS_AS,
Column2 Varchar(10)
)
Go


/*Inserting Duplicate data, But the case differs*/
Insert Tb_Sample1(Column1, Column2) Values('SQL','Server')
Insert Tb_Sample1(Column1, Column2) Values('MS','Access')
Insert Tb_Sample1(Column1, Column2) Values('Adobe','Reader')
Insert Tb_Sample1(Column1, Column2) Values('sql','Server')
Go


/*Fetching data using UNION operator*/
Select Column1 , Column2 From Tb_Sample1 where Column1 in('SQL', 'MS','sql')
Union
Select Column1 , Column2 From Tb_Sample1 where Column1 in('Adobe')


/*Expected result was...*/
Column1Column2
AdobeReader
MSAccess
sqlServer


/*But, The actual result is...*/
Column1Column2
AdobeReader
MSAccess
SQLServer
sqlServer

The UNION operator compares the data depend on Collation what we have used in Columns, Database, Server. So, It could not be considered as a duplicate and eliminate it.

How to fix it ?
- The collation has to be changed to Case-Insensitive as given below
Alter Table Tb_Sample1 Alter Column Column1 Varchar(10) Collate SQL_Latin1_General_CP1_CI_AS
Go


or

- Use COLLATE statement and force the case-insensitive collation when using the UNION operator as given below
Select Column1 Collate SQL_Latin1_General_CP1_CI_AS as Column1, Column2 From Tb_Sample1 where Column1 in('SQL', 'MS','sql')
UNION
Select Column1 , Column2 From Tb_Sample1 where Column1 in('Adobe')


Important:
- UNION operator automatically applies DISTINCT SORT operator internally.
- So, The duplicate data will be eliminated and SORTED from the final result set. But, UNION ALL will NOT work depend on Collation and not do any data Sorting.

This log backup contains bulk-logged changes. It cannot be used to stop at an arbitrary point in time.

Point-In-time restore is not supported If a log backup taken under the BULK-LOGGED recovery model contains bulk-logged changes. Trying to perform point-in-time recovery on a LOG backup that contains bulk changes will cause the following Error.

Msg 4341, Level 16, State 1, Line 1
This log backup contains bulk-logged changes. It cannot be used to stop at an arbitrary point in time.


I would like to walk-through the scenario here...

USE MASTER
GO


/*Creating new Database named : DB1*/
CREATE DATABASE DB1
GO

/*Setting Database recovery mode to BULK_LOGGED*/
ALTER DATABASE DB1 SET RECOVERY BULK_LOGGED
GO


/*Creating a table named : Table1*/
USE DB1
GO

CREATE TABLE table1
(
id INT IDENTITY(1,1),
column1 VARCHAR(10)

)
GO

/*Inserting a record into "Table1" */
INSERT table1 VALUES('Pandian')
GO


/*Taking Full BackUp*/
BACKUP DATABASE db1 TO DISK='e:\backup\Full.Bak' WITH INIT
GO


/*Performing some BULK operation*/
SELECT * INTO table2 FROM table1
GO

/*Inserting data into "table2" */
INSERT table2 VALUES('SQL DBA')
GO

/*Identifying current timestamp*/
SELECT GETDATE()
GO
-- 2011-02-27 19:55:28.760

/*Taking Log BackUp*/
USE MASTER
GO
BACKUP
LOG db1 TO DISK='e:\backup\Log.Trn' WITH INIT
GO


/*Droping the table*/
USE DB1
GO
DROP TABLE
table2
GO

/*Identifying the Bulk-Operation performed any, from the Log backup file*/
USE MASTER
GO
RESTORE HEADERONLY FROM DISK
='e:\backup\log.trn'
GO

/*Result from the above statement*/
We have a column called "HasBulkLoggedData", The column has value '1', means the Log backup contains Bulk operation

/*Performing DATA restore on DB1 database with NoRecovery Model for continue with Log restore*/
RESTORE DATABASE DB1 FROM DISK='e:\backup\Full.Bak' WITH NORECOVERY, REPLACE
GO

/*Performing LOG restore on DB1 database upto Partiular time */
RESTORE LOG DB1 FROM DISK='e:\backup\log.trn' WITH RECOVERY, STOPAT ='2011-02-27 19:55:28.760'
GO

Now, we should get an error as given below

Msg 4341, Level 16, State 1, Line 1
This log backup contains bulk-logged changes. It cannot be used to stop at an arbitrary point in time.


Now, the database seems to be middle of the Restore..., How to recover that ?

RESTORE DATABASE db1 WITH RECOVERY
GO


Note:But, we can restore LOG backup fully without any Point-in-time in this scenario!

How to update sequence number for existing records ?

CREATE TABLE #Table1
(
Id INT NULL,
Column1 VARCHAR(50)
)
GO

INSERT #Table1(Column1) VALUES('Failover Clustering')
INSERT #Table1(Column1) VALUES('Mirroring')
INSERT #Table1(Column1) VALUES('Replication')
INSERT #Table1(Column1) VALUES('Log Shipping')
INSERT #Table1(Column1) VALUES('Database Snapshot')
INSERT #Table1(Column1) VALUES('Database Backup')
GO

DECLARE @Seq INT
SELECT @Seq = 0
UPDATE #Table1 SET Id = @Seq , @Seq = @Seq + 1
GO


SELECT * FROM #Table1
GO

DROP TABLE #Table1
GO

Cannot change the state of non-data files or files in the primary filegroup.

When we try to set a particular Data file OFFLINE, The following Error occurred

Msg 5077, Level 16, State 2, Line 1
Cannot change the state of non-data files or files in the primary filegroup.


Why the Err ?
1. The file should be a Data file.
2. The file should be there in seperate Filegroup otherthan PRIMARY.
3. Log file can not be set OFFLINE as It can not be placed in seperate filegroup otherthan PRIMARY.

To set Data file OFFLINE:
ALTER DATABASE <Database Name> MODIFY FILE(NAME='<Data File Name>',OFFLINE)

To identify the file state(OFFLINE/ONLINE):
USE <Database Name>
GO
SELECT
Name [File Name], Type_Desc [Data File Type],State_Desc [State] FROM SYS.MASTER_FILES WHERE NAME LIKE DB_NAME() + '%'

How to make the Data file ONLINE:
- Restore the backup file is the way to set the data file ONLINE
- Restore all the backup files (Full, Differential, Log(If any))

USE master
GO
RESTORE DATABASE <Database Name>
FILE
= N'<Data File1>',
FILE = N'<Data File2>' FROM DISK = N'<Backup file Path.Bak>'
WITH FILE = 1,
NORECOVERY,
REPLACE
GO
--
--
--
RESTORE DATABASE <Database Name>
FILE
= N'<Data File1>',
FILE = N'<Data File2>' FROM DISK = N'<Backup file Path.Bak>'
WITH FILE = 1,
REPLACE
GO

- All the Data file(s) changed to ONLINE.

What are the Database Mail sent from SQL Server - Between two date


DECLARE
@StartDate DATETIME,
@EndDate DATETIME

SELECT @StartDate = '2011-02-01',
@EndDate = '2011-02-08 23:59:59'

SELECT
A.Name [Mail Account Name],
A.Display_Name [Mail Account Display Name],
P.Name [Mail Profile Name],
A.EMail_Address [From Mail ID],
SI.Recipients [To Mail ID],
SI.Copy_Recipients [CC Mail ID],
SI.Blind_Copy_Recipients [BCC Mail ID],
SI.[Subject] [Subject],
SI.Body [Body],
SI.Send_Request_Date [Request Date],
SI.Sent_Date [Sent Date],
SI.Sent_Status [Mail Status]
FROM MSDB.dbo.sysmail_sentitems SI JOIN MSDB.dbo.sysmail_profile p
ON (SI.Profile_Id = P.Profile_Id)
JOIN MSDB.dbo.sysmail_account A
ON (A.Account_ID =SI.Sent_Account_ID)
WHERE SI.Sent_Date BETWEEN @StartDate AND @EndDate
ORDER BY SI.sent_date DESC
GO

To Cleanup Backup and Restore history

To remove the Backup and Restore related history from the system tables older than the specified date:

USE msdb
GO
EXEC sp_delete_backuphistory '2011-01-31'
GO

To remove the Backup and Restore related history from the system tables for the specified Database:

USE msdb
GO
EXEC sp_delete_Database_backuphistory 'SQLDB1'
GO

Backup/Restore history removed from the following system tables
1.backupfile
2.backupfilegroup
3.backupmediafamily
4.backupmediaset
5.backupset
6.restorefile
7.restorefilegroup
8.restorehistory

Invalid backup mirror specification. All mirrors must have the same number of members.

When we try to take MIRRORED BACKUP the Err occurred in some situation.

Ok. Let us try to take MIRRORED BACKUP...

BACKUP DATABASE
SQLServerBuddy
TO DISK='C:\Backup\SQLBuddy_20110123.Bak',
DISK = 'E:\Backup\SQLBuddy_20110123.Bak'
MIRROR TO DISK = 'F:\Backup\SQLBuddy_20110123.Bak' WITH INIT, FORMAT
GO

Error Occurred:
Msg 3010, Level 16, State 1, Line 1
Invalid backup mirror specification. All mirrors must have the same number of members.


Reason:
-"TO DISK" and "MIRROR TO DISK" should have same number of Backup Set.
-But, "TO DISK" have two member(C:\ and E:\) and "MIRROR TO DISK" has only one member(F:\), So the media members not matched..

Solution:
BACKUP DATABASE SQLServerBuddy
TO DISK='C:\Backup\SQLBuddy_20110123.Bak',
DISK = 'E:\Backup\SQLBuddy_20110123.Bak'
MIRROR TO DISK = 'F:\Backup\SQLBuddy_20110123.Bak',
DISK = 'G:\Backup\SQLBuddy_20110123.Bak' WITH INIT, FORMAT
GO

Both the media members TO DISK(C:\ and E:\) and MIRROR TO DISK(F:\ and G:\) can work independently.

Here we have used both Backup Stripping(TO DISK - C:\ and E:\, F:\ and G:\) and Mirrored Backup. So, when we try to restore it, the media should be combined (C:\, E:\) and (F:\, G:\)

Let us try to restore it
RESTORE DATABASE SQLServerBuddy FROM DISK='C:\Backup\SQLBuddy_20110123.Bak', DISK='E:\Backup\SQLBuddy_20110123.Bak' WITH REPLACE
GO

(or)

RESTORE DATABASE SQLServerBuddy FROM DISK='F:\Backup\SQLBuddy_20110123.Bak', DISK='G:\Backup\SQLBuddy_20110123.Bak' WITH REPLACE
GO

Note: This option is available only in SQL Server 2005 Enterprise Edition and later versions