简体   繁体   English

使用 Scala 在 Databricks 中执行一系列存储过程

[英]Execute series of Stored procedures in Databricks using Scala

I am working in a migration project, where lift and shift method is used to migrate SQL server DB from onprem to AZure Cloud.我在一个迁移项目中工作,其中使用提升和移位方法将 SQL 服务器数据库从 onprem 迁移到 AZure Cloud。 There is a lot of stored procedures used for integration in On prem.Now here in On prem, to process the XMl file and execute the same procedures pointing to the cloud Db I need to write a code in Databricks. On prem 中有很多用于集成的存储过程。现在在 On prem 中,要处理 XMl 文件并执行指向云 Db 的相同过程,我需要在 Databricks 中编写代码。 I have code to execute a single stored procedure using Scala, as I am new to coding in python/scala I couldn't find the right method to execute the procedures.我有使用 Scala 执行单个存储过程的代码,因为我是 python/scala 编码的新手,我找不到正确的方法来执行这些过程。

The below code is what I have used to execute one procedure- sample下面的代码是我用来执行一个过程的代码示例

%scala

val username = "xxxxx"
val pass = "xxxxx"
val url = "jdbc:sqlserver://xxx.database.windows.net:1433;databaseName=xxx"
val table = "SalesLT.Temp3"
val query = s"EXEC sp_truncate_table '${table}'"


val conn = DriverManager.getConnection(url, username, pass)
val rs = conn.createStatement.execute(query)

I have a requirement to execute some 10 stored procedures in series.我需要串行执行大约 10 个存储过程。 Looking forward for your suggestions.期待您的建议。

To run multiple stored procedures sequentially you can create one Stored procedure and call all your stored procedures in that and call this stored procedure through Databricks.要按顺序运行多个存储过程,您可以创建一个存储过程并在其中调用所有存储过程,然后通过 Databricks 调用此存储过程。

The following code example is referred from here by @Thom A @Thom A此处引用了以下代码示例

CREATE PROC MySP1  @int  int AS  
     PRINT @int  /  0;  --To error  
GO  
  
CREATE PROC MySP2  @int  int AS   
     PRINT @int  +  1;  
GO  
  
CREATE PROC MySP3  @int  int AS  
     PRINT @int  +  2;  
GO  
  
CREATE PROC AllMySPs  @Int  int AS  
     PRINT 'Executing SP1...';  
     BEGIN TRY  
         EXEC MySP1  @Int;  
     END TRY  
     BEGIN CATCH  
         PRINT 'MySP1 failed';  
     END CATCH  
     PRINT 'Executing SP2...';  
     BEGIN TRY  
         EXEC MySP2  @Int;  
     END TRY  
     BEGIN CATCH  
         PRINT 'MySP2 failed';  
     END CATCH  
     PRINT 'Executing SP3...';  
     BEGIN TRY  
         EXEC MySP3  @Int;  
     END TRY  
     BEGIN CATCH  
         PRINT 'MySP3 failed';  
     END CATCH  
GO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM