将项目中用到的一个方法记录下
在ssis中,我们要将oracle的库通过ssis转储到sqlserver中,在oracle环境下的sql语句中有用到一个sqlserver库的动态值,所以就要用到变量。现将过程记录下,以备后用
1.首先定义两个变量,一个是存储原来sql的变量,也就是sql中带有变量,注意变量书写为 :xxx的形式,而且sql中不能有换行符等特殊符号,我这里的是
select ID, SiteID, StoreName, FolioNo, CCNo, Description, ItemRemark, Unit, WAC,BinLocation, LeadTime, MinStock, LogUser, LastUpdated, Status, Category, Remarks, CreateDate, LastIssueDate, LastTOCDate, LastPurchaseCost, StockBal, LastStockCheckDate, ForecastQty, Consumption, Consumption1, Consumption2, Consumption3, ConsumMonth, ConsumMonth1, ConsumMonth2, ConsumMonth3, Actions, LastPurchaseDate, BUDate, AvgMonthCon, OpernBalance, CloseBalance, MinStock+(AvgMonthCon*LeadTime) as ReOrderLevel, MonthConsumption, LastWAC, UNSPSC, 'SYS' as UIDLSTUPD, 'GDMX' as TRANIDLSTUPD, sysdate as DTMISLSTUPD from (select ID, SiteID, StoreName, FolioNo, CCNo, Description, ItemRemark, Unit, WAC, BinLocation, LeadTime, MinStock, LogUser, LastUpdated, Status, Category, Remarks, CreateDate, LastIssueDate, LastTOCDate, LastPurchaseCost, StockBal, LastStockCheckDate, ForecastQty, Consumption, Consumption1, Consumption2, Consumption3, ConsumMonth, ConsumMonth1, ConsumMonth2, ConsumMonth3, Actions, LastPurchaseDate, BUDate, case when (Consumption + Consumption1 + Consumption2 + Consumption3) <> 0 and (ConsumMonth + ConsumMonth1 + ConsumMonth2 + ConsumMonth3) <> 0 then ceil((Consumption + Consumption1 + Consumption2 + Consumption3/ConsumMonth + ConsumMonth1 + ConsumMonth2 + ConsumMonth3)*1000000)/1000000 else 0 end as AvgMonthCon, OpernBalance, CloseBalance, ReOrderLevel, MonthConsumption, LastWAC, UNSPSC from (select inve.itemnum as ID, null as SiteID, inve.location as StoreName, inve.itemnum as FolioNo, item.ccnum as CCNo, item.description as Description, null as ItemRemark, item.issueunit as Unit, invc.avgcost as WAC, inve.binnum as BinLocation, inve.deliverytime as LeadTime, inve.sstock as MinStock, null as LogUser, null as LastUpdated, item.status as Status, null as Category, item.remark as Remarks, item.createdate as CreateDate, inve.lastissuedate as LastIssueDate, null as LastTOCDate, invc.lastcost as LastPurchaseCost, invb.curbal as StockBal, invb.physcntdate as LastStockCheckDate, inve.forecastqty as ForecastQty, abs(invyc.qty) as Consumption, abs(invyc1.qty) as Consumption1, abs(invyc2.qty) as Consumption2, abs(invyc3.qty) as Consumption3, case when item.createdate is null then 0 else to_number(to_char(:batchRunTime, 'mm')) - 1 end as ConsumMonth, case when item.createdate is null or to_number(to_char(item.createdate, 'yyyy')) > to_number(to_char(:batchRunTime, 'yyyy')) - 1 then 0 when to_number(to_char(item.createdate, 'yyyy')) = to_number(to_char(:batchRunTime, 'yyyy')) - 1 then 12 - to_number(to_char(item.createdate, 'mm')) when to_number(to_char(item.createdate, 'yyyy')) < to_number(to_char(:batchRunTime, 'yyyy')) - 1 then 12 else null end as ConsumMonth1, case when item.createdate is null or to_number(to_char(item.createdate, 'yyyy')) > to_number(to_char(:batchRunTime, 'yyyy')) - 2 then 0 when to_number(to_char(item.createdate, 'yyyy')) = to_number(to_char(:batchRunTime, 'yyyy')) - 2 then 12 - to_number(to_char(item.createdate, 'mm')) when to_number(to_char(item.createdate, 'yyyy')) < to_number(to_char(:batchRunTime, 'yyyy')) - 2 then 12 else null end as ConsumMonth2, case when item.createdate is null or to_number(to_char(item.createdate, 'yyyy')) > to_number(to_char(:batchRunTime, 'yyyy')) - 3 then 0 when to_number(to_char(item.createdate, 'yyyy')) = to_number(to_char(:batchRunTime, 'yyyy')) - 3 then 12 - to_number(to_char(item.createdate, 'mm')) when to_number(to_char(item.createdate, 'yyyy')) < to_number(to_char(:batchRunTime, 'yyyy')) - 3 then 12 else null end as ConsumMonth3, null as Actions, null as LastPurchaseDate, null as BUDate, null as AvgMonthCon, null as OpernBalance, null as CloseBalance, null as ReOrderLevel, null as MonthConsumption, null as LastWAC, null as UNSPSC from inventory inve left join item on item.itemnum = inve.itemnum left join invcost invc on invc.itemnum = inve.itemnum and invc.siteid = inve.siteid and invc.location = inve.location left join (select itemnum, location, siteid, max(physcntdate) physcntdate, sum(curbal) curbal from invbalances group by itemnum, location, siteid) invb on invb.itemnum = inve.itemnum and invb.siteid = inve.siteid and invb.location = inve.location left join (select itemnum, storeloc, siteid, sum(quantity) qty from matusetrans where issuetype = 'ISSUE' and to_char(actualdate, 'yyyy') = to_char(:batchRunTime, 'yyyy') group by itemnum, storeloc, siteid) invyc on invyc.itemnum = inve.itemnum and invyc.storeloc = inve.location and invyc.siteid = inve.siteid left join (select itemnum, storeloc, siteid, sum(quantity) qty from matusetrans where issuetype = 'ISSUE' and to_char(actualdate, 'yyyy') = to_char(to_number(to_char(:batchRunTime, 'yyyy')) - 1) group by itemnum, storeloc, siteid) invyc1 on invyc1.itemnum = inve.itemnum and invyc1.storeloc = inve.location and invyc1.siteid = inve.siteid left join (select itemnum, storeloc, siteid, sum(quantity) qty from matusetrans where issuetype = 'ISSUE' and to_char(actualdate, 'yyyy') = to_char(to_number(to_char(:batchRunTime, 'yyyy')) - 2) group by itemnum, storeloc, siteid) invyc2 on invyc2.itemnum = inve.itemnum and invyc2.storeloc = inve.location and invyc2.siteid = inve.siteid left join (select itemnum, storeloc, siteid, sum(quantity) qty from matusetrans where issuetype = 'ISSUE' and to_char(actualdate, 'yyyy') = to_char(to_number(to_char(:batchRunTime, 'yyyy')) - 3) group by itemnum, storeloc, siteid) invyc3 on invyc3.itemnum = inve.itemnum and invyc3.storeloc = inve.location and invyc3.siteid = inve.siteid))
一个是用来从sqlserver中查出的动态值,其值为空
2.从toolbox中拖出一个 Execute SQL Task,按照下图编辑
图2
3.从toolbox中拖出一个script task 编辑
进行编辑
MsgBox(Dts.Variables("timestamp").Value)
Dts.Variables("execSQL").Value = Dts.Variables("execSQL").Value.ToString.Replace(":batchRunTime", "to_date('" & Dts.Variables("timestamp").Value.ToString & "','yyyy/mm/dd HH24:MI:SS')")
'MsgBox(Dts.Variables("execSQL").Value.ToString)
' Dim sw As StreamWriter = New StreamWriter("d:\vb.txt", False)
'sw.WriteLine(Dts.Variables("execSQL").Value.ToString)
' sw.Flush()
' sw.Close()
Dts.TaskResult = Dts.Results.Success
4.在data flow中选择最终修改后的sql变量
到此这个工作就完成了。。。
下来还有一种方案,就是在script task中直接连接数据库,这样就只需要定义一个变量,
Public Sub Main()
Dim sConnectionString As String
sConnectionString = "Provider=SQLOLEDB.1;server=机器名;database=数据库名;uid=sa;pwd=密码"
Dim myConnection As New OleDb.OleDbConnection(sConnectionString)
Dim sSQL As String
sSQL = "select left(LSYSRVAL.PARMVAL,10) from LSYSRVAL where PARMNAME = 'GLDMAXTOMISLBDTMP'"
Dim myCommand As New OleDb.OleDbCommand(sSQL, myConnection)
Dim result As String
myConnection.Open()
result = CStr(myCommand.ExecuteScalar)
result = "to_date('" & result & "','yyyy-mm-dd')"
MsgBox(result)
Dts.Variables("STOCKMSTSQL").Value = Dts.Variables("STOCKMSTSQL").Value.ToString.Replace("sysdate", result)
myConnection.Close()
Dim sw As StreamWriter = New StreamWriter("d:\DataView.txt", False)
sw.WriteLine(Dts.Variables("STOCKMSTSQL").Value.ToString)
sw.Flush()
sw.Close()
Dts.TaskResult = Dts.Results.Success
End Sub
ok,完成
- 大小: 15.8 KB
- 大小: 13.8 KB
- 大小: 15.2 KB
- 大小: 12 KB
分享到:
相关推荐
例如,在事务中使用rowversion来标识和更新数据,就可以避免两个事务同时修改同一行数据时造成的丢失更新问题。 接下来,我们了解SSIS中VB代码的使用。Visual Basic(简称VB)是Microsoft推出的一种面向对象的编程...
- **Package Variables**: 介绍在此上下文中使用的变量。 - **Package Data Flows**: 详细描述使用Merge Join的数据流设计。 - **Word of Caution**: 强调在使用Merge Join时需要注意的问题。 - **Merge Join ...
2. **使用SQL Server作业**:可以利用SQL Server Agent中的作业功能,定期自动执行SSIS包,实现自动化导入。 ### 八、解决发布错误 在发布SSIS包到SQL Server时,可能会遇到以下错误: - **代码**: 0xC0016016 - ...
SQL Server 2005及以后版本引入了更强大的Integration Services(SSIS),虽然DTS在SQL Server 2000中非常实用,但SSIS在功能、性能和可扩展性上都有显著提升。然而,对于仍在使用SQL Server 2000的环境,理解并熟练...
6. **变量的使用**:每个包都包含用于存储错误消息和审计消息的相同变量。这些变量是处理异常和追踪信息的关键。 7. **事件处理器**:通过事件处理器记录错误消息,提供更详细的故障排查信息,增强了系统的可追溯性...
5. asp.net:可能包含与ASP.NET Web应用程序相关的示例代码或配置信息,说明如何在Web环境中使用Oracle Provider for OLE DB。 6. instantclient_11_2:这是Oracle Instant Client的11.2版本,它是一个轻量级的...
4. README或其他文档:提供项目指南、安装步骤、使用说明等。 5. 测试脚本或自动化脚本:用于验证SSIS包和Lambda函数的正确性。 现代化SSIS包利用AWS Lambda涉及的关键知识点: 1. AWS Lambda基础知识:了解如何...
此资源包括一个详细的使用说明书和一个体验版的软件,使得学习过程更加直观和实践导向。 SQL Server编程是数据库管理系统中的关键技能,涵盖了SQL查询语言、数据库设计、存储过程、触发器、索引优化等多个方面。SQL...
《SQL Server 精华集》是一本涵盖了数据库操作核心知识的资源集合,它为学习和精通SQL Server提供了丰富的资料。...同时,提供的阅读器下载和E书说明将帮助你更好地使用和理解这些资源,使学习过程更加顺畅。
关于数据操作,第四章详细说明了SQL的连接(JOIN)语句,包括INNER JOIN、LEFT/RIGHT/FULL OUTER JOIN和CROSS JOIN等,这些连接类型是整合来自多个表的数据的关键技术。 创建和修改表是数据库设计的核心环节,第五...
在本《SQL Server 手把手教程》中,我们将深入探讨SQL语言的基础以及如何在SQL Server 2000环境中高效地使用它。SQL(Structured Query Language),即结构化查询语言,是用于管理关系数据库的标准语言,包括数据...
文档中还提供了对可再发行 CAB 文件的说明,这些文件可能包含必要的组件和库,用于支持 BizTalk Server 的某些功能。 ### 结论 BizTalk Server 2006 的安装和配置是一个复杂的过程,涉及多个步骤和组件。遵循官方...