视图创建问题

jasonhxm 2010-10-28 09:29:59
declare @cut int
set @cut = (select count(*) as cut from sys.views where name = 'abc')
if @cut > 0
print 'c'
else
create view abc as select * from goods

我的目的是判断库中是否存在视图'abc',若不存在就创建。但是这样写报错,

消息 156,级别 15,状态 1,第 6 行
关键字 'view' 附近有语法错误。

请大虾帮忙指点。谢谢!
...全文
53 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jasonhxm 2010-10-28
  • 打赏
  • 举报
回复
谢谢~~
SQLCenter 2010-10-28
  • 打赏
  • 举报
回复
declare @cut int
set @cut = (select count(*) as cut from sys.views where name = 'abc')
if @cut > 0
print 'c'
else
exec ('create view abc as select * from goods')
jasonhxm 2010-10-28
  • 打赏
  • 举报
回复
在库中发现存在abc,就什么都不做(不用drop),不存在abc再创建。是这样个意思。
-晴天 2010-10-28
  • 打赏
  • 举报
回复
create 语句应该是批的第一条语句吧.
dawugui 2010-10-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 liangck 的回复:]
SQL code
if exists(select * from sys.views where name='abc')
drop view abc;
go
create view as select * from goods;
go
[/Quote]就这个。
liangCK 2010-10-28
  • 打赏
  • 举报
回复
if exists(select * from sys.views where name='abc')
drop view abc;
go
create view as select * from goods;
go

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