--Skapar tabell
IF exists (SELECT * FROM sysobjects
WHERE ID = object_id(N'[dbo].[TblCurrentof]'))
DROP TABLE [dbo].[TblCurrentof]
GO
CREATE TABLE [dbo].[TblCurrentof] (
[Field1] [varchar] (10) NULL ,
[Field2] [varchar] (10) NULL
) ON [PRIMARY]
GO
--Adderar värden i Field1
INSERT INTO TblCurrentof(Field1)
VALUES('POST NR 1')
INSERT INTO TblCurrentof(Field1)
VALUES('POST NR 2')
INSERT INTO TblCurrentof(Field1)
VALUES('POST NR 3')
INSERT INTO TblCurrentof(Field1)
VALUES('POST NR 4')
INSERT INTO TblCurrentof(Field1)
VALUES('POST NR 5')
--Skapar lagrad procedur
IF exists (SELECT * FROM sysobjects
WHERE ID = object_id(N'[dbo].[SprCurrentof]'))
DROP PROCEDURE SprCurrentof
GO
CREATE procedure SprCurrentof
AS
DECLARE @Field1 Varchar(10)
--Öppnar upp en cursor
DECLARE CrsCurrentof CURSOR
FOR
SELECT Field1 FROM TblCurrentof
OPEN CrsCurrentof
FETCH NEXT FROM CrsCurrentof INTO @Field1
--Loopar cursorn
WHILE @@FETCH_STATUS=0
BEGIN
--Om det är post nr 3 eller 5
uppdateras aktuell post
IF(RIGHT(@Field1,1)in('3','5'))
BEGIN
UPDATE TblCurrentof SET
Field2='NR '+RIGHT(@Field1,1)
WHERE CURRENT OF CrsCurrentof
END
FETCH NEXT FROM CrsCurrentof INTO @Field1
END
CLOSE CrsCurrentof
DEALLOCATE CrsCurrentof
SELECT * FROM TblCurrentof