执行SQLCommand(Select)查询后命名DataSet.table

发布于 2024-11-02 19:07:16 字数 226 浏览 1 评论 0原文

在存储过程 MS SQL 中我的查询是:

SELECT *  
FROM ContentReportRequests a,UserPreferences d
WHERE  a.UserID = d.UserID and a.ID =@ID

我想给结果表一些名称。 我该怎么做?

我想将其拉到 ADO.Net DataSet.tables["NAME"]

In stored procedure MS SQL My query is:

SELECT *  
FROM ContentReportRequests a,UserPreferences d
WHERE  a.UserID = d.UserID and a.ID =@ID

I want to give the result table some name.
How can I do this ?

I want to pull it to ADO.Net DataSet.tables["NAME"]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

故乡的云 2024-11-09 19:07:16

我可以想象你可能想表达的一些意思。

如果您想保留此结果集,以便在以后的多个查询中使用,您可能需要查找 SELECT INTO:

SELECT * into NewTableName
FROM ContentReportRequests a,UserPreferences d
WHERE  a.UserID = d.UserID and a.ID =@ID

其中 NewTableName 是新名称,并且将创建一个新的(永久)表。如果您希望该表在完成后消失,请在名称前添加 # 前缀,使其成为临时表。

或者,您可能只是想将其吸收到一个更大的查询中,在这种情况下,您将考虑将其设为子选择:

SELECT *
FROM (SELECT *  
FROM ContentReportRequests a,UserPreferences d
WHERE  a.UserID = d.UserID and a.ID =@ID
) NewTableName
WHERE NewTableName.ColumnValue = 'abc'

或 CTE:

WITH NewTableName AS (
    SELECT *  
    FROM ContentReportRequests a,UserPreferences d
    WHERE  a.UserID = d.UserID and a.ID =@ID
)
SELECT * from NewTableName

最后,您可能会讨论将结果集拉入例如 ADO 中。 Net DataTable,并且您希望自动设置名称。我不确定这是否可行。

I can imagine a few things you might be meaning.

If you want to persist this result set, for consumption in multiple later queries, you might be looking for SELECT INTO:

SELECT * into NewTableName
FROM ContentReportRequests a,UserPreferences d
WHERE  a.UserID = d.UserID and a.ID =@ID

Where NewTableName is a new name, and a new (permanent) table will be created. If you want that table to go away when you're finished, prefix the name with a #, to make it a temp table.

Alternatively, you might just be wanting to absorb it into a single larger query, in which case you'd be looking at making it a subselect:

SELECT *
FROM (SELECT *  
FROM ContentReportRequests a,UserPreferences d
WHERE  a.UserID = d.UserID and a.ID =@ID
) NewTableName
WHERE NewTableName.ColumnValue = 'abc'

or a CTE:

WITH NewTableName AS (
    SELECT *  
    FROM ContentReportRequests a,UserPreferences d
    WHERE  a.UserID = d.UserID and a.ID =@ID
)
SELECT * from NewTableName

Finally, you might be talking about pulling the result set into e.g. an ADO.Net DataTable, and you want the name to be set automatically. I'm not sure that that is feasible.

祁梦 2024-11-09 19:07:16

您可以使用table类型的变量。在此处阅读更多信息:T-SQL 中的表变量

You can use a variable of type table. Read more here: Table Variables In T-SQL

阪姬 2024-11-09 19:07:16

在存储过程中:

select  CH.PrimaryKey, CH.Name,
        NULL    "CustomerHeader"
from CustomerHeader "CH";
--
select  CD.PrimaryKey, CD.ShipTo,
        NULL    "CustomerDetail"
from CustomerDetail "CD";
--
select  *, NULL "Orders"
from    OrderTable;

在 Vb.Net 代码中:

Dim ds As DataSet = Nothing
ds = SqlExecute();
Dim dtCustHeader As DataTable = Nothing
Dim dtCustDetail As DataTable = Nothing
Dim dtOrders As DataTable = Nothing
For Each dt As DataTable In ds.tables
    Select Case True
        Case dt.Columns.Contains("CustomerHeader")
            dtCustHeader = dt
        Case dt.Columns.Contains("CustomerDetail")
            dtCustDetail = dt
        Case dt.Columns.Contains("Orders")
            dtOrders = dt
    End Select
Next

有点愚蠢(或愚蠢),您无法命名结果集中的表。
但这可以让您在没有大量字节数的情况下在每行中重复表名。

为每行传回 NULL 值仍然存在开销。也许传递 BIT 值会更小......

另一种方法是始终使用列(0):
在 SQL 中:

select NULL "CustomerDetail", CustName,Addr1,Addr2... from CustomerDetail;

在 vb.net 中:

    Dim ds As DataSet = Nothing
    ds = SqlExecute();
    Dim dtCustHeader As DataTable = Nothing
    Dim dtCustDetail As DataTable = Nothing
    Dim dtOrders As DataTable = Nothing
    For Each dt As DataTable In ds.Tables
        Dim tblName As String = dt.Columns(0).ColumnName
        Select Case tblName.ToUpper
            Case "CUSTOMERDETAIL" : dtCustHeader = dt
            Case "CUSTOMERDETAIL" : dtCustDetail = dt
            Case "ORDERS" : dtOrders = dt
        End Select
    Next

即使查询返回零行,这些方法也会获取表名。

但是最好的最后...一种每次从 SQL 存储过程中自动实际命名数据集中的表的方法(在代码的帮助下):

Dim ds As DataSet = Nothing
ds = SqlExecute();
For Each dt As DataTable In ds.Tables
    dt.TableName = dt.Columns(0).ColumnName
Next

在此之后,您可以使用以下命令访问您的表 :您在存储过程中控制的名称...从第一天起就应该如此!

编辑:选择性实施:
将模式中的第一列命名为“TN:Customer”。
您的旧存储过程正常工作,只会影响您想要修改的存储过程。

            For Each dt As DataTable In mo_LastDataset.Tables
                Dim tblName() As String = dt.Columns(0).ColumnName.Split(":")
                If tblName.Length >= 2 AndAlso tblName(0).ToUpper = "TN" Then
                    dt.TableName = tblName(1)
                End If
            Next

...大卫...

in stored procedure:

select  CH.PrimaryKey, CH.Name,
        NULL    "CustomerHeader"
from CustomerHeader "CH";
--
select  CD.PrimaryKey, CD.ShipTo,
        NULL    "CustomerDetail"
from CustomerDetail "CD";
--
select  *, NULL "Orders"
from    OrderTable;

in Vb.Net code:

Dim ds As DataSet = Nothing
ds = SqlExecute();
Dim dtCustHeader As DataTable = Nothing
Dim dtCustDetail As DataTable = Nothing
Dim dtOrders As DataTable = Nothing
For Each dt As DataTable In ds.tables
    Select Case True
        Case dt.Columns.Contains("CustomerHeader")
            dtCustHeader = dt
        Case dt.Columns.Contains("CustomerDetail")
            dtCustDetail = dt
        Case dt.Columns.Contains("Orders")
            dtOrders = dt
    End Select
Next

Kinda SILLY (OR STUPID) that you cannot name tables in a result set.
But this gets you there without a HUGE byte count repeating the table name within each row.

There is still overhead passing the NULL value back for each row. Perhaps passing a BIT value would be smaller yet...

And an alternative is to always use column(0):
in SQL:

select NULL "CustomerDetail", CustName,Addr1,Addr2... from CustomerDetail;

in vb.net:

    Dim ds As DataSet = Nothing
    ds = SqlExecute();
    Dim dtCustHeader As DataTable = Nothing
    Dim dtCustDetail As DataTable = Nothing
    Dim dtOrders As DataTable = Nothing
    For Each dt As DataTable In ds.Tables
        Dim tblName As String = dt.Columns(0).ColumnName
        Select Case tblName.ToUpper
            Case "CUSTOMERDETAIL" : dtCustHeader = dt
            Case "CUSTOMERDETAIL" : dtCustDetail = dt
            Case "ORDERS" : dtOrders = dt
        End Select
    Next

These methods get your table-names even if the query returns zero rows.

but the best for last... a way to actually name the tables in the dataset automatically, every time FROM SQL STORED PROCEDURE (with help from your code):

Dim ds As DataSet = Nothing
ds = SqlExecute();
For Each dt As DataTable In ds.Tables
    dt.TableName = dt.Columns(0).ColumnName
Next

After this, you may access your tables with the name YOU control within the stored procedure... as it should have been from day-one!

EDIT: selective implementation:
Name the first column in the pattern "TN:Customer".
Your legacy stored procedures work normally, only impacting the stored procedures you wish to modify.

            For Each dt As DataTable In mo_LastDataset.Tables
                Dim tblName() As String = dt.Columns(0).ColumnName.Split(":")
                If tblName.Length >= 2 AndAlso tblName(0).ToUpper = "TN" Then
                    dt.TableName = tblName(1)
                End If
            Next

... david ...

恬淡成诗 2024-11-09 19:07:16
SELECT * AS MyTableName  
  FROM ContentReportRequests a, UserPreferences d  
 WHERE a.UserID = d.UserID and a.ID =@ID  
SELECT * AS MyTableName  
  FROM ContentReportRequests a, UserPreferences d  
 WHERE a.UserID = d.UserID and a.ID =@ID  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文