SET Indentity_Insert ON

Hi,

I need to change a non identity field in a table to an identity field. Therefore I create a new table with identity field and copy data. In order to maintain the old ID values, I need to set Indentity_Insert for the new table to ON

The following code generates an error: Incorrect syntax near the keyword 'SET'
Can anybody tell me what is wrong?
procedure SP_ReplaceWeekplanning(Versie : Integer; DropOudeStoredProc : Boolean);
begin
  if DropOudeStoredProc then
  try
    DM.ADOConnectionSQL.Execute('Drop PROCEDURE dbo.ReplaceWeekplanning');
  except
  end;
  if (Versie = 1) then
    vSQLText := 'CREATE PROCEDURE ReplaceWeekplanning AS                ' + chr(13) +
                'BEGIN                                                  ' + chr(13) +
                'CREATE TABLE [dbo].[WeekPlanning2](                    ' + chr(13) +
                '	[WeekPlanningID] [int] IDENTITY(1,1) NOT NULL,        ' + chr(13) +
                '	[EigenBedrijfnr] [int] NOT NULL,                      ' + chr(13) +
                '	[OpdrachtID] [int] NULL,                              ' + chr(13) +
                '	[Bedrijfsnr] [int] NULL,                              ' + chr(13) +
                '	[Relatienr] [int] NULL,                               ' + chr(13) +
                '	[StartDatumTijd] [datetime] NULL,                     ' + chr(13) +
                '	[EindDatumTijd] [datetime] NULL,                      ' + chr(13) +
                '	[AantalPersonen] [int] NULL,                          ' + chr(13) +
                '	[LocatieBedrijfsnr] [int] NULL,                       ' + chr(13) +
                '	[Functienr] [int] NULL,                               ' + chr(13) +
                '	[IsVerwijderd] [bit] NOT NULL,                        ' + chr(13) +
                '	[MedewerkerID] [int] NOT NULL,                        ' + chr(13) +
                '	[Afspraaktypenr] [int] NOT NULL,                      ' + chr(13) +
                '	[Medewerkernr] [int] NOT NULL,                        ' + chr(13) +
                '	[AutomatischPlannen] [bit] NOT NULL,                  ' + chr(13) +
                ' CONSTRAINT [aaaaaWeekPlanning_PK] PRIMARY KEY NONCLUSTERED                         ' + chr(13) +
                '(	[WeekPlanningID] ASC,                                                            ' + chr(13) +
                '	[EigenBedrijfnr] ASC                                                               ' + chr(13) +
                ')WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF,                             ' + chr(13) +
                ' IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]  ' + chr(13) +
                ') ON [PRIMARY]                                                                      ' + chr(13) +
                'go                                                                                  ' + chr(13) +
                'SET IDENTITY_INSERT [dbo].[WeekPlanning2] ON                                        ' + chr(13) +
                'go                                                                                  ' + chr(13) +
                'IF EXISTS(SELECT * FROM [dbo].[WeekPlanning])                                       ' + chr(13) +
                'INSERT INTO [KLAASSENDB].[dbo].[WeekPlanning2]                                      ' + chr(13) +
                '           ([WeekPlanningID]                                                        ' + chr(13) +
                '           ,[EigenBedrijfnr]                                                        ' + chr(13) +
                '           ,[OpdrachtID]                                                            ' + chr(13) +
                '           ,[Bedrijfsnr]                                                            ' + chr(13) +
                '           ,[Relatienr]                                                             ' + chr(13) +
                '           ,[StartDatumTijd]                                                        ' + chr(13) +
                '           ,[EindDatumTijd]                                                         ' + chr(13) +
                '           ,[AantalPersonen]                                                        ' + chr(13) +
                '           ,[LocatieBedrijfsnr]                                                     ' + chr(13) +
                '           ,[Functienr]                                                             ' + chr(13) +
                '           ,[IsVerwijderd]                                                          ' + chr(13) +
                '           ,[MedewerkerID]                                                          ' + chr(13) +
                '           ,[Afspraaktypenr]                                                        ' + chr(13) +
                '           ,[Medewerkernr]                                                          ' + chr(13) +
                '           ,[AutomatischPlannen])                                                   ' + chr(13) +
                '     SELECT [WeekPlanningID]                                                        ' + chr(13) +
                '           ,[EigenBedrijfnr]                                                        ' + chr(13) +
                '           ,[OpdrachtID]                                                            ' + chr(13) +
                '           ,[Bedrijfsnr]                                                            ' + chr(13) +
                '           ,[Relatienr]                                                             ' + chr(13) +
                '           ,[StartDatumTijd]                                                        ' + chr(13) +
                '           ,[EindDatumTijd]                                                         ' + chr(13) +
                '           ,[AantalPersonen]                                                        ' + chr(13) +
                '           ,[LocatieBedrijfsnr]                                                     ' + chr(13) +
                '           ,[Functienr]                                                             ' + chr(13) +
                '           ,[IsVerwijderd]                                                          ' + chr(13) +
                '           ,[MedewerkerID]                                                          ' + chr(13) +
                '           ,[Afspraaktypenr]                                                        ' + chr(13) +
                '           ,[Medewerkernr]                                                          ' + chr(13) +
                '           ,[AutomatischPlannen]                                                    ' + chr(13) +
                '           FROM [dbo].[WeekPlanning] TABLOCKX                                       ' + chr(13) +
                '                                                                                    ' + chr(13) +
                'go                                                                                  ' + chr(13) +
                'SET IDENTITY_INSERT [dbo].[WeekPlanning2] OFF                                       ' + chr(13) +
                'go                                                                                  ' + chr(13) +
                'DROP TABLE [dbo].[WeekPlanning]                                                     ' + chr(13) +
                'go                                                                                  ' + chr(13) +
                'sp_RENAME ' + QuotedStr('[WeekPlanning2]') + ', ' + QuotedStr('WeekPlanning') + '   ' + chr(13) +
                'go                                                                                  ' + chr(13) +
                'END ';
