SQL Queries: Capturing changes to DB, Analyzing the changes

Backing up default tables:

Select * Into USERS_Backup from USERS

(6 row(s) affected)

Duplicating Rows:

INSERT into [USERS] SELECT * from [USERS] where [USERNAME] = ‘DEMO’

Msg 2601, Level 14, State 1, Line 1

Cannot insert duplicate key row in object ‘dbo.USERS’ with unique index ‘USRRECID’. The duplicate key value is (PQ$&3-#5U1).

The statement has been terminated.

Modifying existing rows:

UPDATE [MYDB].[dbo].[USERS]
SET [USERNAME] = ‘DEMO’,
[NAME] = ‘DEMO’
WHERE USERNAME = ‘NEWDEMO’
GO

Creating new users:

SET ansi_warnings OFF
INSERT INTO [USERS] VALUES (‘TESTUSR1′,’TESTUSR1′,NULL,NULL,NULL,NULL,NULL,NULL,’0_’,’encryptedpasswordstringhere’,NULL,’TESTUSR1′,’2011-12-16 00:00:00.000′,’13:23′,NULL,”,’new-recid’)

Compare two tables having identical layout:

SQL Query:

SELECT * FROM CONTACT1 WHERE NOT EXISTS (SELECT * FROM CONTACT1_Backup WHERE CONTACT1_Backup.CONTACT = CONTACT1.CONTACT)

Results:

image

Leave a Reply

Your email address will not be published. Required fields are marked *