簡體   English   中英

SQL Server-將參數列表轉換為函數,將多個結果導出至Excel

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

我有一個函數,將ID作為輸入。 該功能從您輸入的ID中獲取所有相關ID。 當我使用不同的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)

我得到以下結果:

   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    |
+-----+-------+

我如何 -

-在不更改功能的情況下將ID列表放入功能中?

-將所有表導出到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    |
+-----+-------+

對於

在不更改功能的情況下將ID列表放入功能中?

使用交叉申請

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;

關於導出到excel的回答不止一次,例如,這里鏈接服務器“(null)”的OLE DB訪問接口“ Microsoft.ACE.OLEDB.12.0”返回消息“書簽無效”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM