执行SQLCommand(Select)查询后命名DataSet.table
在存储过程 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我可以想象你可能想表达的一些意思。
如果您想保留此结果集,以便在以后的多个查询中使用,您可能需要查找 SELECT INTO:
其中
NewTableName
是新名称,并且将创建一个新的(永久)表。如果您希望该表在完成后消失,请在名称前添加#
前缀,使其成为临时表。或者,您可能只是想将其吸收到一个更大的查询中,在这种情况下,您将考虑将其设为子选择:
或 CTE:
最后,您可能会讨论将结果集拉入例如 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:
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:
or a CTE:
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.
您可以使用
table
类型的变量。在此处阅读更多信息:T-SQL 中的表变量You can use a variable of type
table
. Read more here: Table Variables In T-SQL在存储过程中:
在 Vb.Net 代码中:
有点愚蠢(或愚蠢),您无法命名结果集中的表。
但这可以让您在没有大量字节数的情况下在每行中重复表名。
为每行传回 NULL 值仍然存在开销。也许传递 BIT 值会更小......
另一种方法是始终使用列(0):
在 SQL 中:
在 vb.net 中:
即使查询返回零行,这些方法也会获取表名。
但是最好的最后...一种每次从 SQL 存储过程中自动实际命名数据集中的表的方法(在代码的帮助下):
在此之后,您可以使用以下命令访问您的表 :您在存储过程中控制的名称...从第一天起就应该如此!
编辑:选择性实施:
将模式中的第一列命名为“TN:Customer”。
您的旧存储过程正常工作,只会影响您想要修改的存储过程。
...大卫...
in stored procedure:
in Vb.Net code:
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:
in vb.net:
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):
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.
... david ...