[英]How to let SQL Server not return null query?
I have a sql query like this: 我有一个这样的SQL查询:
if table1 exist
if table1 has data
select * from table1
if table2 exist
if table2 has data
select * from table2
...
tableN
then I iterate dataset in c# but even a table does not exist in database, there is an empty result table in dataset, How to write the sql to only return not empty queries? 然后我在C#中迭代数据集,但是数据库中甚至没有一个表,数据集中有一个空结果表,如何编写sql只返回不为空的查询? say I have 10 tables, 3 not exist in database, So, only 7 result tables in returned dataset? 说我有10个表,数据库中不存在3个表,那么返回的数据集中只有7个结果表?
I meant "there is an empty table with no cols and rows in returned dataset" 我的意思是“返回的数据集中有一个没有cols和行的空表”
ahh... so you want the missing table to have a stub in your DataSet
? 嗯...所以您希望丢失的表在DataSet
有一个存根? Seems pretty... oddd - but I guess you could do something like: 似乎很...奇怪-但我想您可以做类似的事情:
if object_id('table2') is not null -- yeuch yeuch yeuch (see below)
begin
select * from table2
end
else
begin
select 1 where 1 = 0
end
Again, I stress that I find this desire... unusual; 我再次强调,我发现这种愿望……与众不同; but: 但:
select
even if there isn't data, we just get an empty table, which is fine 如果我们select
即使没有数据,我们也会得到一个空表,这很好 There are better ways of checking for existance of an object - for example, checking the info-schema tables; 有更好的检查对象是否存在的方法-例如,检查信息模式表; but I'm still not convinced that it is (in the general case) sensible to be overly concerned about existence / non-existence of tables, unless you're: 但我仍然不相信过度考虑表的存在/不存在(在一般情况下)是明智的 ,除非您是:
You can easily extract details about each table available in your Database: 您可以轻松提取有关数据库中每个可用表的详细信息:
use MyDBName
go
select * from sys.tables
go
Using a cursor (though I personally avoid them if at all possible), you could loop through and build your queries dynamically to use only the tables available, meaning you won't return any NULLs. 使用游标(尽管我个人尽可能避免使用它们),您可以循环遍历并动态构建查询以仅使用可用表,这意味着您将不返回任何NULL。
不知道是什么类型的数据库就很难猜测,尤其是所涉及的实体和关系。正如前面所说,通常不会事先检查表是否存在-在我看来,这是一个问题坚持...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.