Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Remove constraint from VFP app
Message
General information
Forum:
Microsoft SQL Server
Category:
Other
Miscellaneous
Thread ID:
01639883
Message ID:
01639902
Views:
52
>>>>Hi,
>>>>
>>>>The SQL Database has a constraint that I see (in SSMS) in Keys and Indexes. I need to remove this contraint and replace it with another.
>>>>
>>>>For example,
>>>>Constraint name IX_CATEGORY_UNIQUE is as follows:
>>>>
>>>>ALTER TABLE [dbo].[MyTable] ADD  CONSTRAINT [IX_CATEGORY_UNIQUE] UNIQUE NONCLUSTERED 
>>>>(
>>>>	[CATEGORY] ASC
>>>>)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = 
>>>>OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
>>>>GO
>>>>
>>>>
>>>>I want to find it an replace with [SITE_NO],[CATEGORY]
>>>>
>>>>The first step is to find and drop it. What is the SQL Select to exececute from VFP?
>>>>
>>>>TIA
>>>
>>>
>>>TEXT TO lcSQL NOSHOW
>>>     ALTER TABLE [dbo].[MyTable] DROP CONSTRAINT [IX_CATEGORY_UNIQUE]
>>>ENTEXT
>>>IF SQLEXECT(sqlHandler, lcSQL)  < 0
>>>   AERROR(laError)
>>>   MessageBox(laError[1,2])
>>>   RETURN
>>>ENDIF
>>>
>>>TEXT TO lcSQL NOSHOW
>>>ALTER TABLE [dbo].[MyTable] ADD  CONSTRAINT [IX_CATEGORY_UNIQUE] UNIQUE NONCLUSTERED 
>>>(
>>>	[SITE_NO],[CATEGORY]
>>>)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = 
>>>OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
>>>ENDTEXT
>>>IF SQLEXECT(sqlHandler, lcSQL)  < 0
>>>   AERROR(laError)
>>>   MessageBox(laError[1,2])
>>>ENDIF
>>>
>>
>>Thank you very much for your code. I usually run this code on program update (which is done not often but still somewhat a repeat procedure). And I want to avoid having to run this code every time. So I would probably name the second constraint (on SITE_NO, CATEGORY) a little different. For example, IX_TABLE_CATEGORY_UNIQUE. But in this case, what happens when I run this code the 2nd time and the constraint IX_CATEGORY_UNIQUE does not exist and still the code tries to drop it. Then, there will be an error.
>>This is why I thought that before DROP the code would check if the constraint exist. Does it make sense?
>
>
>Is that index?
>
>
>TEXT TO lcSQL NOSHOW 
>IF EXISTS(SELECT * FROM sys.indexes WHERE OBJECT_name(object_id) ='MyTable' AND Name = 'IX_CATEGORY_UNIQUE')
>   BEGIN
>      EXEC sp_executesql N'ALTER TABLE dbo.MyTable DROP INDEX IX_CATEGORY_UNIQUE'
>      EXEC sp_executesql N'ALTER TABLE [dbo].[MyTable] ADD  CONSTRAINT [IX_CATEGORY_UNIQUENEW] UNIQUE NONCLUSTERED 
>                          ([SITE_NO],[CATEGORY])WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF,
>                           SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]'
>   END
>ENDTEXT
>sqlexec....
>
>
First, thank you very much.
When I run the above code from the VFP application, the line that DROPs INDEX, get this error message:
Incorrect syntax near 'INDEX'. If this is intended as a part of a table hint, A WITH keyword and parenthesis are now required. 
Where do I need to add the 'WITH'?
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform