Solved

Using cursor to populate table

Posted on 2006-10-26
5
309 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I have a large data set and a SSIS package. How can I load this file in multi threading?
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…

792 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