我正在使用 SQL 2005,并具有以下代码:
DECLARE @exist4 INT
IF ( SELECT COUNT(*) existe
FROM sysobjects obj
WHERE obj.name = 'table4'
) > 0
BEGIN
SET @exist4 = 1
END
ELSE
BEGIN
SET @exist4 = 0
END
/*now if table4 exist i need add to my query but if i do something as it
i got next error
Invalid object name 'table4'.*/
IF ( @exist4 = 0 )
BEGIN
SELECT [table].col1 ,
[table].col2 ,
[table].col3 ,
[table].colN
FROM [table] ,
table2 ,
table3
WHERE [table].id = table1.id
AND table3.id = table2.id
END
ELSE
BEGIN
SELECT [table].col1 ,
[table].col2 ,
[table].col3 ,
[table].colN
FROM [table] ,
table2 ,
table3 ,
table4
WHERE [table].id = table2.id
AND table3.id = table2.id
AND table4.id = table3.id
END
当 Table4 不存在时,此代码返回错误。我怎样才能让这个查询工作?