循环记录集以填充临时表中的列 [英] Loop through a recordset to populate columns in a temp table

查看:79
本文介绍了循环记录集以填充临时表中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从约会表中获取内容,并将任何指定月份的

约会插入临时表中,其中每天的所有

预约都是插入一行,每个月的每一天都有一列



有一种简单的方法吗?


我的记录集如下:


SELECT

a.Date,

a.Client --contents: Joe,Frank,Fred,Pete,Oscar

FROM

dbo.tblAppointments a

WHERE

a.date ...(选定月份的第一天和最后一天)


我想要做的是创建一个包含31列的临时表

到保持约会并在每一栏中插入任何约会

日期......

CREATE TABLE #Appointments(id int identity,Day1 nvarchar(500),第2天

nvarchar(500),Day3 nvarchar(500)等...)


然后遍历上面的记录集到插入第1天,第2天,第3天,

等当天的所有约会,多次约会

以逗号分隔。


INSERT INTO

#Appointments(Day1)

SELECT

a.Client

FROM

dbo.tblAppointments a

WHERE

a.date =(...当天的第一天)


(LOOP到第31天)

结果看起来像

Day1 Day2 Day3 ...

Row1 Joe,Pete
弗兰克,

弗雷德


也许还有更好的方法来应对这种情况?

谢谢,

lq

I want to take the contents from a table of appointments and insert the
appointments for any given month into a temp table where all the
appointments for each day are inserted into a single row with a column
for each day of the month.
Is there a simple way to do this?

I have a recordset that looks like:

SELECT
a.Date,
a.Client --contents: Joe, Frank, Fred, Pete, Oscar
FROM
dbo.tblAppointments a
WHERE
a.date between ...(first and last day of the selected month)

What I want to do is to create a temp table that has 31 columns
to hold appointments and insert into each column any appointments for
the date...

CREATE TABLE #Appointments (id int identity, Day1 nvarchar(500), Day2
nvarchar(500), Day3 nvarchar(500), etc...)

Then loop through the recordset above to insert into Day1, Day 2, Day3,
etc. all the appointments for that day, with multiple appointments
separated by a comma.

INSERT INTO
#Appointments(Day1)
SELECT
a.Client
FROM
dbo.tblAppointments a
WHERE
a.date = (...first day of the month)

(LOOP to Day31)
The results would look like
Day1 Day2 Day3 ...
Row1 Joe, Pete
Frank,
Fred

Maybe there''s an even better way to handle this sort of situation?
Thanks,
lq

推荐答案

您可以使用查询来执行此类报告而无需循环或临时表。

按照规定,水平列出日期似乎有点奇怪,因为

纵向轴上显然没有数据,但其余的只是

a问题一旦您有基本查询格式化。格式化是最好的

完成客户端而不是数据库。


DECLARE @dt DATETIME

/ *第一次约会这个月* /

