如何使用Cursor for Count Months并插入表n [英] How To use Cursor For Count Months and insert into table n

查看:74
本文介绍了如何使用Cursor for Count Months并插入表n的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SELECT @ Date = COUNT(Date),@ Date = COUNT(Date)FROM TableName WHERE Date = @Date



SELECT COUNT(Date)AS Modified,年(日期)年,月(日)月

来自TableName

GROUP BY年(日期),MONTH(日期)按月划分

解决方案

查看来自@PIEBALDconsult的评论 - 不要使用光标来执行此操作。



如果要创建新表从您的查询然后只需使用

 SELECT COUNT([Date])AS Modified,Year([Date])[Year],MONTH([Date])[Month] 
INTO NewTable
来自TableName
GROUP BY年([日期]),MONTH([日期])



如果 NewTable 已经存在,那么就这样做而不是

   INSERT   INTO  NewTable  
SELECT COUNT([日期]) AS 已修改,年份([日期])[年],MONTH([日期])[月]
FROM TableName
GROUP BY 年份([< span class =code-keyword> Date ]),MONTH([ Date ])



也不是方括号 [] 围绕列名日期 - 这是因为这些单词是SQL中的保留字 - 如果你真的想不出其他什么来调用列,你必须向SQL指出你的意思是列名而不是保留字。


SELECT @Date= COUNT(Date),@Date= COUNT(Date) FROM TableName WHERE Date= @Date

SELECT COUNT(Date) AS Modified, Year(Date) Year,MONTH(Date) Month
FROM TableName
GROUP BY Year(Date),MONTH(Date) ORDER BY MONTH

解决方案

See the comment from @PIEBALDconsult - do not use a cursor to do this.

If you want to create a new table from your query then simply use

SELECT COUNT([Date]) AS Modified, Year([Date]) [Year],MONTH([Date]) [Month]
INTO NewTable
FROM TableName
GROUP BY Year([Date]),MONTH([Date])


If NewTable already exists then do it this way instead

INSERT INTO NewTable
SELECT COUNT([Date]) AS Modified, Year([Date]) [Year],MONTH([Date]) [Month]
FROM TableName
GROUP BY Year([Date]),MONTH([Date])


Also not the square brackets [ ] around the column names Date and Month - this is because those words are reserved words in SQL - if you really cannot think of anything else to call the column you must indicate to SQL that you really mean the column name and not the reserved word.


这篇关于如何使用Cursor for Count Months并插入表n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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