Solved

Using cursor to populate table

Posted on 2006-10-26
5
313 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

617 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