//  try
    DM.ADOConnectionSQL.Execute(vSQLText);
//  except
//  end;
end;

Open in new window

Stef MerlijnDeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sinisa VukSoftware architectCommented:
Try to replace
go
word in sql to
;
or to nothing
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Stef MerlijnDeveloperAuthor Commented:
Replacing "Go" with ";" results in a syntax error.
It looks like the compiler reads the string and detects a reserved keyword???
0
Sinisa VukSoftware architectCommented:
0
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Stef MerlijnDeveloperAuthor Commented:
The following code skips the SET-error, but now an error is presented at the sp_RENAME.
Error: Incorrect syntax near sp_RENAME

procedure SP_ReplaceWeekplanning(Versie : Integer; DropOudeStoredProc : Boolean);
begin
  if DropOudeStoredProc then
  try
    DM.ADOConnectionSQL.Execute('Drop PROCEDURE dbo.ReplaceWeekplanning');
  except
  end;
  if (Versie = 1) then
    vSQLText := 'CREATE PROCEDURE ReplaceWeekplanning AS                ' + chr(13) +
                'BEGIN                                                  ' + chr(13) +
                'CREATE TABLE dbo.WeekPlanning2(                        ' + chr(13) +
                '	[WeekPlanningID] [int] IDENTITY(1,1) NOT NULL,        ' + chr(13) +
                '	[EigenBedrijfnr] [int] NOT NULL,                      ' + chr(13) +
                '	[OpdrachtID] [int] NULL,                              ' + chr(13) +
                '	[Bedrijfsnr] [int] NULL,                              ' + chr(13) +
                '	[Relatienr] [int] NULL,                               ' + chr(13) +
                '	[StartDatumTijd] [datetime] NULL,                     ' + chr(13) +
                '	[EindDatumTijd] [datetime] NULL,                      ' + chr(13) +
                '	[AantalPersonen] [int] NULL,                          ' + chr(13) +
                '	[LocatieBedrijfsnr] [int] NULL,                       ' + chr(13) +
                '	[Functienr] [int] NULL,                               ' + chr(13) +
                '	[IsVerwijderd] [bit] NOT NULL,                        ' + chr(13) +
                '	[MedewerkerID] [int] NOT NULL,                        ' + chr(13) +
                '	[Afspraaktypenr] [int] NOT NULL,                      ' + chr(13) +
                '	[Medewerkernr] [int] NOT NULL,                        ' + chr(13) +
                '	[AutomatischPlannen] [bit] NOT NULL,                  ' + chr(13) +
                ' CONSTRAINT [aaaaaWeekPlanning_PK] PRIMARY KEY NONCLUSTERED                         ' + chr(13) +
                '(	[WeekPlanningID] ASC,                                                            ' + chr(13) +
                '	[EigenBedrijfnr] ASC                                                               ' + chr(13) +
                ')WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF,                             ' + chr(13) +
                ' IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]  ' + chr(13) +
                ') ON [PRIMARY];                                                                     ' + chr(13) +
                ' ' + chr(13) +
                'SET IDENTITY_INSERT dbo.WeekPlanning2 ON;                                           ' + chr(13) +
                ' ' + chr(13) +
                'IF EXISTS(SELECT * FROM dbo.WeekPlanning)                                           ' + chr(13) +
                'INSERT INTO dbo.WeekPlanning2                                                       ' + chr(13) +
                '           ([WeekPlanningID]                                                        ' + chr(13) +
                '           ,[EigenBedrijfnr]                                                        ' + chr(13) +
                '           ,[OpdrachtID]                                                            ' + chr(13) +
                '           ,[Bedrijfsnr]                                                            ' + chr(13) +
                '           ,[Relatienr]                                                             ' + chr(13) +
                '           ,[StartDatumTijd]                                                        ' + chr(13) +
                '           ,[EindDatumTijd]                                                         ' + chr(13) +
                '           ,[AantalPersonen]                                                        ' + chr(13) +
                '           ,[LocatieBedrijfsnr]                                                     ' + chr(13) +
                '           ,[Functienr]                                                             ' + chr(13) +
                '           ,[IsVerwijderd]                                                          ' + chr(13) +
                '           ,[MedewerkerID]                                                          ' + chr(13) +
                '           ,[Afspraaktypenr]                                                        ' + chr(13) +
                '           ,[Medewerkernr]                                                          ' + chr(13) +
                '           ,[AutomatischPlannen])                                                   ' + chr(13) +
                '     SELECT [WeekPlanningID]                                                        ' + chr(13) +
                '           ,[EigenBedrijfnr]                                                        ' + chr(13) +
                '           ,[OpdrachtID]                                                            ' + chr(13) +
                '           ,[Bedrijfsnr]                                                            ' + chr(13) +
                '           ,[Relatienr]                                                             ' + chr(13) +
                '           ,[StartDatumTijd]                                                        ' + chr(13) +
                '           ,[EindDatumTijd]                                                         ' + chr(13) +
                '           ,[AantalPersonen]                                                        ' + chr(13) +
                '           ,[LocatieBedrijfsnr]                                                     ' + chr(13) +
                '           ,[Functienr]                                                             ' + chr(13) +
                '           ,[IsVerwijderd]                                                          ' + chr(13) +
                '           ,[MedewerkerID]                                                          ' + chr(13) +
                '           ,[Afspraaktypenr]                                                        ' + chr(13) +
                '           ,[Medewerkernr]                                                          ' + chr(13) +
                '           ,[AutomatischPlannen]                                                    ' + chr(13) +
                '           FROM dbo.WeekPlanning TABLOCKX;                                          ' + chr(13) +
                ' ' + chr(13) +
                'SET IDENTITY_INSERT dbo.WeekPlanning2 OFF;                                          ' + chr(13) +
                ' ' + chr(13) +
                'DROP TABLE dbo.WeekPlanning;                                                        ' + chr(13) +
                ' ' + chr(13) +
                'sp_RENAME ' + QuotedStr('WeekPlanning2') + ', ' + QuotedStr('WeekPlanning') + ';  ' + chr(13) +
                ' ' + chr(13) +
                'END ';
//  try
    DM.ADOConnectionSQL.Execute(vSQLText);
//  except
//  end;
end;

Open in new window

0
Stef MerlijnDeveloperAuthor Commented:
Got it.
Exec must be placed before sp_RENAME
0
Stef MerlijnDeveloperAuthor Commented:
Thank you very much.
0
Sinisa VukSoftware architectCommented:
Yes :-)
Small comment - lots of space you use. Clear these on right....
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.