lsmyth1717
Programmer
I have been asked to produce an example of a cursor and then to point out why it isn't required to use on. I am having difficulty creating my example and I was wondering if someone could help me with the syntax to write one.
Basically I want to select the FavouriteSport table (below) into a cursor and then loop through each row and insert all records which have a value football in column sport1, sport2, and sport3 into the FavouriteSportMain table.
I know this is a strange request and you wouldn't need to use a cursor for this but this is what I need to demonstrate. Could someone please show me how to write the cursor code for this.
Cheers
Script to create table.
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FavouriteSport]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[FavouriteSport]
GO
CREATE TABLE [dbo].[FavouriteSport] (
[URN] [bigint] IDENTITY (1, 1) NOT NULL ,
[Sport1] [char] (30) COLLATE Latin1_General_CI_AS NULL ,
[Sport2] [char] (30) COLLATE Latin1_General_CI_AS NULL ,
[Sport3] [char] (30) COLLATE Latin1_General_CI_AS NULL
) ON [PRIMARY]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FavouriteSportMain]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[FavouriteSportMain]
GO
CREATE TABLE [dbo].[FavouriteSportMain] (
[URN] [bigint] IDENTITY (1, 1) NOT NULL ,
[Sport1] [char] (30) COLLATE Latin1_General_CI_AS NULL ,
[Sport2] [char] (30) COLLATE Latin1_General_CI_AS NULL ,
[Sport3] [char] (30) COLLATE Latin1_General_CI_AS NULL
) ON [PRIMARY]
GO
So if my FavouriteSport table looked like this:
URN Sport1 Sport2 Sport3
1 golf tennis gaelic
2 hurling football snooker
3 running rugby golf
4 Karate boxing football
5 Pool running gaelic
After the cursor had finished i'd want it to have inserted two rows
URN Sport1 Sport2 Sport3
2 hurling football snooker
4 Karate boxing football
Basically I want to select the FavouriteSport table (below) into a cursor and then loop through each row and insert all records which have a value football in column sport1, sport2, and sport3 into the FavouriteSportMain table.
I know this is a strange request and you wouldn't need to use a cursor for this but this is what I need to demonstrate. Could someone please show me how to write the cursor code for this.
Cheers
Script to create table.
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FavouriteSport]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[FavouriteSport]
GO
CREATE TABLE [dbo].[FavouriteSport] (
[URN] [bigint] IDENTITY (1, 1) NOT NULL ,
[Sport1] [char] (30) COLLATE Latin1_General_CI_AS NULL ,
[Sport2] [char] (30) COLLATE Latin1_General_CI_AS NULL ,
[Sport3] [char] (30) COLLATE Latin1_General_CI_AS NULL
) ON [PRIMARY]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FavouriteSportMain]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[FavouriteSportMain]
GO
CREATE TABLE [dbo].[FavouriteSportMain] (
[URN] [bigint] IDENTITY (1, 1) NOT NULL ,
[Sport1] [char] (30) COLLATE Latin1_General_CI_AS NULL ,
[Sport2] [char] (30) COLLATE Latin1_General_CI_AS NULL ,
[Sport3] [char] (30) COLLATE Latin1_General_CI_AS NULL
) ON [PRIMARY]
GO
So if my FavouriteSport table looked like this:
URN Sport1 Sport2 Sport3
1 golf tennis gaelic
2 hurling football snooker
3 running rugby golf
4 Karate boxing football
5 Pool running gaelic
After the cursor had finished i'd want it to have inserted two rows
URN Sport1 Sport2 Sport3
2 hurling football snooker
4 Karate boxing football