English 中文(简体)
页: 1 从日期范围选择每月姓名的表格
原标题:SQL Selecting tables with month names from date range

我的表格名称如下:

Posts_August_2011
Posts_September_2011
Posts_October_2011
Posts_November_2011
Posts_December_2011
Posts_January_2012

我想建立存储程序,以便<联合国词汇表><联合国编码>>表>,但这不是问题,因为我可以使用动态的电子数据集加以总结,而我的问题是将月和年限从区域信息网到某种清单? 之后,如果表格存在,将表格整理起来,然后核对。

页: 1

具体范围每个月抽取每月名称和年份的最佳途径是什么。

Say I give it a range @FromDate = 2011-10-18 14:16:27.000 and @ToDate = 2012-01-09 14:16:27.000 can someone advise how to extract Month and year.

DECLARE @SqlToExecute as nvarchar(MAX);

using dinamyc SQL is acceptable,It could check for existence of table in while loop and glue to string for each table that exists in range as:

@SqlToExecute +=  SELECT * FROM Posts_  + extractedMonthName +  _  + extractedYear +   UNION ALL  ;
最佳回答

造成这一问题的原因是数据库设计不当。 您需要把所有表格的浏览量放在一个表Posts上,有date的栏目,以指明月份和年份。

查阅表格<代码> 序号_%和从名称中摘取的日期:可使用以下代码:

create table Posts_August_2011(i int)
GO
create table Posts_September_2011(i int)
GO

DECLARE @SqlToExecute varchar(max)
DECLARE @FromDate date =  20110801 , @ToDate date =  20110809 

SELECT  name PostName, CAST(REPLACE(REPLACE(name, Posts_ , 01/ ), _ , / ) as date) PostDate
INTO #Posts
FROM sys.tables
WHERE name like  Posts_% 

SET @SqlToExecute=  

SELECT @SqlToExecute +=  SELECT * FROM   + PostName +   UNION ALL  
FROM #Posts
WHERE PostDate>=@FromDate
      AND PostDate<@ToDate

SELECT SUBSTRING(@SqlToExecute,1, LEN(@SqlToExecute)-10)

DROP TABLE #Posts
DROP TABLE Posts_August_2011
DROP TABLE Posts_September_2011

sys. tables is system catalog view which returns a row for each table object in the current database.

问题回答

您可使用DATENAME(月、日)和YEAR(日期)来建立您的月份和年份。

最容易的是,建立一个小盘,将年/月的组合和过滤你的日期范围,然后看看结果,建立你的询问和执行。

以后,如果表格的大小使得你把表格分开,你就可以将表格分为每月的分部分。

Is it possible to put these in a single [Post] table with post date? And partition them if you need to?





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签