Excel如何使用Transact SQL从SQL Server导入数据

时间:2015-01-14 09:32:45

标签: sql-server excel tsql import

我想使用TSQL将SQL服务器数据导入Excel。在Excel中,我可以完美地使用SQL代码检索表。但是当我编写TSQL代码而不是SQL时,它给了我“没有什么可以回溯”。

Excel是否支持TSQL代码?如果是这样,我如何将数据从SQL服务器导入excel?

我有这个示例代码BTW。

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

我很可能只想将临时表UrunListesis返回给Excel。

4 个答案:

答案 0 :(得分:0)

替代方式

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

<?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参数创建一个过程,该参数将以最高格式格式化表和数据并返回输出变量。

答案 1 :(得分:0)

在excel(2010)中,我使用以下步骤以表格或数据透视表格式返回数据:

  • 打开Excel单击数据选项卡,选择&#34;来自其他来源&#34;选择 &#34;来自SQL Server&#34;

输入服务器信息

导入数据屏幕上的

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

选择所需的数据输出,然后单击“确定”。

答案 2 :(得分:0)

我解决了我的问题。如果要在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

答案 3 :(得分:0)

如果您使用 SQL Server连接而不是使用Microsoft Query,则可以从Excel运行T-SQL。选择数据&gt;来自其他来源&gt;来自SQL Server

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

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

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