reset the identity coulmn


if you want to reset the identity coulmn in any table use The following line resets the Identity value for the table to 0 so that the next record added starts at 1
DBCC CHECKIDENT(‘Table Name’, RESEED, 0)
So i make Curssor to Select all tables with Identity column in any database then make reset to this tables

DECLARE @Table_Name AS NVARCHAR(100)
DECLARE @NewLineChar AS CHAR(2)
SET @NewLineChar = CHAR(13) + CHAR(10)
DECLARE Reset_Identity CURSOR
for
select o.name
from syscolumns c, sysobjects o
where c.status = 128  and c.xtype = 56 and c.colid =1 and c.offset = 2
and o.id = c.id
order by o.name
OPEN Reset_Identity

FETCH NEXT FROM Reset_Identity INTO @Table_Name
WHILE (@@fetch_status <> -1)
BEGIN
IF (@@fetch_status <> -2)
BEGIN
DECLARE @message varchar(100)
SELECT @message =  ‘DBCC CHECKIDENT(”’ + @Table_Name + ”’, RESEED, 0) ‘  + @NewLineChar +’GO’
PRINT @message
END
FETCH NEXT FROM Reset_Identity INTO @Table_Name
END

CLOSE Reset_Identity
DEALLOCATE Reset_Identity
GO

After you run this code on any database take the result becouse this code will genrate the reset identity column code in the database so if you take the result an run it in any database in this case you make reset  to the identity column

Shrink Database Log File


http://platform.twitter.com/widgets/hub.1326407570.html

Shrink Database Log File

Only use this script for SQL Server development servers!
Script must be executed as sysadmin
This script will execute the following actions on all databases

– set recovery model to [Simple]
– trucate log file
– shrink log file
– Set recovery model to Simple

use [master]
go

— Declare container variabels for each column we select in the cursor
declare @databaseName nvarchar(128)

— Define the cursor name
declare databaseCursor cursor
— Define the dataset to loop
for
select [name] from sys.databases where database_id>4

— Start loop
open databaseCursor

— Get information from the first row
fetch next from databaseCursor into @databaseName

— Loop until there are no more rows
while @@fetch_status = 0
begin
print ‘Setting recovery model to Simple for database [‘ + @databaseName + ‘]’
exec(‘alter database [‘ + @databaseName + ‘] set recovery Simple’)

checkpoint

Print ‘checkpoint’

print ‘Shrinking logfile for database [‘ + @databaseName + ‘]’
exec(‘
use [‘ + @databaseName + ‘];’ +’

declare @logfileName nvarchar(128);
set @logfileName = (
select top 1 [name] from sys.database_files where [type] = 1
);
dbcc shrinkfile(@logfileName,1);
‘)
print ‘Setting recovery model to FULL for database [‘ + @databaseName + ‘]’
exec(‘alter database [‘ + @databaseName + ‘] set recovery Full’)
— Get information from next row
fetch next from databaseCursor into @databaseName
end

— End loop and clean up
close databaseCursor
deallocate databaseCursor
go

Create view By cursor


Create view By cursor

now my manager ask me to make views in database ((A)) from all tables already exist  in database ((B)) that’s hard task to catch tha tabel one by one and make create view XXXX as select * from Table_name . this operation very hard and will take a long time . so my friend MR/Mohamed Osman make Script dynamic to make this Operation .

Run this script on the database (B) where there are tables and the database you want to make the views select from there put the name of this DB in prameter name @DBNAME

— =============================================
— Create View dynamic
— Created by Mohamed osman
— =============================================
DECLARE @DBNAME AS NVARCHAR(100)
DECLARE @NewLineChar AS CHAR(2)
SET @NewLineChar = CHAR(13) + CHAR(10)
SET @DBNAME =’GEN2010.DBO.’ —— views will select from this database
DECLARE @RESULT AS NVARCHAR (MAX)

DECLARE V CURSOR
READ_ONLY
FOR SELECT  ‘Create View ‘ + name + ‘ AS SELECT * FROM  ‘ + @DBNAME +  name
FROM sys.objects AS so WHERE so.type = ‘U’
DECLARE @name varchar(MAX)
OPEN V

FETCH NEXT FROM V INTO @name
WHILE (@@fetch_status <> -1)
BEGIN
IF (@@fetch_status <> -2)
BEGIN

DECLARE @message varchar(max)
SELECT @message =  @name+ @NewLineChar +’GO’
PRINT @message
END
FETCH NEXT FROM V INTO @name
END

CLOSE V
DEALLOCATE V
GO

Trigger to Prevent any user to Create Table on Database


Event Can prevent happend on your database
CREATE_TABLE
CREATE_INDEX
CREATE_FUNCTION

Example

USE
DBNAME;
GO
CREATE TRIGGER Prevent_Create

ON
DATABASE FOR CREATE_TABLE
AS
PRINT ‘CREATE TABLE Issued.’
SELECT EVENTDATA().value
(‘(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]’,’nvarchar(max)’)
RAISERROR (‘New tables cannot be created in this database.’, 16, 1)
ROLLBACK;
GO
–Test the trigger.
CREATE TABLE T2 (C1 int);
GO
–Drop the trigger.
DROP TRIGGER Prevent_Create_Table ON DATABASE;
GO

Convert Date


in this article will learn how to convert date :

  • 1- From AD to Hegira
  • 2- From Hegira to AD

AD = “01/01/2011”   & Hegira  = “01/01/1432”

  1. Convert Date From AD To Hegira

EXAMPLE NO 1

set dateformat dmy
DECLARE @DATEM DATETIME
DECLARE @ResultH NCHAR(10)
SET @DATEM = ’19/03/2011′
SET @ResultH = (SELECT CONVERT(NCHAR(10), @DATEM, 131))
SELECT @ResultH
PRINT ‘The Date Time IS’ + ‘ ‘ + CAST(@ResultH AS NVARCHAR(50))
PRINT ‘————————————-‘

—————————————————————————————————–

EXAMPLE NO 2

Create Table Convert_Date
(ID int,
[date] datetime)

—- data type of [date] column must be datetime but if you will insert Hegira date the column data type must be Nvarchar
GO
Insert  into Convert_Date values (3,’01/01/2011′),(3,’10/09/2011′)
GO
SET DATEFORMAT DMY
SELECT   ID,[DATE] ,CONVERT(NCHAR(10),CAST([DATE] AS DATETIME ) ,131)
AS  Hegira FROM Convert_Date

—————————————————————————————————-

2-Convert Date From Hegira  to AD

Example No 1
DECLARE @ResultM DATETIME
DECLARE @DateH NVARCHAR(50)
SET @DATEH = ’09/04/1405′
SET @ResultM = (SELECT CONVERT(DATETIME, @dateH, 130))
SELECT CONVERT(NCHAR(10),@ResultM,103)
PRINT ‘The Date Time IS ‘ + ‘ ‘ + CAST(@ResultM AS NVARCHAR(50))
PRINT ‘————————————-‘