Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need help writng a do while loop in 2008 Sql server


Need help writng a do while loop in 2008 Sql server

Author
Message
thejordans21
thejordans21
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 8
Hello I need some help writing a loop in 2008 Sql server. I am just begining to use this and need some help;

Here is my data:
PLANT PLANT_DESC VENDOR VENDOR_NAME
NOPE NULL NOPE NOPE
7010 Coquitlam DC PR1 -100
NOPE NULL NOPE NOPE
NOPE NULL NOPE NOPE
NOPE NULL NOPE NOPE
NOPE NULL NOPE NOPE
NOPE NULL 1014435 COCA-COLA NORTH AMERI
NOPE NULL 1014435 COCA-COLA NORTH AMERI
NOPE NULL 1014435 COCA-COLA NORTH AMERI


I need a loop to pouplate the PLANT and PLANT_DESC with 7010 and Coquitlam repesctly where the vendor isn't NOPE. Any help would be greatly appreicated

Thanks
Shawn Melton
Shawn Melton
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1099 Visits: 3497
Why do you need to do this using a loop? What have you tried so far?

Shawn Melton
PS C:\>(Find-Me).TwitterURL
@wsmelton
PS C:\>(Find-Me).BlogURL
meltondba.wordpress.com
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14975 Visits: 38999
the beauty of SQL server is set based operations.
that means you affect all records, without a loop, as long as they meet a specific criteria.



IF OBJECT_ID('tempdb.[dbo].[#MyData]') IS NOT NULL
DROP TABLE [dbo].[#MyData]
GO
CREATE TABLE [dbo].[#MyData] (
[PLANT] VARCHAR(30) NULL,
[PLANT_DESC] VARCHAR(30) NULL,
[VENDOR] VARCHAR(30) NULL,
[VENDOR_NAME] VARCHAR(30) NULL)

--my initial data
INSERT INTO #MyData
SELECT 'NOPE',NULL,'NOPE','NOPE' UNION ALL
SELECT '7010',NULL,'Coquitlam DC','PR1 -100' UNION ALL
SELECT 'NOPE',NULL,'NOPE','NOPE' UNION ALL
SELECT 'NOPE',NULL,'NOPE','NOPE' UNION ALL
SELECT 'NOPE',NULL,'NOPE','NOPE' UNION ALL
SELECT 'NOPE',NULL,'NOPE','NOPE' UNION ALL
SELECT 'NOPE',NULL,'1014435','COCA-COLA NORTH AMERI' UNION ALL
SELECT 'NOPE',NULL,'1014435','COCA-COLA NORTH AMERI' UNION ALL
SELECT 'NOPE',NULL,'1014435','COCA-COLA NORTH AMERI'

SELECT * FROM #MyData


UPDATE #MyData
SET PLANT = '7010',
PLANT_DESC='Coquitlam'
WHERE VENDOR <> 'NOPE';

SELECT * FROM #MyData




Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

thejordans21
thejordans21
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 8
There is more data that that but put that just for a start.. I need to populate all the rows with the plant and plant desrc. I wanted to use a loop becuase there is alot more data
LutzM
LutzM
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 7011 Visits: 13559
thejordans21 (7/28/2014)
There is more data that that but put that just for a start.. I need to populate all the rows with the plant and plant desrc. I wanted to use a loop becuase there is alot more data

Especially if there are more data a set based solution should be used.
Except it doesn't matter at all how long it takes to return the results...



Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
thejordans21
thejordans21
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 8
I don;t know how to set that up. Iout the plant and plant desc into another temp table and I am trying to use that to popllaute the orginal temp table
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14975 Visits: 38999
thejordans21 (7/28/2014)
I don;t know how to set that up. Iout the plant and plant desc into another temp table and I am trying to use that to popllaute the orginal temp table


the temp table was so that you could actualyl see a set based example.
i kind of know your column names, assuming what you posted was correct.

the example i posted would work whether there was a million rows or just one.

for example, if you run this command, changing the YourTableName to your Real Table, and you look at the data, would that match the rows you want to affect?


SELECT * FROM YourTableName WHERE VENDOR <> 'NOPE';



a set base doperation would modify the same rows that command returns in a single command...
that's the mental shift from programming a process (row-by-agaonizing row) to a set based operation: instead of updating a million things one at a time, you use a set based operation once to do a million updates.

Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search