Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Administration (2000)
 TRUNCATE All Tables With RI

Author  Topic 

X002548
Not Just a Number

15586 Posts

Posted - 2004-06-22 : 12:13:29
Anyone got code to TRUNCATE every table in a database while leaving RI intact?

The catalog is such a pain to decipher...



Brett

8-)

X002548
Not Just a Number

15586 Posts

Posted - 2004-06-22 : 12:28:25
I thought the error would raise to the calling code...but it raises all the way out...

I'm so confused...does the calling process have to be a sproc?


EDIT: Oh, the Call of myDynamicSQL99 failes with:

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

I expected that I could interogate the rc from the sproc...but it just stops...


CREATE TABLE myTable99(Col1 int PRIMARY KEY)
GO
CREATE TABLE myTable00(Col1 int, Col2 int, PRIMARY KEY(Col1,Col2), FOREIGN KEY (Col1) REFERENCES myTable99(Col1))
GO

INSERT INTO myTable99(Col1) SELECT 1
INSERT INTO myTable00(Col1, Col2) SELECT 1,2
GO

CREATE PROC myDynamicSQL99 @sql varchar(8000) AS EXEC(@sql)
GO

DECLARE @error int, @TABLE_NAME sysname, @sql varchar(8000), @x int, @rc int
SELECT @error = 0, @x = 0

DoItAgain:

DECLARE myCursor99 CURSOR
FOR
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.Tables
WHERE TABLE_NAME LIKE 'myTable%'
ORDER BY TABLE_NAME DESC

OPEN myCursor99

FETCH NEXT FROM myCursor99 INTO @TABLE_NAME

WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @SQL = 'TRUNCATE TABLE ' + @TABLE_NAME
SELECT @SQL
EXEC @rc = myDynamicSQL99 @SQL
SELECT @Error = @@Error
SELECT 'Error Code: ' + CONVERT(varchar(15),@Error) + ' @rc: '+ CONVERT(varchar(15),@rc)
IF @Error <> 0
SELECT @x = @x + 1
FETCH NEXT FROM myCursor99 INTO @TABLE_NAME
END

CLOSE myCursor99
DEALLOCATE myCursor99

IF @x <> 0
BEGIN
SELECT @Error = 0
GOTO DoItAgain
END
GO

DROP PROC myDynamicSQL99
DROP TABLE myTable00
DROP TABLE myTable99
GO



yeah, yeah...like opening an egg with a sledge hammer...



Brett

8-)
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-06-22 : 12:28:58
Regardless if there is no data in the child table, you can not truncate the parent table while the RI is still there.

quote:


You cannot use TRUNCATE TABLE on a table referenced by a FOREIGN KEY constraint; instead, use DELETE statement without a WHERE clause.




Tara
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-06-22 : 12:48:05
Well that sucks...

Sounds like an approach change is required....

Didn't we just do a drop RI method recently?



Brett

8-)
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2004-06-22 : 13:25:43
DELETE no good?

If its any help I've got a script lying around somewhere which works out the RI and from that a sequence that deletes all Children first, rippling up to Parents, Grand Parents and then Mega Ancestors

If its a One Time deal, with no concurrent connection issues, my inclination would be to GENERATE SQL script the DB. EM kindly puts all the DROP FK stuff in a group at the top, and all the CREATE FK [well, ALTER actually, but you know that] stuff in a group at the bottom, so you can easily run all the DROP stuff, then do your TRUNCATES, and then reapply the FKs. Presumably no issue if any DELETE TRIGGERS are still in situ if you go down this route? I mean, it isn't wanting to delete stuff in a remote database, or something ghastly like that, that a TRUNCATE would side-swipe?

Kristen
Go to Top of Page

eyechart
Master Smack Fu Yak Hacker

3575 Posts

Posted - 2004-06-22 : 13:29:19
why don't you just disable all the constraints first.


-ec
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-06-22 : 13:42:08
quote:
Originally posted by eyechart

why don't you just disable all the constraints first.


-ec



That's an idea...

Unfortunatley

SELECT * FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS

Doesn't give you the table the constraint belongs to..



Brett

8-)

EDIT: Did a quick check...doesn't matter if it's disabled...still can't TRUNCATE....

I'm Gonna have to drop them and recreate them...

See this keeps coming back to wondering how EM generates the script...

Go to Top of Page

surefooted
Posting Yak Master

188 Posts

Posted - 2004-06-22 : 16:36:58
Brett this is what I use to empty RI tables try it out, make work for you. As yu found, you cannot just truncate all of teh tables, but you can truncate some..


declare @tblname sysname
declare @sqlstring varchar(500)
declare @sqlstring2 varchar(500)
declare @sqlstring3 varchar(500)
declare @loopcount int
declare @loopmax int
declare @err int

Create Table #Temp1
(ident int Identity,
tablename sysname)


-- delete from tables that refer to other tables


Insert #temp1 (tablename)
select name
from sysobjects
where type = 'u' and
name <> 'dtproperties' and
id not in (select rkeyid from sysforeignkeys)

Set @loopmax = (Select max(ident) from #temp1)
Set @loopcount = 1

While @loopcount <= @loopmax
Begin

Set @tblname = (Select tablename from #temp1 where ident = @loopcount)


set @sqlstring2 = 'truncate table [' + @tblname + ']'
print @sqlstring2
exec (@sqlstring2)


Set @err = @@error
IF @err <> 0
Begin
print 'error yo on table ' + @tblname + ' with error ' + @err


End

Set @loopcount = @loopcount + 1
End

Truncate table #temp1

-- delete from tables that are referenced by other tables

Insert #temp1 (tablename)
select name
from sysobjects
where type = 'u' and
id in (select rkeyid from sysforeignkeys)


Set @loopmax = (Select max(ident) from #temp1)
Set @loopcount = 1

While @loopcount <= @loopmax
Begin

Set @tblname = (Select tablename from #temp1 where ident = @loopcount)

set @sqlstring = 'alter table ' + @tblname + ' noCHECK CONSTRAINT all'
print (@sqlstring)
exec (@sqlstring)
set @sqlstring2 = 'delete [' + @tblname + ']'
print @sqlstring2
exec (@sqlstring2)
set @sqlstring3 = 'alter table ' + @tblname + ' CHECK CONSTRAINT all'
print (@sqlstring3)

Set @err = @@error
IF @err <> 0
Begin
print 'error yo on table ' + @tblname + ' with error ' + @err

End

Set @loopcount = @loopcount + 1
End

drop table #temp1


-Jon
Just a starting member.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-06-22 : 16:40:02
Hey Jon, you are a "Yak Posting Veteran" now. No longer a starting member.

Tara
Go to Top of Page

surefooted
Posting Yak Master

188 Posts

Posted - 2004-06-22 : 16:46:57
doh...updated...

-Jon
Now a "Yak Posting Veteran".
Go to Top of Page

eyechart
Master Smack Fu Yak Hacker

3575 Posts

Posted - 2004-06-22 : 18:36:51
quote:
Originally posted by X002548

EDIT: Did a quick check...doesn't matter if it's disabled...still can't TRUNCATE....





After I posted that I did a quick test case and found the same thing as you. Seems that SQL Server doesn't actually disable the constraint for all operations. BOL says it is just disabled for inserts and updates.



-ec
Go to Top of Page
   

- Advertisement -