Good question, but the answer is wrong.
SQL Server evaluates object references during parse time. Flow-control logic is not applied at this stage.
This is not true. If you change the temporary table to a regular table (e.g. dbo.Tab), the script will work.
DECLARE @MoreColumns bit;
SET @MoreColumns = 1
IF @MoreColumns = 0
CREATE TABLE dbo.Tab (
id int,
name varchar(50)
);
ELSE
BEGIN
CREATE TABLE dbo.Tab (
id int,
name varchar(50),
Description varchar(8000),
nvarchar(200)
)
END
GO
DROP TABLE dbo.Tab
If more than one temporary table is created inside a single stored procedure or batch, they must have different names
ms-help://MS.SQLCC.v10/MS.SQLSVR.v10.en/s10de_6tsql/html/1e068443-b9ea-486a-804f-ce7b6e048e8b.htm
Obviously SQL Server does not care if the two create table statements are mutually exclusive and does not allow that.
Best Regards,
Chris Büttner