This topic has been archived. It cannot be replied.
-
工作学习 / 学科技术讨论 / 简单的 T-SQL 问题,how can I create a view under a condition?when doing this
IF OBJECT_ID ('Roles', 'table') IS NULL BEGIN
PRINT 'not exist!'
END ELSE BEGIN
PRINT 'exist!'
END
it works fine
when doing this
IF OBJECT_ID ('Roles', 'table') IS NULL BEGIN
CREATE VIEW [dbo].[Roles] as (SELECT * FROM [dbo].[my_Roles])
END
it gave me Incorrect syntax near the keyword 'VIEW'.
-sowen(昂居居);
2008-6-10
{361}
(#4493977@0)
-
dont if this works CREATE VIEW [Roles] as (SELECT * FROM [dbo].[my_Roles]) go
-c1xwy(洪兴罩俺去战斗);
2008-6-10
(#4493985@0)
-
just found the reason and the solution, haha
-sowen(昂居居);
2008-6-10
(#4493996@0)
-
en, good, learned something
-c1xwy(洪兴罩俺去战斗);
2008-6-10
(#4494014@0)
-
if exists (select 1 from sysobjects where name = 'Roles' and type = 'V')if exists (select 1 from sysobjects where name = 'Roles' and type = 'V')
drop view Roles
go
CREATE VIEW [dbo].[Roles] as (SELECT * FROM [dbo].[my_Roles])
-rbl73b2(独眼螳螂);
2008-6-12
{170}
(#4498184@0)
-
When you use condition switch, you actually use batches. In T-SQL batches, the following rule must be followedCREATE DEFAULT, CREATE FUNCTION, CREATE PROCEDURE, CREATE RULE, CREATE TRIGGER, and CREATE VIEW statements cannot be combined with other statements in a batch. The CREATE statement must begin the batch. All other statements that follow in that batch will be interpreted as part of the definition of the first CREATE statement.
Since CREATE VIEW statement must include select statement, you cannot directly use it. The
workaround is to use dynamic SQL.
-deep_blue(BLUE);
2008-6-13
{464}
(#4499373@0)