Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2005-10-14 : 08:03:28
|
aman writes "can you give me the codes or structure of codes with which i can retrieve data from a database using batch file?" |
|
danielforrester
Starting Member
1 Post |
Posted - 2005-10-14 : 12:33:06
|
Ahmad,Here is some code for something close to what you want to do which i currently use.This works by updating the filename property of the Textfile source object you have your DTS package. What you need to do is create a package that reads a textfile into a table then add this code to a activex script task before datapump. Obviously you are going to have to modify it to get it to work but the concept is there.--------------------------------------------------------Function MainDim sDir, sFilenameDim fsoDim oFolderDim oFile, sDatasourceDim oConnection, oRecordsetDim sSQLDim sServerNameDim oPackageDim sFiles_Loaded_DBsServerName = DTSGlobalVariables("ServerName")If sServerName = "" Then sServerName = "(local)"'sFiles_Loaded_DB = DTSGlobalVariables("Files_Loaded_DB")sFiles_Loaded_DB = "GBR_STAGING"sDir = DTSGlobalVariables("Directory")If right(sDir,1) <> "\" Then sDir = sDir + "\"Set oConnection = CreateObject("ADODB.Connection")Set oRecordset = CreateObject("ADODB.Recordset")oConnection.Open "Provider=sqloledb;Data Source=" + sServerName + ";Initial Catalog=" + sFiles_Loaded_DB + ";Integrated Security=SSPI;"'Create the Files_LoadedsSQL = "If Not Exists(Select name From sysobjects Where name='Files_Loaded' and xtype='U')"sSQL = sSQL + " Create Table Files_Loaded"sSQL = sSQL + "(row_id integer Identity(1,1),load_date datetime NOT NULL Default(getdate()),filename varchar(256),outcome bit default(0))"oConnection.Execute sSQLSet fso = CreateObject("Scripting.FileSystemObject")Set oFolder = fso.GetFolder(sDir)sDatasource = ""For Each oFile in oFolder.Files' If lcase(Left(oFile.Name,6)) = "expshk" Then sSQL = "Select row_id From Files_Loaded Where filename='" + sDir + oFile.name + "' And outcome=1" oRecordset.Open sSQL, oConnection,3 If oRecordset.RecordCount = 0 Then sDatasource = sDir + oFile.name oRecordset.close' End IfNextDTSGlobalVariables("Datasource") = sDatasourceSet oPackage = DTSGlobalVariables.Parent'Prevent the package from runningIf sDatasource = "" Then 'MsgBox "Disabled" oPackage.Steps("Load Data File").DisableStep = trueElse'Run with new source 'MsgBox "Loading" Set oTextFileConnection = oPackage.Connections("TextFile") oTextFileConnection.DataSource = sDatasource sSQL = "INSERT INTO Files_Loaded(filename) Values('" + sDatasource + "')" oConnection.Execute sSQL oPackage.Steps("Load Data File").DisableStep = FalseEnd IfoConnection.CloseSet oConnection = NothingSet oRecordset = NothingSet file = NothingSet folder = NothingSet fso = NothingMain = DTSTaskExecResult_SuccessEnd Function |
 |
|
|
|
|
|
|