Selamlar, elimde bir sql kod var. SQLmanagament'e çalışıyor ve yedek alıyor. Bunu excel'de çalıştırmak istiyorum. Aşağıdaki şekilde denedim incorrect syntax near 'try' hatası aldım. Çözüm bula bilir miyiz?
Sub yedekal()
'SQLCON.ConnectionString = " Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=TIGER;Data Source=muhasebe\sqlexpress "
SQLCON.ConnectionTimeout = 5 ' Time out for the connection
SQLCON.Provider = "sqloledb" ' OLEDB Providers
' SQLCON.Properties("Network Address").Value = SERVER ' set the ip address of your sql server
SQLCON.CommandTimeout = 50000 ' set timeout for 3 minutes
SQLCON.Properties("Data Source").Value = Sayfa9.Range("F1")
SQLCON.Properties("Initial Catalog").Value = Sayfa9.Range("F2")
SQLCON.Properties("User ID").Value = Sayfa9.Range("F3")
SQLCON.Properties("Password").Value = Sayfa9.Range("F4")
SQLCON.Open
Sql = ""
Sql = Sql + " begin try "
Sql = Sql + " declare @yil varchar(4) = '2015' "
Sql = Sql + " declare @genel varchar(5) = 'GENEL' "
Sql = Sql + " declare @dizin varchar(max) = 'D:\AdminMG_DB_YDK\' "
Sql = Sql + " declare @dbname nvarchar(max) ='', @sql nvarchar(max)='' "
Sql = Sql + " declare @table table (mesaj varchar(max))"
Sql = Sql + " declare @count int = 0"
Sql = Sql + " declare c cursor for SELECT name FROM sys.databases where name like '%'+@yil+'%' Or name like '%GENEL' "
Sql = Sql + " open c "
Sql = Sql + " fetch next from c into @dbname "
Sql = Sql + " while @@FETCH_STATUS = 0 "
Sql = Sql + " begin "
Sql = Sql + " begin try "
Sql = Sql + " set @sql = 'backup database '+@dbname+' to disk='''+@dizin+@dbname+'.bak'' ' "
Sql = Sql + " EXECUTE sp_executesql @sql "
Sql = Sql + " end try "
Sql = Sql + " begin catch "
Sql = Sql + " insert into @table select @dbname + ' database nin yedeği alınamadı. Hata Kodu : '+ Error_Message() "
Sql = Sql + " set @count = 1 "
Sql = Sql + " end catch "
Sql = Sql + " fetch next from c into @dbname "
Sql = Sql + " End "
Sql = Sql + " Close C "
Sql = Sql + " deallocate C "
Sql = Sql + " if @count = 0 "
Sql = Sql + " select 'Tüm database lerin yedeği başarılı bir şekilde alındı.' "
Sql = Sql + " Else "
Sql = Sql + " end try "
Sql = Sql + " begin catch "
Sql = Sql + " end catch "
RS.Open Sql, SQLCON
SQLCON.Close
End Sub
Sub yedekal()
'SQLCON.ConnectionString = " Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=TIGER;Data Source=muhasebe\sqlexpress "
SQLCON.ConnectionTimeout = 5 ' Time out for the connection
SQLCON.Provider = "sqloledb" ' OLEDB Providers
' SQLCON.Properties("Network Address").Value = SERVER ' set the ip address of your sql server
SQLCON.CommandTimeout = 50000 ' set timeout for 3 minutes
SQLCON.Properties("Data Source").Value = Sayfa9.Range("F1")
SQLCON.Properties("Initial Catalog").Value = Sayfa9.Range("F2")
SQLCON.Properties("User ID").Value = Sayfa9.Range("F3")
SQLCON.Properties("Password").Value = Sayfa9.Range("F4")
SQLCON.Open
Sql = ""
Sql = Sql + " begin try "
Sql = Sql + " declare @yil varchar(4) = '2015' "
Sql = Sql + " declare @genel varchar(5) = 'GENEL' "
Sql = Sql + " declare @dizin varchar(max) = 'D:\AdminMG_DB_YDK\' "
Sql = Sql + " declare @dbname nvarchar(max) ='', @sql nvarchar(max)='' "
Sql = Sql + " declare @table table (mesaj varchar(max))"
Sql = Sql + " declare @count int = 0"
Sql = Sql + " declare c cursor for SELECT name FROM sys.databases where name like '%'+@yil+'%' Or name like '%GENEL' "
Sql = Sql + " open c "
Sql = Sql + " fetch next from c into @dbname "
Sql = Sql + " while @@FETCH_STATUS = 0 "
Sql = Sql + " begin "
Sql = Sql + " begin try "
Sql = Sql + " set @sql = 'backup database '+@dbname+' to disk='''+@dizin+@dbname+'.bak'' ' "
Sql = Sql + " EXECUTE sp_executesql @sql "
Sql = Sql + " end try "
Sql = Sql + " begin catch "
Sql = Sql + " insert into @table select @dbname + ' database nin yedeği alınamadı. Hata Kodu : '+ Error_Message() "
Sql = Sql + " set @count = 1 "
Sql = Sql + " end catch "
Sql = Sql + " fetch next from c into @dbname "
Sql = Sql + " End "
Sql = Sql + " Close C "
Sql = Sql + " deallocate C "
Sql = Sql + " if @count = 0 "
Sql = Sql + " select 'Tüm database lerin yedeği başarılı bir şekilde alındı.' "
Sql = Sql + " Else "
Sql = Sql + " end try "
Sql = Sql + " begin catch "
Sql = Sql + " end catch "
RS.Open Sql, SQLCON
SQLCON.Close
End Sub