Excel 如何使用 Transact SQL 从 SQL 服务器导入数据

Excel How to import data from SQL Server using Transact SQL

我想使用 TSQL 将 SQL 服务器数据导入 Excel。在Excel中,我可以用SQL代码完美地检索到table。但是当我写 TSQL 代码而不是 SQL 它给了我 "Nothing to retrive".

Excel支持TSQL码吗?如果可以,我如何将数据从 SQL 服务器导入到 excel?

顺便说一句,我有这个示例代码。

use GOPLUS;
DECLARE @kayitNo INT;
DECLARE @CardRefOfItem INT;
DECLARE @CardRefOfPrice INT;
DECLARE @urunKodu VARCHAR(100);
DECLARE @urunAdi VARCHAR(100);
DECLARE @fiyat1 INT;
DECLARE @fiyat2 INT;
DECLARE @fiyat3 INT;
DECLARE @numb INT;
DECLARE @kayitSayisi INT = (SELECT COUNT(*) FROM LG_001_ITEMS);
DECLARE @sayac INT = 0;

CREATE TABLE #UrunListesis( 
Sira INT NOT NULL PRIMARY KEY,
Ürün_Kodu VARCHAR(100),
Ürün_Adı VARCHAR(100),
Fiyat1 INT,
Fiyat2 INT,
Fiyat3 INT
)
set @kayitNo = (SELECT TOP 1 LOGICALREF FROM LG_001_ITEMS ORDER BY LOGICALREF ASC)

WHILE @sayac < @kayitSayisi
BEGIN

    set @urunKodu = (SELECT LG_001_ITEMS.CODE FROM LG_001_ITEMS WHERE LG_001_ITEMS.LOGICALREF = @kayitNo)
    set @urunAdi = (SELECT LG_001_ITEMS.NAME FROM LG_001_ITEMS WHERE LG_001_ITEMS.LOGICALREF = @kayitNo)

    set @CardRefOfPrice = (SELECT TOP 1 LOGICALREF FROM LG_001_PRCLIST WHERE CARDREF = @kayitNo)

    set @numb = (SELECT COUNT(*) FROM LG_001_PRCLIST WHERE LG_001_PRCLIST.CARDREF = @kayitNo)
    if (@numb >=1) 
    begin
        set @fiyat1 = (SELECT LG_001_PRCLIST.PRICE FROM LG_001_PRCLIST WHERE LG_001_PRCLIST.LOGICALREF = @CardRefOfPrice)
    end
    if (@numb >=2)
    begin
    set @fiyat2 = (SELECT LG_001_PRCLIST.PRICE FROM LG_001_PRCLIST WHERE LG_001_PRCLIST.LOGICALREF = @CardRefOfPrice+1)
    end
    if (@numb =3)
    begin
    set @fiyat3 = (SELECT LG_001_PRCLIST.PRICE FROM LG_001_PRCLIST WHERE LG_001_PRCLIST.LOGICALREF = @CardRefOfPrice+2)
    end

    INSERT INTO #UrunListesis VALUES(@kayitNo,@urunKodu,@urunAdi,ISNULL(@fiyat1,0),ISNULL(@fiyat2,0),ISNULL(@fiyat3,0));
    set @kayitNo = @kayitNo+1;
    set @sayac = @sayac + 1
    set @fiyat1 = 0
    set @fiyat2 = 0
    set @fiyat3 = 0
END

SELECT * FROM #UrunListesis

很可能我只是想要 return 临时 table UrunListesis 到 Excel 就是这样。

另一种方法

您可以使用 xp_cmdshell 将 sql 导入 xls 文件,从而以这种格式插入您的 table 结构和数据:

<?xml version="1.0" standalone="yes"?>
<Columns>
<ColumnData Column1="asd" Column2="dsa" Column3="ads" />
<ColumnData Column1="asd1" Column2="dsa2" Column3="ads3" />
</Columns>

使用类似

的东西
exec xp_cmdshell 'echo ^<?xml version="1.0" standalone="yes"?^> >>C:\file.xls'

您可以创建一个带有 tableName 参数的过程,它将以最高格式格式化 table 和数据,并 return 一个输出变量。

在 excel(2010) 中,我使用以下步骤对 return 数据进行 table 或数据透视格式:

  • 打开 Excel 单击“数据”选项卡 select "From Other Sources" select "From SQL Server"

输入服务器信息

