Solved

Using cursor to populate table

Posted on 2006-10-26
5
304 Views
Last Modified: 2008-03-17
Hello,

I need to create a table variable to hold dates associated with serial numbers.  Here is what I have so far:

--Create table variable to hold list of serial numbers for Clark County or Parkrose users
DECLARE @serials TABLE
(
  RowID INT IDENTITY PRIMARY KEY NONCLUSTERED,
  Serial VARCHAR(5) NULL
)

--Insert values into variable
INSERT @serials
SELECT
Serial
FROM SERVER.Main.dbo.UserInfo
WHERE LEFT(Location, 3) IN('PAR', 'CLA')

UNION

SELECT
Serial
FROM SERVER.Archive.dbo.UserInfo
WHERE LEFT(Location, 3) IN('PAR', 'CLA')

--Create table variable to hold dates associated with serial numbers
DECLARE @dates TABLE
(
  RowID INT IDENTITY PRIMARY KEY NONCLUSTERED,
  EventDate DATETIME NOT NULL
)


DECLARE @count INT
SELECT @count = 1
WHILE @count < 5
BEGIN
      INSERT @dates
      SELECT CONVERT(DATETIME, '2006-' + CONVERT(CHAR(1), @count) + '-1')
      SELECT @count = @count + 1
END

SELECT *
FROM @dates


So far, the @serials variable holds a list of unique serial numbers and the @dates variable holds 4 dates like this:

RowID     EventDate
1            2006-01-01 00:00:00.000
2            2006-02-01 00:00:00.000
3            2006-03-01 00:00:00.000
4            2006-04-01 00:00:00.000


What I need to do is to join the two together to come up with something like this:

RowID     SerialNo     EventDate
1             2356         2006-01-01 00:00:00.000
2             2356         2006-02-01 00:00:00.000
3             2356         2006-03-01 00:00:00.000
4             2356         2006-04-01 00:00:00.000
5             2375         2006-01-01 00:00:00.000
6             2375         2006-02-01 00:00:00.000
7             2375         2006-03-01 00:00:00.000
8             2375         2006-04-01 00:00:00.000
etc ...

I think that I may need to use a cursor to accomplish this but I'm not sure.  Can anyone help me to figure this out?

Derrick
0
Comment
Question by:dnsvh
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:CIC Admin
ID: 17813444
I am not quite sure what purpose the @dates table is performing.  Since you create the date dynamically, it doesn't appear to be a real date.

You should be able to combine the two functions into one with something like :

DECLARE @serials TABLE
(
  RowID INT IDENTITY PRIMARY KEY NONCLUSTERED,
  Serial VARCHAR(5) NULL
  EventDate DATETIME NOT NULL
)

Then remove the @Dates table declaration and use the @Serials in your loop instead.

DECLARE @count INT
SELECT @count = 1
WHILE @count < 5
BEGIN
     INSERT @serial
     SELECT CONVERT(DATETIME, '2006-' + CONVERT(CHAR(1), @count) + '-1')
     SELECT @count = @count + 1
END

Unless I am missing something, the @serial table would then match what you want your final result to be.

Good luck,
-kent-
0
 
LVL 4

Author Comment

by:dnsvh
ID: 17813503
You are missing the serial numbers.  The insert statement you have only inserts the dates.  I need to match the dates to the list of serial numbers.
0
 
LVL 4

Author Comment

by:dnsvh
ID: 17813518
You are correct, by the way, that I can eliminate the @dates table variable once I figure out how to combine the two.
0
 
LVL 5

Accepted Solution

by:
CIC Admin earned 500 total points
ID: 17813626
Yep, I jumped the gun on that a bit.  We'll go back to adding the @dates table, but add in a Serial field.  

Try the following modifications :

--Create table variable to hold dates associated with serial numbers
DECLARE @dates TABLE
(
  RowID INT IDENTITY PRIMARY KEY NONCLUSTERED,
  Serial VARCHAR(5) NULL
  EventDate DATETIME NOT NULL
)

DECLARE @count INT
SELECT @count = 1
WHILE @count < 5
BEGIN
     INSERT @dates (Serial, EventDate) VALUES SELECT Serial, CONVERT(DATETIME, '2006-' + CONVERT(CHAR(1), @count) + '-1') FROM @Serial
     SELECT @count = @count + 1
END

0
 
LVL 4

Author Comment

by:dnsvh
ID: 17813815
That worked.  Thank you!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now