创建存储过程语句的错误

puhaohmaru 2007-08-20 08:54:38
我使用一下的语句创建存储过程,为什么会不对?如果要实现我想要的结果,该怎么写?谢谢!
if exists(select * from sysobjects where name='prHello' and type='p')
begin
alter procedure prHello
as
select 'alter'
end
else
begin
create procedure prHello
as
select 'create'
end

错误提示:
服务器: 消息 156,级别 15,状态 1,行 3
在关键字 'procedure' 附近有语法错误。
服务器: 消息 156,级别 15,状态 1,行 9
在关键字 'procedure' 附近有语法错误。
...全文
243 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
zp_cool 2007-08-20
  • 打赏
  • 举报
回复
鱼兄正解
puhaohmaru 2007-08-20
  • 打赏
  • 举报
回复
呵呵,谢谢,明白了
多谢paoluo兄和chuifengde兄
结贴了
chuifengde 2007-08-20
  • 打赏
  • 举报
回复
要是单引号弄混了可以用引号,也可以这么写:
set quoted_identifier off

if exists(select * from sysobjects where name='prHello' and type='p')
begin
exec("
alter procedure prHello
as
select 'alter'")
end
else
begin
exec("create procedure prHello
as
select 'create'")
end
chuifengde 2007-08-20
  • 打赏
  • 举报
回复
我的第一次执行能通过,第二次执行就通不过了,因为alter里面多了一个end,呵呵,鱼的是正确的
paoluo 2007-08-20
  • 打赏
  • 举报
回复
puhaohmaru(等我成功) ??難道我寫的沒有通過?我還是先貼出來的?

拼結動態語句的時候,用兩個單引號替換原來的一個單引號。
puhaohmaru 2007-08-20
  • 打赏
  • 举报
回复
谢谢chuifengde,可以通过

可以解释一下那几个单引号吗?

没有看太明白几个单引号的意义,谢谢!
chuifengde 2007-08-20
  • 打赏
  • 举报
回复
不好意思,括进去一个,谢鱼兄:)。
paoluo 2007-08-20
  • 打赏
  • 举报
回复
chuifengde(树上的鸟儿) , 你的多弄了個end.
chuifengde 2007-08-20
  • 打赏
  • 举报
回复
用动态实现吧,一个批处理中不能与别的语句组合
if exists(select * from sysobjects where name='prHello' and type='p')
begin

exec('alter procedure prHello
as
select ''alter''
end')
end
else
begin
exec('create proc prHello
as
select ''create''')
end
puhaohmaru 2007-08-20
  • 打赏
  • 举报
回复
dawugui:
不行啊,出错

服务器: 消息 170,级别 15,状态 1,行 2
第 2 行: 'begin' 附近有语法错误。
服务器: 消息 156,级别 15,状态 1,过程 prHello,行 4
在关键字 'end' 附近有语法错误。
服务器: 消息 156,级别 15,状态 1,过程 prHello,行 4
在关键字 'end' 附近有语法错误。
paoluo 2007-08-20
  • 打赏
  • 举报
回复
加go肯定是不行的,沒經過測試.
paoluo 2007-08-20
  • 打赏
  • 举报
回复
if exists(select * from sysobjects where name='prHello' and type='p')
begin
exec('
alter procedure prHello
as
select ''alter''')
end
else
begin
exec('create procedure prHello
as
select ''create''')
end
dawugui 2007-08-20
  • 打赏
  • 举报
回复
if exists(select * from sysobjects where name='prHello' and type='p')
begin
go
alter procedure prHello
as
select 'alter'
end
else
begin
go
create procedure prHello
as
select 'create'
end

34,591

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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