在导入数据屏幕上

  • select 属性转到定义选项卡将命令类型设置为 SQL 输入 SQL 查询命令文本

select想要的数据输出,点击确定。

我解决了我的问题。如果你想在 Excel 中使用 TSQL。你应该声明一个存储过程。像这样;

USE GOPLUS
GO
CREATE PROCEDURE deneme
AS
DECLARE @Urunlistesi TABLE( 
Sira int,
Ürün_Kodu VARCHAR(100),
Ürün_Adı VARCHAR(100),
Fiyat1 int,
Fiyat2 int,
Fiyat3 int
)
BEGIN
SET NOCOUNT ON
DECLARE @kayitNo INT;
DECLARE @CardRefOfItem INT;
DECLARE @CardRefOfPrice INT;
DECLARE @urunKodu VARCHAR(100);
DECLARE @urunAdi VARCHAR(100);
DECLARE @fiyat1 INT;
DECLARE @fiyat2 INT;
DECLARE @fiyat3 INT;
DECLARE @numb INT;
DECLARE @kayitSayisi INT = (SELECT COUNT(*) FROM LG_001_ITEMS);
DECLARE @sayac INT = 0;

set @kayitNo = (SELECT TOP 1 LOGICALREF FROM LG_001_ITEMS ORDER BY LOGICALREF ASC)

WHILE @sayac < @kayitSayisi
BEGIN

    set @urunKodu = (SELECT LG_001_ITEMS.CODE FROM LG_001_ITEMS WHERE LG_001_ITEMS.LOGICALREF = @kayitNo)
    set @urunAdi = (SELECT LG_001_ITEMS.NAME FROM LG_001_ITEMS WHERE LG_001_ITEMS.LOGICALREF = @kayitNo)

    set @CardRefOfPrice = (SELECT TOP 1 LOGICALREF FROM LG_001_PRCLIST WHERE CARDREF = @kayitNo)

    set @numb = (SELECT COUNT(*) FROM LG_001_PRCLIST WHERE LG_001_PRCLIST.CARDREF = @kayitNo)
    if (@numb >=1) 
    begin
        set @fiyat1 = (SELECT LG_001_PRCLIST.PRICE FROM LG_001_PRCLIST WHERE LG_001_PRCLIST.LOGICALREF = @CardRefOfPrice)
    end
    if (@numb >=2)
    begin
    set @fiyat2 = (SELECT LG_001_PRCLIST.PRICE FROM LG_001_PRCLIST WHERE LG_001_PRCLIST.LOGICALREF = @CardRefOfPrice+1)
    end
    if (@numb =3)
    begin
    set @fiyat3 = (SELECT LG_001_PRCLIST.PRICE FROM LG_001_PRCLIST WHERE LG_001_PRCLIST.LOGICALREF = @CardRefOfPrice+2)
    end

    INSERT INTO @Urunlistesi VALUES(@kayitNo,@urunKodu,@urunAdi,ISNULL(@fiyat1,0),ISNULL(@fiyat2,0),ISNULL(@fiyat3,0));
    set @kayitNo = @kayitNo+1;
    set @sayac = @sayac + 1
    set @fiyat1 = 0
    set @fiyat2 = 0
    set @fiyat3 = 0
END
SELECT * FROM @Urunlistesi
SET NOCOUNT OFF
END
GO

然后将此代码用于 excel;

DECLARE @return_value int

EXEC    @return_value = [dbo].[deneme]

SELECT  'Return Value' = @return_value

如果您建立 SQL 服务器连接 而不是使用 Microsoft,则可以从 Excel 运行 T-SQL询问。选择 数据 > 来自其他来源 > 来自 SQL 服务器

输入您的服务器名称并选择您的数据库。 Excel 将为您提供 table 的列表。 这令人困惑,因为它让你看起来好像不能使用 SQL 或 T-SQL,但你可以。继续选择 table。在下一页上设置您想要的任何设置,然后单击“完成”。

出现“导入数据”对话框后,单击属性,然后选择定义 选项卡。在 命令类型 下:将选择更改为 SQL。然后你可以在 Command Text 框中输入你喜欢的任何 SQL 或 T-SQL。您现在可以使用 T-SQL 因为连接字符串是 SQL OLEDB 而不是更通用的东西。

一旦您完成命令文本的输入,Excel 会给您一个错误提示,告诉您连接将不再与您最初设置的连接相同。只需单击“是”并从那里继续。