简体   繁体   English

从一个表或另一个表中选择数据的最佳方法

[英]best way to select data from one table or another

I have two temporary tables, say #t1 and #t2 in Sql Server 2008. I need to create #t3 such as: 我有两个临时表,比如Sql Server 2008中的#t1#t2 。我需要创建#t3例如:

  • when #t1 has rows, indifferently of #t2 's contents, #t3 = select * from #t1 #t1有行时,与#t2的内容无关, #t3 = select * from #t1
  • when #t1 has no rows, #t3 = select * from #t2 #t1没有行时, #t3 = select * from #t2

we can assume #t1 and #t2 have the same columns but I don't think I would like to rely on that fact. 我们可以假设#t1#t2具有相同的列,但我认为我不想依赖这个事实。

I was thinking of something that draws some logic out of ' if exists (select * ...) ' statements, but wouldn't there be better like some sort of bool operators ? 我正在考虑从' if exists (select * ...) '语句中得出一些逻辑,但不会像某种bool运算符那样更好吗?

The simplest way is to implement the logic as: 最简单的方法是将逻辑实现为:

if (exists (select * from #t1))
begin
    select *
    into #t3
    from #t1;
end;
else
begin
    select *
    into #t3
    from #t2;
end;

You can do this in one statement as: 您可以在一个声明中执行此操作:

select t.*
into #t3
 from ((select *
        from #t1
       )
       union all
       (select *
        from #t2
        where not exists(select * from #t1)
       )
      ) t

However, I think the explicit if is a clearer way to express your intent. 但是,我认为明确的if是一种更清晰的方式来表达你的意图。

This query would do just that, would see if there are rows in t1 and in that case populate table t3 with the contents of t1 , provided they have the same columns, otherwise it populates t3 with the contents of t2 . 这个查询就是这样,会看到t1是否有行,并且在这种情况下用t1的内容填充表t3 ,前提是它们具有相同的列,否则它用t2的内容填充t3

IF(select count(*) from t1) > 0 
    BEGIN 
        select * into t3 from t1
    END
ELSE
    BEGIN
        select * into t3 from t2
    END

Here is a SQLFiddle so you can see how it works. 这是一个SQLFiddle,所以你可以看到它是如何工作的。 To test the case when t1 does not have any rows, just remove the line that inserts into t1 , rebuild schema and re-run the query. 要测试t1没有任何行的情况,只需删除插入t1的行,重建模式并重新运行查询。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数据从一个表移动到另一个 SQL 服务器的最佳方法 - Best way to move data from one table to another SQL Server 从一个表返回数据以及从另一个表映射数据的SQL最佳方法 - SQL Best way to return data from one table along with mapped data from another table 每天将数据从一个数据库中的源表传输到另一个数据库中的目标表的最佳方法 - Best way to transfer data from source table in one db to destination table in another db daily 从一个sql表中选择数据并查看其是否与同一数据库中另一表上的数据匹配的最有效方法 - Most efficient way to select data from one sql table and see if it matches data on another table in the same database 将 SELECT 数据从一个表复制到另一个表 - Copy SELECT data from one table to another 在SQL中从另一个表更新一个表的最佳方法是什么? - What is the best way to update a one table from another in SQL? 从表中选择数据的最佳方法是什么 - What's ths best way to select data from table 从一个表中获取相交数据的最佳方法是什么? - What's the best way to get intersected data from one table? 将数据从一个表复制到另一表的最简单方法? - Easiest way to copy data from one table to another table? SQL:从表中选择仅在具有特定ID的另一个表中不存在的行的最佳方法是什么? - SQL: What is the best way to select rows from a table that do not exist in another table only with a particular id?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM