SQL Resultset使用*运算符返回表名,而不是在指定列时返回 [英] SQL Resultset returns a table name with * operator and not when specifying columns

查看:61
本文介绍了SQL Resultset使用*运算符返回表名,而不是在指定列时返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储过程,可以在#table中选择某些列。然后我做

  SELECT  *  FROM  #table 

为了检索我的结果。



我的问题是当我使用

  SELECT  *  INTO  #table 

它返回#table名称和结果,但是当我

  SELECT  DoodleName  INTO  #table 

它返回一个空白表名。



我想在我的代码中使用表名。



这是我的C#代码:

 DataTable dataTable =  new  DataTable(); 
使用(SqlCommand command = new SqlCommand( doodle,connection))
{
command.CommandType = CommandType.StoredProcedure;
尝试
{
if (connection.State == ConnectionState .Closed)
openConnectionString(connection);

SqlDataReader reader = command.ExecuteReader();
dataTable.Load(读者);
reader.Close();
}
catch (例外情况)
{
userConnectionErrorMessage = 错误检索结果。 + ex.Message;
}
最后
{
if (连接.State == ConnectionState.Open)
connection.Close();
}





这是返回#table名称的存储过程:



 选择 
*
进入 [#table]
来自 tblDoodle d
RIGHT OUTER JOIN tblRandom r ON d.DoodleID = d。 DoodleID
其中 r.RandomID 喜欢 1
d.Active 喜欢 0 ;

选择 * 来自 #table;





这里是不返回#table名称的存储过程:



 选择 
d.DoodleName
进入 [#table]
来自 tblDoodle d
RIGHT OUTER JOIN tblRandom r ON d.DoodleID = d.DoodleID
其中 r.RandomID 喜欢 1
< span class =code-keyword>和 d.Active like 0 ;

选择 * 来自 #table;





我们非常感谢任何帮助。

解决方案

问题在于此声明:

  SELECT  *  INTO  #table 





MSDN写道:

SELECT ... INTO创建一个新表





看看这里: INTO (T-SQL) [ ^ ]



顺便说一句:如果你想将数据从一个表插入到第二个表中,请使用如下命令:

< pre lang =SQL> INSERT INTO DestinationTableName(Field1,Field2, ...)
SELECT Field1,Field2,...
FROM SourceTableName


I have a stored procedure that selects certain columns into a #table. I then do a

SELECT * FROM #table

in order to retrieve my results.

My problem is that when I use

SELECT * INTO #table

it returns the #table name and the results, but when I

SELECT DoodleName INTO #table

it returns a blank table name.

I want to use the table name in my code.

This is my C# code:

DataTable dataTable = new DataTable();
using (SqlCommand command = new SqlCommand("doodle", connection))
{
   command.CommandType = CommandType.StoredProcedure;
   try
   {
      if (connection.State == ConnectionState.Closed)
            openConnectionString(connection);

      SqlDataReader reader = command.ExecuteReader();
      dataTable.Load(reader);
      reader.Close();
   }
   catch (Exception ex)
   {
      userConnectionErrorMessage = "Error Retrieving results. " + ex.Message;
   }
   finally
   {
      if (connection.State == ConnectionState.Open)
           connection.Close();
   }



Here is the stored procedure that returns the #table name:

select
   *
   into [#table]
from tblDoodle d
  RIGHT OUTER JOIN tblRandom r ON d.DoodleID = d.DoodleID
where r.RandomID like 1
  and d.Active like 0;

select * from #table;



and here is the stored procedure that does not return the #table name:

select
  d.DoodleName
  into [#table]
from tblDoodle d
  RIGHT OUTER JOIN tblRandom r ON d.DoodleID = d.DoodleID
where r.RandomID like 1
  and d.Active like 0;

select * from #table;



Any help would be greatly appreciated.

解决方案

The problem is in this statement:

SELECT * INTO #table



MSDN wrote:

SELECT…INTO creates a new table



Have a look here: INTO (T-SQL)[^]

By The Way: If you want to insert data from one table to second one, use command like that:

INSERT INTO DestinationTableName (Field1, Field2, ...)
SELECT Field1, Field2, ...
FROM SourceTableName


这篇关于SQL Resultset使用*运算符返回表名,而不是在指定列时返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