بازسازی seed یک ستون identity در sql server

dbcc checkident('TableName',reseed,0)

برای وارد کردن ردیفی با مقدار identity مشخص از مثال زیر می شود استفاده کرد:

set identity_insert TableName on

insert into TableName ([identityCol],[col1],[col2],[col3])

select 1,'33',654564,'2012/11/13'

ادامه نوشته

تابع تبدیل تاریخ میلادی به شمسی در sql server

تابع تبدیل تاریخ میلادی به شمسی در sql server برگرفته از این وبلاگ. من تست کردم درست کار می کرد. تابع در ادامه مطلب می باشد.
ادامه نوشته

روابط در Entity Framework

در حال نوشتن مطلبی درباره تعریف روابط در EF 4.1 و Code First هستم. مقاله هنوز ناقصه ولی لینکش را اینجا میگذارم.


نسخه docx

نسخه pdf

تهیه پشتیبان Backup از تمامی پایگاه های داده ای Database در Sql Server

مطلب جالبی در اینجا هست درباره پشتیبان گیری توسط T-SQL که بسیار کاراست. برای اینکار فقط کافیست آدرس path را تغییر داد، بقیه کد بدون تغییر باقی می ماند:

DECLARE @name VARCHAR(50) -- database name

DECLARE @path VARCHAR(256) -- path for backup files

DECLARE @fileName VARCHAR(256) -- filename for backup

DECLARE @fileDate VARCHAR(20) -- used for file name

SET @path = 'D:\Backup\'

SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)

 DECLARE db_cursor CURSOR FOR

SELECT name

FROM master.dbo.sysdatabases

WHERE name NOT IN ('master','model','msdb','tempdb')

 OPEN db_cursor 

FETCH NEXT FROM db_cursor INTO @name 

 

WHILE @@FETCH_STATUS = 0 

BEGIN 

       SET @fileName = @path + @name + '_' + @fileDate + '.BAK'

       BACKUP DATABASE @name TO DISK = @fileName

  FETCH NEXT FROM db_cursor INTO @name 

END 

CLOSE db_cursor 

DEALLOCATE db_cursor

There is no row at position 0

تشکر از نویسنده این مقاله (هرچه کردم اسمش را پیدا نکردم)

چند وقتی بود که وقتی می خواستم وارد Properties یا پنجره ها و Dialog های دیتابیس هام روی SQL server 2008 R2 بشم Error ای با شرح "There is no row at position 0" می گرفتم. مقاله بالا کمکم کرد با اجرا کردن دستور زیر مشکل را برطرف کنم.

C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Install>sqlcmd -S (local) -i u_tables.sql -E

A very common Not answered question

[ Configuration Database ] TF255062: A connection cannot be made to the following SQL Server Instance: Communited11\Development. Verify that you have entered the correct name for the server, including the instance name, that the server you are attempting to connect to is online, and that you have the required permissions to connect. If you are attempting to connect to the remote SQL Server instance, then verify that it is configured to allow remote connections, that TCP/IP protocol is both enabled and configured to use port 1433 (default), and that Windows Firewall does not block this port.

این TFS بعضی وقتها عجب ما را سر کار می گذاره ! دیروز هر کار کردیم که configure اش کنیم نشد که نشد. امروز اومدم سر کار و از یک سیستم دیگر که داشت خوب کار می کرد نگاه کردم دیدم تو security دیتابیس sql server 2008 r2 کاربر Administrator وجود داره ولی تو اون سیستمی که مشکل داشت این user نبود.

شک کردم نکنه از اون وقتی که من کاربر قبلی سیستم و پاک کردم و با administrator وارد می شم این مشکل بوجود اومده باشه رفتم تو اون سیستمی که سر کار گذاشته بود ما را (نگارش فارسی صفر ولی خطم خوبه) با windows authentication اومدم بالا دیدم بعععععععععله مشکل از همینه خودم هم نمی تونستم با اکانت ویندوز بالا بیام حالا Team foundation server که جای خود دارد.

خلاصه اینکه به راحتی با اضافه کردن اکانت جدید که administrator بود و دادن sysadmin بهش مشکل را حل کردم و دوباره به configure کردن TFS پرداختم.


Test if you can login to sql server by windows authentication user, if there is problem in connecting to Sql Server then go to security panel and add the Administrator or the current user account.


kill all current connections یا حذف تمام کانکشن ها در sql server

برای این کار می توان از اسکریپت زیر استفاده کرد:

-- set your current connection to use master otherwise you might get an error

use master
ALTER DATABASE YourDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE

--do you stuff here

ALTER DATABASE YourDatabase SET MULTI_USER

برطرف کردن مشکل اتصال به دیتابیس

 (provider: Named Pipes Provider, error: 40 – Could not open a connection to SQL Server) (Microsoft SQL Server, Error: )


Regular readers of my blog are aware of the fact that I have written about this subject umpteen times earlier, and every time I have spoken about a new issue related to it. Few days ago, I had redone my local home network. I have LAN setup with wireless router connected with my four computers, two mobile devices, one printer and one VOIP solution. I had also formatted my primary computer and clean installed SQL Server 2008 into it. Yesterday, incidentally, I was sitting in my yard trying to connect SQL Server located in home office and suddenly I stumbled upon the following error. Finding the solution was the most infuriating part as it consumed my precious 10 minutes.

