Showing posts with label operations. Show all posts
Showing posts with label operations. Show all posts

Monday, March 12, 2012

Help W/Disabling FK Constraints for Batch Operations

HI all,

I'm trying to have a SProc that will initialize a database for me. This db is in development (I'm primarily writing SSIS packages, atm), and I constantly need to truncate the tables, then re-add a dummy/unknown row (PK/Identity value = 1). Of course, I need triggers not to fire (got that part working), and FK constraints to be bypassed temporarily -- that's the problem.

Here's where I'm at:

-
CREATE PROCEDURE [dbo].[_InitializeDB]
AS

SET NOCOUNT ON

DECLARE @.name varchar(255)
DECLARE @.sql nvarchar(255)

DECLARE tables CURSOR FOR SELECT [name] FROM [sysobjects] WHERE [type]='U' AND [name]<>'sysdiagrams'

OPEN tables
FETCH NEXT FROM tables INTO @.name
WHILE @.@.FETCH_STATUS=0
BEGIN
SET @.sql = 'ALTER TABLE ['+ @.name + '] NOCHECK CONSTRAINT ALL'
EXEC sp_executeSQL @.sql
SET @.sql = 'DISABLE TRIGGER ALL ON [' + @.name + ']'
EXEC sp_executeSQL @.sql
SET @.sql = 'TRUNCATE TABLE [' + @.name + ']'
EXEC sp_executesql @.sql
BEGIN TRY
SET @.sql = 'INSERT INTO [' + @.name + '] (Active) VALUES (0)'
EXEC sp_executeSQL @.sql
END TRY
BEGIN CATCH
PRINT @.sql + ':'
PRINT ERROR_MESSAGE()
END CATCH
SET @.sql = 'ENABLE TRIGGER ALL ON [' + @.name + ']'
EXEC sp_executeSQL @.sql
SET @.sql = 'ALTER TABLE ['+ @.name + '] CHECK CONSTRAINT ALL'
EXEC sp_executeSQL @.sql
FETCH NEXT FROM tables INTO @.name
END

CLOSE tables
DEALLOCATE tables
-

Running this Sproc produces (for the first ref'd table):

Msg 4712, Level 16, State 1, Line 1
Cannot truncate table 'Person' because it is being referenced by a FOREIGN KEY constraint.

If you dont wanna be bothered by the FK Constraints you have to drop them do your work and recreate them after you did your work.

HTH, Jens Suessmeyer.


http://www.sqlserver2005.de

|||TRUNCATE TABLE doesn't check the state of the FK constraint so this method will not work. You will have to drop and recreate the FK constraint for the truncate table to work.

Sunday, February 19, 2012

Help required to do backup/restore from a client app

Hi,
As part of a client application that we are building, we need to do database
backup and restore operations. The application could be run on a machine that
does not have SQL server.
Need clarification on:
1. How do we initiate the backup/restore operations from the client machine?
Should we be looking at SQL DMO objects?
If so, the client machine may not have SQL DMO objects and its dependent
files.
Are these files distributable so that they could be included along with our
application?
2. Our application is also 'localizable' and it should be able to work with
different language versions of SQL Server.
In this case, should we be using 'localized' versions of SQL DMO files?
3. Or is there an alternate way of achieving backup/restore and not be
concerned about 'redistributable' versions of SQL DMO files (and 'localized'
file set additionally, if it comes to requiring such files)?
Any help on these will be greatly appreciated.
Thanks.
Regards,
KK
DMO, ADO or any other type of connection that can execute TSQL commands is
all you need to initiate backups. DMO doesn't include any UI functionality
so AFAIK there is no localization setting in that API. As far as the server
is concerned the language is an option set in the connection (SET LANGUAGE).
DMO is redistributable (see the readme REDIST.TXT on your installation disc
for the actual file names) with the proviso that the client machine still
needs to be properly licensed as a client for SQL Server.
David Portas
SQL Server MVP

Help required to do backup/restore from a client app

Hi,
As part of a client application that we are building, we need to do database
backup and restore operations. The application could be run on a machine tha
t
does not have SQL server.
Need clarification on:
1. How do we initiate the backup/restore operations from the client machine?
Should we be looking at SQL DMO objects?
If so, the client machine may not have SQL DMO objects and its dependent
files.
Are these files distributable so that they could be included along with our
application?
2. Our application is also 'localizable' and it should be able to work with
different language versions of SQL Server.
In this case, should we be using 'localized' versions of SQL DMO files?
3. Or is there an alternate way of achieving backup/restore and not be
concerned about 'redistributable' versions of SQL DMO files (and 'localized'
file set additionally, if it comes to requiring such files)?
Any help on these will be greatly appreciated.
Thanks.
Regards,
KKDMO, ADO or any other type of connection that can execute TSQL commands is
all you need to initiate backups. DMO doesn't include any UI functionality
so AFAIK there is no localization setting in that API. As far as the server
is concerned the language is an option set in the connection (SET LANGUAGE).
DMO is redistributable (see the readme REDIST.TXT on your installation disc
for the actual file names) with the proviso that the client machine still
needs to be properly licensed as a client for SQL Server.
David Portas
SQL Server MVP
--

Help required to do backup/restore from a client app

Hi,
As part of a client application that we are building, we need to do database
backup and restore operations. The application could be run on a machine that
does not have SQL server.
Need clarification on:
1. How do we initiate the backup/restore operations from the client machine?
Should we be looking at SQL DMO objects?
If so, the client machine may not have SQL DMO objects and its dependent
files.
Are these files distributable so that they could be included along with our
application?
2. Our application is also 'localizable' and it should be able to work with
different language versions of SQL Server.
In this case, should we be using 'localized' versions of SQL DMO files?
3. Or is there an alternate way of achieving backup/restore and not be
concerned about 'redistributable' versions of SQL DMO files (and 'localized'
file set additionally, if it comes to requiring such files)?
Any help on these will be greatly appreciated.
Thanks.
Regards,
KKDMO, ADO or any other type of connection that can execute TSQL commands is
all you need to initiate backups. DMO doesn't include any UI functionality
so AFAIK there is no localization setting in that API. As far as the server
is concerned the language is an option set in the connection (SET LANGUAGE).
DMO is redistributable (see the readme REDIST.TXT on your installation disc
for the actual file names) with the proviso that the client machine still
needs to be properly licensed as a client for SQL Server.
--
David Portas
SQL Server MVP
--