简体   繁体   English

根据CSV中的输入和输出执行另一个过程的主要存储过程

[英]Main Stored Procedure to Execute another Proc depending on Input and output in a CSV

I have two procedures which basically give an Table output. 我有两个基本给出表输出的过程。 I have the third Procedure which basically calls these two procedures and gives the output in an csv file format. 我有第三个过程,基本上调用了这两个过程,并以csv文件格式给出输出。

Can anyone help me in building this the right way. 任何人都可以帮助我以正确的方式进行构建。 Below is something I am trying to do: 以下是我正在尝试做的事情:

Each of the two Procedures gives out an output with like around 100k rows, I want to capture that and want to give the output here from the Main procedure in a csv file.(Please let me know if you need more info) 这两个过程中的每一个都给出大约10万行的输出,我想捕获该输出并想从Main过程中的csv文件中给出输出(请告诉我是否需要更多信息)

Create PROC MAIN
   @InputParam int
  AS
 Begin
 Set NOCOUNT ON; 

 BEGIN TRY
 IF @InputParam not in (1,2)
  BEGIN
    Print 'Error Message'
  END
  Else
   begin
   IF @InputParam=1 
     BEGIN
      Exec StoredProc1
      Print 'Stored Procedure StoredProc1 ended at '+Convert(Varchar(25),GETDATE(),21);
     End 
    Else 
      Begin 
      Print 'StoredProc1 does not exist'
      END

    IF @InputParam=2 
     BEGIN
      Exec StoredProc2
      Print 'Stored Procedure StoredProc2 ended at '+Convert(Varchar(25),GETDATE(),21);
     End 
    Else 
     BEGIN 
      Print 'StoredProc2 does not exist'
     END  
  END   -- This is END for ELSE loop

   END TRY

   Begin Catch
     Print 'Input Validation Catch Block with # '+ Convert(Varchar(5),ERROR_NUMBER())+'      Msg: '+ERROR_MESSAGE();

   End Catch
 END

What you may want to try is to declare a table vairable in your main stored procedure. 您可能想尝试的是在主存储过程中声明一个表vairable。 This table needs to match the output from the sub procedures exactly. 该表需要与子过程的输出完全匹配。 The syntax is: 语法为:

DECLARE @Temp TABLE(
  Filed1 INT,
  Field2 VARCHAR(100),
  etc
)

Then you can execute your sub stored procedure and insert into the table you defined, like this: 然后,您可以执行子存储过程并将其插入您定义的表中,如下所示:

INSERT INTO @Temp
EXEC StoredProc1

And finally, select from the @Temp 最后,从@Temp中选择

SELECT * FROM @Temp

You will obviously need to fit your exact requirements into this, but it should assist I hope 显然,您将需要满足您的确切要求,但是我希望它可以为您提供帮助

You cannot output to a CSV directly from a Stored Procedure without some form of code, so that really depends what you are developing with (ie what will call the stored procedure). 没有某种形式的代码,您不能直接从存储过程输出到CSV,因此这实际上取决于您要开发的内容(即称为存储过程的东西)。 You could perhaps build a SQL Job to output to CSV. 您也许可以构建一个SQL作业以输出到CSV。

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

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