开发者

Truncate tables?

开发者 https://www.devze.com 2022-12-14 14:18 出处:网络
I\'m trying to create an Integration Service project that will use a set of existing (legacy) databases to create a new database. As I\'m a SSIS noob, my progress is rat开发者_运维知识库her incrementa

I'm trying to create an Integration Service project that will use a set of existing (legacy) databases to create a new database. As I'm a SSIS noob, my progress is rat开发者_运维知识库her incremental and adding a truncate on the entire target database would help me test it more easily.

Is such a thing possible?


This SQL statement will do it for you. If you want it to be part of an IS project then make it the SQL of a SQL Script task.

declare @object_id int
declare @str varchar(255)

select @object_id = min(object_id) 
from sys.tables
where name <> 'sysdtslog90'

while @object_id is not null
begin
    select @str = 'truncate table ' + name
    from sys.tables
    where object_id = @object_id

    print @str
    exec (@str)

    select @object_id = min(object_id) 
    from sys.tables
    where object_id > @object_id
    and name <> 'sysdtslog90'
end

If you are not happy with using the sys.tables view, then you could always define the table list yourself.


Here is an interesting solution

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=65341


Why use such a complicated SQL statements? Why not just use a Execute SQL Task with an expression variable with value of (DELETE FROM tablename WHERE ControlKey = ?)


Here a Doctrine 1.2 code to empty all the tables, ignoring innoDB foreign KEYS. I'm using it to bootstrap unit tests

/**
 * Empty all tables, ignoring innoDB foreign keys
 */
function emptyTables()
{
    $conn = Doctrine_Manager::getInstance()->getCurrentConnection();
    $stmt = $conn->prepare('SHOW TABLES');
    $stmt->execute();

    $queries = "SET FOREIGN_KEY_CHECKS = 0;";
    foreach($stmt->fetchAll() as $table) {
        $queries .= "TRUNCATE  `".array_shift($table)."`;\n";
    }
    $queries .= "SET FOREIGN_KEY_CHECKS = 1;";

    $conn->getDbh()->query($queries);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消