ref:

http://blog.sqlauthority.com/2009/05/21/sql-server-fix-error-provider-named-pipes-provider-error-40-could-not-open-a-connection-to-sql-server-microsoft-sql-server-error/


Remote Connection to sql server 2008

نحوه نصب و اتصال در windows 7:

1- به start/microsoft sql server 2008 r2/configuration tools/sql server configuration manager می رویم.

2- از پنجره باز شده Protocols for mssqlserver را پیدا کرده.

3- TCP/IP و Named Pipes  را فعال می کنیم.

4- از microsoft sql server management studio  بر روی  instance مورد نظر کلیک راست و Properties را انتخاب کرده.

5- از قسمت Connections تیک Allow Remote connection to this server را می زنیم.

در این مرحله باید کامپیوتر های دیگر بتوانند به دیتا بیس این سرور کانکت شوند .

* در صورتیکه هنوز مشکلی در ارتباط با سرور وجود داشت باید بررسی شود که آیا این کامپیوتر روی پورت 1433 در حال Listening هست یا خیر netstat /ano

* فایروال ویندوز یا خاموش شود یا پورت بالا برای TCP/IP باز شود. windows firewall

Shrink

کاهش حجم فایل log دیتابیس 2005 MS SQL SERVER

backup log [DbName] with NO_LOG
dbcc shrinkfile ([DbName_Log],1)

کاهش حجم فایل log دیتابیس 2008 MS SQL SERVER

Alter database [DbName] SET Recovery simple
DBCC SHRINKFILE (N' DbLogName' , 1)


Delete All Database Records

Set NoCount ON


Declare @tableName varchar(200)
Declare @tableOwner varchar(100)
Declare @skipident varchar(3)
Declare @identInitValue int

set @tableName = ''
set @tableOwner = ''
set @skipident = 'NO'
set @identInitValue=1

/*
Step 1: Disable all constraints
*/

exec sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
exec sp_MSforeachtable 'ALTER TABLE ? DISABLE TRIGGER ALL'

/*
Step 2: Delete the data for all child tables & those which has no relations
*/

While exists
(
select T.table_name from INFORMATION_SCHEMA.TABLES T
left outer join INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
on T.table_name=TC.table_name where (TC.constraint_Type ='Foreign Key'
or TC.constraint_Type is NULL) and
T.table_name not in ('dtproperties','sysconstraints','syssegments')
and Table_type='BASE TABLE' and T.table_name > @TableName
)


Begin
Select top 1 @tableOwner=T.table_schema,@tableName=T.table_name from INFORMATION_SCHEMA.TABLES T
left outer join INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
on T.table_name=TC.table_name where (TC.constraint_Type ='Foreign Key'
or TC.constraint_Type is NULL) and
T.table_name not in ('dtproperties','sysconstraints','syssegments')
and Table_type='BASE TABLE' and T.table_name > @TableName
order by t.table_name


--Delete the table
Exec('DELETE FROM '+ @tableOwner + '.' + @tableName)

--Reset identity column
If @skipident = 'NO'
If exists(
SELECT * FROM information_schema.columns
WHERE COLUMNPROPERTY(OBJECT_ID(
QUOTENAME(table_schema)+'.'+QUOTENAME(@tableName)),
column_name,'IsIdentity')=1
)
begin
set @identInitValue=1
set @identInitValue=IDENT_SEED(@tableOwner + '.' + @tableName)
DBCC CHECKIDENT (@tableName, RESEED, @identInitValue)
end

checkpoint
End

/*
Step 3: Delete the data for all Parent tables
*/

set @TableName=''
set @tableOwner=''

While exists
(
select T.table_name from INFORMATION_SCHEMA.TABLES T
left outer join INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
on T.table_name=TC.table_name where TC.constraint_Type ='Primary Key'
and T.table_name <>'dtproperties'and Table_type='BASE TABLE'
and T.table_name > @TableName
)


Begin
Select top 1 @tableOwner=T.table_schema,@tableName=T.table_name from INFORMATION_SCHEMA.TABLES T
left outer join INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
on T.table_name=TC.table_name where TC.constraint_Type ='Primary Key'
and T.table_name <>'dtproperties'and Table_type='BASE TABLE'
and T.table_name > @TableName
order by t.table_name

--Delete the table
Exec('DELETE FROM '+ @tableOwner + '.' + @tableName)

--Reset identity column
If @skipident = 'NO'
If exists(
SELECT * FROM information_schema.columns
WHERE COLUMNPROPERTY(OBJECT_ID(
QUOTENAME(table_schema)+'.'+QUOTENAME(@tableName)),
column_name,'IsIdentity')=1
)
begin
set @identInitValue=1
set @identInitValue=IDENT_SEED(@tableOwner + '.' + @tableName)
DBCC CHECKIDENT (@tableName, RESEED, @identInitValue)
end

checkpoint

End

/*
Step 4: Enable all constraints
*/

exec sp_MSforeachtable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
exec sp_MSforeachtable 'ALTER TABLE ? ENABLE TRIGGER ALL'

Set NoCount Off
 
 
-----------------
نقل از لینک زیر:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=65341