| 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...Brett8-) |
|
|
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 1Cannot 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)GOCREATE TABLE myTable00(Col1 int, Col2 int, PRIMARY KEY(Col1,Col2), FOREIGN KEY (Col1) REFERENCES myTable99(Col1))GOINSERT INTO myTable99(Col1) SELECT 1INSERT INTO myTable00(Col1, Col2) SELECT 1,2GOCREATE PROC myDynamicSQL99 @sql varchar(8000) AS EXEC(@sql)GODECLARE @error int, @TABLE_NAME sysname, @sql varchar(8000), @x int, @rc intSELECT @error = 0, @x = 0DoItAgain:DECLARE myCursor99 CURSOR FOR SELECT TABLE_NAME FROM INFORMATION_SCHEMA.Tables WHERE TABLE_NAME LIKE 'myTable%' ORDER BY TABLE_NAME DESCOPEN myCursor99FETCH NEXT FROM myCursor99 INTO @TABLE_NAMEWHILE @@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 ENDCLOSE myCursor99DEALLOCATE myCursor99IF @x <> 0 BEGIN SELECT @Error = 0 GOTO DoItAgain ENDGODROP PROC myDynamicSQL99DROP TABLE myTable00DROP TABLE myTable99GO yeah, yeah...like opening an egg with a sledge hammer...Brett8-) |
 |
|
|
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 |
 |
|
|
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?Brett8-) |
 |
|
|
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 AncestorsIf 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 |
 |
|
|
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 |
 |
|
|
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_CONSTRAINTSDoesn't give you the table the constraint belongs to..Brett8-)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... |
 |
|
|
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 sysnamedeclare @sqlstring varchar(500)declare @sqlstring2 varchar(500)declare @sqlstring3 varchar(500)declare @loopcount intdeclare @loopmax intdeclare @err intCreate 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 = 1While @loopcount <= @loopmaxBeginSet @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 EndSet @loopcount = @loopcount + 1EndTruncate table #temp1-- delete from tables that are referenced by other tablesInsert #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 = 1While @loopcount <= @loopmaxBeginSet @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 EndSet @loopcount = @loopcount + 1Enddrop table #temp1 -JonJust a starting member. |
 |
|
|
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 |
 |
|
|
surefooted
Posting Yak Master
188 Posts |
Posted - 2004-06-22 : 16:46:57
|
doh...updated... -JonNow a "Yak Posting Veteran". |
 |
|
|
eyechart
Master Smack Fu Yak Hacker
3575 Posts |
Posted - 2004-06-22 : 18:36:51
|
quote: Originally posted by X002548EDIT: 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 |
 |
|
|
|