IT Community - Software Programming, Web Development and Technical Support

Sql loop Statement

This is a discussion on Sql loop Statement within the Database Support forums, part of the Web Development category; Hi, How to fetch row by row in sql server without using cursor...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > Database Support

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 04-25-2008, 02:30 AM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Sql Loop Statement

Hi,
How to fetch row by row in sql server without using cursor
__________________
Shaalini.S
Be the Best of Whatever you are...

Last edited by shaalini : 05-06-2008 at 10:08 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-26-2008, 12:25 AM
poornima poornima is offline
D-Web Sr.Programmer
 
Join Date: Dec 2007
Posts: 189
poornima is on a distinguished road
Smile Re: Sql loop Statement

Hi Shaalini,
Could u explain ur need in detail?
How u want the records to be displayed explain with sample eg?

Last edited by poornima : 04-26-2008 at 12:28 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-26-2008, 12:59 AM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Sql loop Statement

Hi poornima
for ex: select * from users it return multiple rows.
I have to get each row user id and perform some insert and update function
Just like for loop function.
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-02-2008, 10:44 PM
Falcon Falcon is offline
D-Web Analyst
 
Join Date: Nov 2007
Location: Chennai
Posts: 288
Falcon is on a distinguished road
Default Re: Sql loop Statement

Hi

We can archive the Loop statement in SQL. Here is some Example
For SQL Server
Code:
DECLARE @CNT INT
DECLARE @Q NVARCHAR(1000)

