Copy Only the Structure of A Table

This old little trick had been asked for so many times. So, it might be a good itea to post it on the blog.

In Oracle:

CREATE TABLE NEW_TAB AS
SELECT *
FROM OLD_TAB
WHERE 0 = -1

In SQL Server:

SELECT *
INTO NEW_TAB
FROM OLD_TAB
WHERE 0 = -1

The key point is WHERE condition. Because this condition is never to be met, so there is no records actually being selected. Someone might like WHERE 1 = 2, however, 1 or 2 can be referred to column one and column two. If by chance the value in column one just equals to column two, haha, you will get some data.

No comments:

Post a Comment

Labels