SQL Server存储过程,从多个表中选择行

时间:2021-09-14 16:40:53

I have a stored procedure that is pulling data from one table, now I have got the same data collected in a different table, as the IDs are different I cannot put all data from the second table into the first one.

我有一个从一个表中提取数据的存储过程,现在我在不同的表中收集了相同的数据,因为ID不同我不能将第二个表中的所有数据放入第一个表中。

So now I have to have two select statements in one stored procedure.

所以现在我必须在一个存储过程中有两个select语句。

Although the corresponding data is same but the column names in both table are different.

虽然相应的数据相同但两个表中的列名不同。

For instance BRIEF_TITLE would be briefTitle in the second table.

例如,BRIEF_TITLE将是第二个表中的briefTitle。

How can I merge the data from two different tables into one?

如何将两个不同表中的数据合并为一个?

The result is bonded to ASP.net grid view control.

结果绑定到ASP.net网格视图控件。

1 个解决方案

#1


3  

As the comments above state, you need something like this:

正如上面的评论所述,你需要这样的东西:

select BRIEF_TITLE from t1
union all
select briefTitle from t2

This will give you a column name of BRIEF_TITLE, but if you want something else, then add an alias to the first select

这将为您提供一个名称为BRIEF_TITLE,但如果您想要其他内容,则为第一个选择添加别名

select BRIEF_TITLE as ShortTitle from t1
union all
select briefTitle from t2

#1


3  

As the comments above state, you need something like this:

正如上面的评论所述,你需要这样的东西:

select BRIEF_TITLE from t1
union all
select briefTitle from t2

This will give you a column name of BRIEF_TITLE, but if you want something else, then add an alias to the first select

这将为您提供一个名称为BRIEF_TITLE,但如果您想要其他内容,则为第一个选择添加别名

select BRIEF_TITLE as ShortTitle from t1
union all
select briefTitle from t2