TSQL de With kullanarak belirli sayıda elemanı olan bir seri oluşturabilirsiniz. Örneğin 1 den başlayarak 1000'e kadar olan integer sayılardan oluşan bir seri oluşturmak istiyorsanız aşağıdaki scripti kullanabilirsinizInteger-Serisi
123456789WITHT(ID)AS(SELECT1 IDUNIONALLSELECTID + 1FROMTWHEREID < 1000)SELECT*FROMTOPTION(MAXRECURSION 1000)Eğer bir tarihten başlayarak belirli bir tarihe kadar olan tüm günleri barındıran bir seri oluşturmak istersek bu sefer aşağıdaki gibi bir script kullanmamız gerekir.Aşağıdaki script 2011.01.01 tarihinden itibaren tarihe 1 gün ekleyerek bugune kadar tüm tarihleri oluşturur. maxrecursion 1000 diyerek ise en fazla 1000 tane oluştur demiş oluyoruz. 2011.01.01 den günümüze(2012.10.05) yaklaşık 645 gün olduğu için max recursion sayısına ulaşamayız. Eğer bu değeri düzgn vermez isek aşağıdaki hatayı alırız.Msg 530, Level 16, State 1, Line 1
The statement terminated. The maximum recursion 1000 has been exhausted before statement completion.İki Tarih Arasındaki tüm Günler için Seri Oluşturmak
12345678910WITHT(Date)AS(SELECTCONVERT(datetime,'20110101')DateUNIONALLSELECTDATEADD(dd, 1,Date)FROMTWHEREDate< GETDATE())SELECT*FROMTOPTION(MAXRECURSION 1000)
5 Ekim 2012 Cuma
TSQL ile Belirli bir Sayı veya Tarih Aralığında Bir Seri Üretmek
9 Eylül 2012 Pazar
Sql'de Linked Server Olmadan Uzaktaki Sunucuya Bağlantı
Bildiğiniz gibi LinkedServer tanımlayarak A server’ı üzerinden B Server’ında select,update gibi işlemleri yapabilmemiz mümkün.
Bugün anlatacağım OpenDataSource komutu ile linked server’a gerek kalmadan bu işlemleri nasıl yapabileceğimizi görüyor olacağız.
OpenDataSource kullanılabilmesi için sorguyu çalıştıran server’da Ad Hoc Distributed Queriesözelliğinin enable edilmesi gerekmektedir.
Aksi halde şöyle bir hata alınır.
Msg 15281, Level 16, State 1, Line 1
SQL Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ad Hoc Distributed Queries' by using sp_configure. For more information about enabling 'Ad Hoc Distributed Queries', see "Surface Area Configuration" in SQL Server Books Online.
SQL Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ad Hoc Distributed Queries' by using sp_configure. For more information about enabling 'Ad Hoc Distributed Queries', see "Surface Area Configuration" in SQL Server Books Online.
Ad Hoc Distributed Queries özelliğini aktif etmek için sorguyu çalıştıran server’da şu script’i çalıştırabilirsiniz.
1 | exec sp_configure 'show advanced options',1 |
2 | ReConfigure with override |
3 | exec sp_configure 'Ad Hoc Distributed Queries',1 |
4 | ReConfigure with override |
5 | exec sp_configure 'show advanced options',0 |
6 | ReConfigure with override |
Şimdi kullanıma bakalım.
1 | SELECT top 10 * FROM OPENDATASOURCE( 'SQLOLEDB', |
2 | 'Data Source=Server2\sql2008;User ID=LoginName;Password=Password').ADRES.dbo.IL |
Kaydol:
Kayıtlar (Atom)