SET @CNT = 100
WHILE(@CNT < 1000)
BEGIN
    SET @Q = 'Insert into infostorageCompanies
    Values(' + CAST (@CNT AS NVARCHAR) + ',' + '''testCompany_9''' + ',' + '''''' + ',' + '''''' + ')'
    PRINT @Q
    EXEC sp_executesql @Q
    SET @CNT = @CNT + 100
END
For MS SQL
Code:
declare @i int
while (@i<10)
begin
insert into dbname.dbo.table_name values @i
@i=@i+1
end
For MySql
Code:
declare @i int
while (@i<10)
begin
insert into dbname.dbo.table_name values @i
@i=@i+1
end
Regards
Falcon
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-04-2008, 11:04 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Sql loop Statement

Hi Falcon,
I have my table structure like this
Rec
RecId Name Description
1 Chicken chicken
2 Pizza Pizza

and other table as RecItem

RecItemId ReciId ItemId
1 1 23
2 1 24
3 1 26
4 2 43
5 2 52
I have to clone both these rec and recitem and i have to get result as follow
Rec
RecId Name Description
1 Chicken chicken
2 Pizza Pizza
3 chicken chicken
4 pizza pizza


RecItem

RecItemId ReciId ItemId
1 1 23
2 1 24
3 1 26
4 2 43
5 2 52
6 3 23
7 3 24
8 3 26
9 4 43
10 4 52

How to perform without using cursor
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 05-05-2008, 09:01 PM
bluesky bluesky is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 201
bluesky is on a distinguished road
Default Re: Sql loop Statement

Hi shaalini.
Can u try with the following query

DECLARE @MaxRecId as INT
SELECT @MaxRecId = MAX(RecId )
FROM Rec


SET IDENTITY_INSERT Rec ON
INSERT INTO Rec (RecId,[Name],Description)
SELECT RecId = RecId + @MaxRecId ,
[Name] = [Name],
Description= Description,
FROM Rec
SET IDENTITY_INSERT Rec OFF

INSERT INTO RecItem(RecId,ItemId)
SELECT RecId = RecId + @MaxRecId ,
ItemId= ItemId
FROM RecItem
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 05-06-2008, 02:26 AM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Sql loop Statement

Hi, blue sky its working fine. but can u explain about the SET IDENTITY_INSERT
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 05-06-2008, 02:28 AM
bluesky bluesky is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 201
bluesky is on a distinguished road
Default Re: Sql loop Statement

hi,
Identity_Insert
Allows explicit values to be inserted into the identity column of a table.
SET IDENTITY_INSERT [ database_name . [ schema_name ] . ] table { ON | OFF }
database_name

Is the name of the database in which the specified table resides.

schema_name


Is the name of the schema to which the table belongs.

table


Is the name of a table with an identity column.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 05-06-2008, 02:30 AM
bluesky bluesky is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 201
bluesky is on a distinguished road
Default Re: Sql loop Statement

At any time, only one table in a session can have the IDENTITY_INSERT property set to ON. If a table already has this property set to ON, and a SET IDENTITY_INSERT ON statement is issued for another table, SQL Server 2005 returns an error message that states SET IDENTITY_INSERT is already ON and reports the table it is set ON for.

If the value inserted is larger than the current identity value for the table, SQL Server automatically uses the new inserted value as the current identity value.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 05-06-2008, 02:37 AM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Sql loop Statement

Hi blue sky,
Thanks for your information. But as of now my requirement i have to clone the rec based on some condition.when i follow the code which u have mentioned above i m getting the following error Violation of PRIMARY KEY constraint
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #11 (permalink)  
Old 05-06-2008, 10:06 PM
nnraja nnraja is offline
D-Web Programmer
 
Join Date: May 2007
Posts: 94
nnraja is on a distinguished road
Default Re: Sql loop Statement

Hi shaalini,
Provide the table structure for ur new requirement
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12 (permalink)  
Old 05-06-2008, 10:15 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Sql loop Statement

Hi nnraja
Rec
RecId Name Description locationId
1 Chicken chicken 1
2 Pizza Pizza 1
3 chickenbreast chickenbreast 2

and other table as RecItem

RecItemId ReciId ItemId locationId
1 1 23 1
2 1 24 1
3 1 26 1
4 2 43 1
5 2 52 1
6 3 23 2
7 3 42 2
I have to clone both these rec and recitem and i have to get result as follow
Rec
RecId Name Description
1 Chicken chicken
2 Pizza Pizza
3 chicken chicken
4 pizza pizza


RecItem

RecItemId ReciId ItemId
1 1 23
2 1 24
3 1 26
4 2 43
5 2 52
6 3 23
7 3 24
8 3 26
9 4 43
10 4 52
This is my table structure i have to clone based on the locationId
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13 (permalink)  
Old 05-08-2008, 09:14 PM
bluesky bluesky is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 201
bluesky is on a distinguished road
Default Re: Sql loop Statement

DECLARE @LocationSource INT --The location we want to clone
DECLARE @LocationTarget INT --The newly cloned location

--===== Identify the loaction numbers involved in the cloning
SET @LocationSource = 1
SET @LocationTarget = 4

--===== Start a transaction because this all has to happen together
BEGIN TRANSACTION

--===== Get ALL the information we need at once including the new recipe ID's
;WITH
cteKnowAll AS
(
SELECT RecNumber = DENSE_RANK() OVER (ORDER BY s.RecId),
oldRecId =s.RecId,
s.[Name],
OldLocationID = s.LocationID,
NewLocationID = @LocationTarget,
i.ItemId
FROM Rec WITH (TABLOCKX), --Lock the table so no one can insert/update/delete
RecItem WITH (TABLOCKX) --Lock the table so no one can insert/update/delete
WHERE s.recId= s.recId
AND s.LocationID = @LocationSource
)
SELECT NewrecId = k.RecNumber +IDENT_CURRENT('rec'),
[Name] = k.[Name],
NewLocationID = k.NewLocationID,
itemId = k.ItemId
INTO #Clones
FROM cteKnowAll k

--===== Clone the rec
SET IDENTITY_INSERT recON
INSERT INTO rec
(recId,[Name],LocationID)
SELECT DISTINCT
NewrecId ,[Name],NewLocationID
FROM #Clones
SET IDENTITY_INSERT rec ON

--===== Clone the rec items
INSERT INTO recItem
(recId,ItemId)
SELECT DISTINCT NewrecId ,ItemId
FROM #Clones

--===== If we made it to here, we can commit
COMMIT

--===== Verify the original condition of the test tables
SELECT * FROM rec
SELECT * FROM recItem
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #14 (permalink)  
Old 05-13-2008, 12:45 AM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Sql loop Statement

hi bluesky,
I have followed the same procedure as u have mentioned. Its working fine. If i execute the code more than once i got the error as There is already an object named '#Clones' in the database.So i have changed the '#Clones' in to different name when each and every time it executes.
__________________
Shaalini.S
Be the Best of Whatever you are...

Last edited by shaalini : 05-13-2008 at 12:54 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #15 (permalink)  
Old 05-13-2008, 12:48 AM
bluesky bluesky is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 201
bluesky is on a distinguished road
Default Re: Sql loop Statement

Hi shaalini,
Not quite the way to do it... if you convert the code to a stored procedure and run it, #Clone will drop itself at the completion of the stored procedure, automatically. You're trying to run it more than once as a simple script and you need to forcibly drop #Clone for additional runs
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #16 (permalink)  
Old 05-13-2008, 12:51 AM
bluesky bluesky is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 201
bluesky is on a distinguished road
Default Re: Sql loop Statement

hi shaalini,
Try with the following code

--===== If the clone table already exists, drop it...
IF OBJECT_ID('TempDB..#Clone','U') IS NOT NULL
DROP TABLE #Clones

DECLARE @LocationSource INT --The location we want to clone
DECLARE @LocationTarget INT --The newly cloned location

--===== Identify the loaction numbers involved in the cloning
SET @LocationSource = 1
SET @LocationTarget = 4

--===== Start a transaction because this all has to happen together
BEGIN TRANSACTION

--===== Get ALL the information we need at once including the new recipe ID's
;WITH
cteKnowAll AS
(
SELECT RecNumber = DENSE_RANK() OVER (ORDER BY s.RecId),
oldRecId =s.RecId,
s.[Name],
OldLocationID = s.LocationID,
NewLocationID = @LocationTarget,
i.ItemId
FROM Rec WITH (TABLOCKX), --Lock the table so no one can insert/update/delete
RecItem WITH (TABLOCKX) --Lock the table so no one can insert/update/delete
WHERE s.recId= s.recId
AND s.LocationID = @LocationSource
)
SELECT NewrecId = k.RecNumber +IDENT_CURRENT('rec'),
[Name] = k.[Name],
NewLocationID = k.NewLocationID,
itemId = k.ItemId
INTO #Clones
FROM cteKnowAll k

--===== Clone the rec
SET IDENTITY_INSERT recON
INSERT INTO rec
(recId,[Name],LocationID)
SELECT DISTINCT
NewrecId ,[Name],NewLocationID
FROM #Clones
SET IDENTITY_INSERT rec ON

--===== Clone the rec items
INSERT INTO recItem
(recId,ItemId)
SELECT DISTINCT NewrecId ,ItemId
FROM #Clones

--===== If we made it to here, we can commit
COMMIT

--===== Verify the original condition of the test tables
SELECT * FROM rec
SELECT * FROM recItem
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #17 (permalink)  
Old 05-13-2008, 12:56 AM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Sql loop Statement

Hi bluesky.
Its working fine as the result i expected.thanx bluesky
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop in Mysql Murali Database Support 0 02-12-2008 05:37 AM
Statement Coverage vigneshgets Software Testing 2 01-24-2008 02:26 AM
Mysql - what is the use of DO statement? write2ashokkumar Database Support 1 08-14-2007 12:34 AM
How can i do loop testing manually? sundarraja Software Testing 1 07-25-2007 12:02 AM
Javaa:Tutorial - The Loop pranky Java Programming 0 02-23-2007 11:58 PM


All times are GMT -7. The time now is 09:58 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0