简体   繁体   English

SQL Server-将参数列表转换为函数,将多个结果导出至Excel

[英]SQL Server - List of parameters into function, Export Multiple results into Excel

I have a function that takes an ID as an input. 我有一个函数,将ID作为输入。 The function gets all related IDs from the ID you input. 该功能从您输入的ID中获取所有相关ID。 When I run the function five times with different ID numbers such as: 当我使用不同的ID号运行该函数五次时,例如:

select * from [levelfunction](100)
select * from [levelfunction](101)
select * from [levelfunction](102)
select * from [levelfunction](104)
select * from [levelfunction](108)

CREATE TABLE #Table1(
    ID [int] NULL,
    Level [int] NULL)

    INSERT INTO #Table1 (ID, Level) VALUES (100,0)

CREATE TABLE #Table2(
    ID [int] NULL,
    Level [int] NULL)

    INSERT INTO #Table2 (ID, Level) VALUES (101,0)

CREATE TABLE #Table3(
    ID [int] NULL,
    Level [int] NULL)

    INSERT INTO #Table3 (ID, Level) VALUES (102,0)

CREATE TABLE #Table4(
    ID [int] NULL,
    Level [int] NULL)

    INSERT INTO #Table4 (ID, Level) VALUES (103,0), (104,1), (105,2), (106,3)

CREATE TABLE #Table5(
    ID [int] NULL,
    Level [int] NULL)

    INSERT INTO #Table5 (ID, Level) VALUES (107,0), (108,1), (109,2)

I get the below result: 我得到以下结果:

   select * from #Table1
   select * from #Table2
   select * from #Table3
   select * from #Table4
   select * from #Table5
+-----+-------+
| ID  | Level | 
+-----+-------+
| 100 |  0    | 
+-----+-------+
+-----+-------+
| ID  | Level | 
+-----+-------+
| 101 |  0    |
+-----+-------+
+-----+-------+
| ID  | Level | 
+-----+-------+    
| 102 |  0    |
+-----+-------+
+-----+-------+
| ID  | Level | 
+-----+-------+     
| 103 |  0    |
| 104 |  1    |
| 105 |  2    |
| 106 |  3    |
+-----+-------+ 
+-----+-------+
| ID  | Level | 
+-----+-------+ 
| 107 |  0    |
| 108 |  1    |
| 109 |  2    |
+-----+-------+

How do I - 我如何 -

-Put a list of IDs into a function without altering the function? -在不更改功能的情况下将ID列表放入功能中?

-Export all the tables into excel as 1 result? -将所有表导出到excel作为1结果?

Expected excel result:

+-----+-------+
| ID  | Level | 
+-----+-------+
| 100 |  0    | 
| 101 |  0    |
| 102 |  0    |
| 103 |  0    |
| 104 |  1    |
| 105 |  2    |
| 106 |  3    |
| 107 |  0    |
| 108 |  1    |
| 109 |  2    |
+-----+-------+

For 对于

Put a list of IDs into a function without altering the function? 在不更改功能的情况下将ID列表放入功能中?

use CROSS APPLY 使用交叉申请

declare @IDlist table(ID int)
insert into @IDlist(ID) values (100),(101),(102),(103),(104),(105),(106),(107)

select a.Id,a.Level from @IDlist d
cross apply levelfunction(d.ID) as a;

About export to excel was answered more then one time, for example here OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" returned message "Bookmark is invalid." 关于导出到excel的回答不止一次,例如,这里链接服务器“(null)”的OLE DB访问接口“ Microsoft.ACE.OLEDB.12.0”返回消息“书签无效”。

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

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