SET @dt ='''20050501''


SELECT

MAX(当天的情况(天, @ dt,date)= 0那么客户端END),

MAX(当DATEDIFF(DAY,@ dt,日期)= 1那么客户端END),

MAX(例如DATEDIFF(DAY,@ dt,date)= 2那么客户端END),

...

MAX(DEDIFF时的情况(DAY,@ dt,date) = 30那么客户端END)

FROM tblAppointments

WHERE date> = @dt

AND date< DATEADD(月,1,@ dt)

GROUP BY客户


-

David Portas

SQL Server MVP

-
You can use a query to do reports like this without looping or temp tables.
As specified it seems a little strange to list the dates horizontally since
there is apparently no data on the vertical axis but the rest is really just
a matter of formatting once you have the basic query. Formatting is best
done client-side rather than in the database.

DECLARE @dt DATETIME
/* First date of the month */
SET @dt = ''20050501''

SELECT
MAX(CASE WHEN DATEDIFF(DAY,@dt,date)=0 THEN client END),
MAX(CASE WHEN DATEDIFF(DAY,@dt,date)=1 THEN client END),
MAX(CASE WHEN DATEDIFF(DAY,@dt,date)=2 THEN client END),
...
MAX(CASE WHEN DATEDIFF(DAY,@dt,date)=30 THEN client END)
FROM tblAppointments
WHERE date >= @dt
AND date < DATEADD(MONTH,1,@dt)
GROUP BY client

--
David Portas
SQL Server MVP
--


laurenq uantrell写道:
laurenq uantrell wrote:
我想从中获取内容约会表,并将任何给定月份的
约会插入临时表中,其中每天的所有
约会被插入一行,每行每天都有一列

有没有一种简单的方法可以做到这一点?

我有一个看起来像的记录集:

SELECT
a.Date,
a。客户 - 内容:乔,弗兰克,弗雷德,皮特,奥斯卡
来自
dbo.tbl任命a
在...之间的日期......(第一天和最后一天)选择的月份)

我想要做的是创建一个包含31列
的临时表来保存约会,并在每列中插入任何约会日期。 ..

CREATE TABLE #Appointments(id int identity, Day1 nvarchar(500),Day2
nvarchar(500),Day3 nvarchar(500)等......)然后循环上面的记录集插入第1天,第2天,第3天,
等当天的所有约会,多个约会用逗号分隔。

INSERT INTO
#Appointments(Day1)
SELECT
a.Client
来自
dbo.tblAppointments a
在哪里
a.date =(...这个月的第一天)

>(LOOP到第31天)

结果看起来像是Day1 Day2 Day3 ......
Row1 Joe,Pete
Frank,
Fred

也许有更好的方法来处理这种情况?
谢谢,
lq
I want to take the contents from a table of appointments and insert the
appointments for any given month into a temp table where all the
appointments for each day are inserted into a single row with a column
for each day of the month.
Is there a simple way to do this?

I have a recordset that looks like:

SELECT
a.Date,
a.Client --contents: Joe, Frank, Fred, Pete, Oscar
FROM
dbo.tblAppointments a
WHERE
a.date between ...(first and last day of the selected month)

What I want to do is to create a temp table that has 31 columns
to hold appointments and insert into each column any appointments for
the date...

CREATE TABLE #Appointments (id int identity, Day1 nvarchar(500), Day2
nvarchar(500), Day3 nvarchar(500), etc...)

Then loop through the recordset above to insert into Day1, Day 2, Day3,
etc. all the appointments for that day, with multiple appointments
separated by a comma.

INSERT INTO
#Appointments(Day1)
SELECT
a.Client
FROM
dbo.tblAppointments a
WHERE
a.date = (...first day of the month)

(LOOP to Day31)
The results would look like
Day1 Day2 Day3 ...
Row1 Joe, Pete
Frank,
Fred

Maybe there''s an even better way to handle this sort of situation?
Thanks,
lq




你''谈论交叉表查询。这是一个可以使用的链接页面:

http://www.google.com/custom?q=cross...D%3A1%3B&hl=en


-

MGFoster ::: mgf00< at> earthlink< decimal-point> net

奥克兰,加利福尼亚州(美国)



You''re talking about crosstab queries. Here''s a page of links that may
be of use:

http://www.google.com/custom?q=cross...D%3A1%3B&hl=en

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)


laurenq uantrell(la ************* @ hotmail。 com)写道:
laurenq uantrell (la*************@hotmail.com) writes:
我想从约会表中取出内容,并将任何给定月份的
约会插入到临时表中,其中每天的所有
约会每月的每一天都会插入一行带有一列

是否有一种简单的方法可以做到这一点?
...
然后循环上面的记录集插入第1天,第2天,第3天,
等当天的所有约会,多个约会用逗号分隔。
I want to take the contents from a table of appointments and insert the
appointments for any given month into a temp table where all the
appointments for each day are inserted into a single row with a column
for each day of the month.
Is there a simple way to do this?
...
Then loop through the recordset above to insert into Day1, Day 2, Day3,
etc. all the appointments for that day, with multiple appointments
separated by a comma.




我担心循环是你必须要做的。并写出31

UPDATE语句,每月一天一个。有一些方法可以使用基于集合的语句构建以逗号分隔的列表,但是使用的

方法不受支持且未定义,并且不可信任。

因此,构建CSV的唯一方法是运行游标。


好​​的,你真的不需要31个UPDATE语句。您可以将

数据汇总到一个表中,每个日期有一个,然后在最后运行一个

31-way自交叉连接以产生最终结果。 />

当然,客户端程序更容易做到这一点

的东西。


-

Erland Sommarskog,SQL Server MVP, es****@sommarskog.se


SQL Server SP3联机丛书
http://www.microsoft.com/sql/techinf...2000/books.asp


这篇关于循环记录集以填充临时表中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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