关于 MS sql的一个关于动态sql语句的执行顺序问题!

yuxh81 2010-10-27 03:39:18
要实现的功能:
检查数据库是否存在,若不存在则创建数据库,并使用数据库


if not exists(select * from master.dbo.sysdatabases where name = 'db_test')
begin
exec
('create database db_test
go
use db_test
');
end


语句很简单吧!可就是通不过!报错!请大家帮忙!


服务器: 消息 105,级别 15,状态 1,行 21
字符串 'create database db_test
' 之前有未闭合的引号。
服务器: 消息 170,级别 15,状态 1,行 21
第 21 行: 'create database db_test
' 附近有语法错误。
服务器: 消息 911,级别 16,状态 1,行 1
未能在 sysdatabases 中找到数据库 'db_test' 所对应的条目。没有找到具有该名称的条目。请确保正确地输入了名称。

...全文
222 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuxh81 2010-10-28
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 abuying 的回复:]

引用 18 楼 yuxh81 的回复:

引用 17 楼 songguozhi 的回复:

引用 13 楼 yuxh81 的回复:
奇怪的是,begin 和 end 之间的代码并没有错!单独可以执行成功!

create database db_test
go
use db_test
go


难道是 if begin end 不是这样用的!!??

楼主,Go表示……
[/Quote]

谢谢。。

30秒应该可以保证执行成功,可要执行的代码块很长(数百行),里面有很多go啊(数十个)!
abuying 2010-10-28
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 yuxh81 的回复:]

引用 17 楼 songguozhi 的回复:

引用 13 楼 yuxh81 的回复:
奇怪的是,begin 和 end 之间的代码并没有错!单独可以执行成功!

create database db_test
go
use db_test
go


难道是 if begin end 不是这样用的!!??

楼主,Go表示一个批处理语句结束了,If代码断只能位于一个批……
[/Quote]

把GO 改成

WAITFOR DELAY '00:00:30'
--延迟30秒执行下面的SQL语句



yuxh81 2010-10-28
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 songguozhi 的回复:]

引用 13 楼 yuxh81 的回复:
奇怪的是,begin 和 end 之间的代码并没有错!单独可以执行成功!

create database db_test
go
use db_test
go


难道是 if begin end 不是这样用的!!??

楼主,Go表示一个批处理语句结束了,If代码断只能位于一个批处理之内,你恰恰整反了
[/Quote]

你说的是应该对的,问题就出在Go!
但关键是这里又需要有执行顺序,且前面执行完了才能执行后面的
不知道有没有什么办法做到?



其实我是在做网站打包(InstallShiled),并实现安装时自动创建数据库并初始化数据
如果不行,我估计要用附加的方式了。。。
songguozhi 2010-10-27
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 yuxh81 的回复:]
奇怪的是,begin 和 end 之间的代码并没有错!单独可以执行成功!

create database db_test
go
use db_test
go


难道是 if begin end 不是这样用的!!??
[/Quote]
楼主,Go表示一个批处理语句结束了,If代码断只能位于一个批处理之内,你恰恰整反了
yjlhch 2010-10-27
  • 打赏
  • 举报
回复
是不能用go 的
yuxh81 2010-10-27
  • 打赏
  • 举报
回复
在 begin 和 end 之间的sql语句是要求有执行顺序的
前面执行完了才执行后面的,分号好像达不到这个效果

sql代码块单独执行是没有任何问题的,放到 begin 和 end 里就不行了!


但感觉问题就出在go那里!!急!!
王向飞 2010-10-27
  • 打赏
  • 举报
回复
if not exists(select * from master.dbo.sysdatabases where name = 'db_test')
begin
create database db_test;
exec ('use db_test;')
create table tt(id int identity(1,1) primary key ,name varchar(20));

insert into tt select 'zhang';
select * from tt;

drop table tt;

end
yuxh81 2010-10-27
  • 打赏
  • 举报
回复
奇怪的是,begin 和 end 之间的代码并没有错!单独可以执行成功!

create database db_test
go
use db_test
go


难道是 if begin end 不是这样用的!!??
yuxh81 2010-10-27
  • 打赏
  • 举报
回复

if not exists(select * from master.dbo.sysdatabases where name = 'db_test')
begin

create database db_test
go
use db_test
go

end


我本来也是这样做的,但也会报错:

服务器: 消息 170,级别 15,状态 1,行 18
第 18 行: 'db_test' 附近有语法错误。
服务器: 消息 911,级别 16,状态 1,行 2
未能在 sysdatabases 中找到数据库 'db_test' 所对应的条目。没有找到具有该名称的条目。请确保正确地输入了名称。
服务器: 消息 156,级别 15,状态 1,行 4
在关键字 'end' 附近有语法错误。
王向飞 2010-10-27
  • 打赏
  • 举报
回复
对呀 ,你试试。
yuxh81 2010-10-27
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 wxf163 的回复:]

if not exists(select * from master.dbo.sysdatabases where name = 'db_test')
create database db_test
go
use db_test
go
你就直接写 他也是按照这个顺序执行的。
[/Quote]

你是这个意思吧:

if 数据库不存在
begin
//直接把sql语句丢这里,不用exec
end
王向飞 2010-10-27
  • 打赏
  • 举报
回复
if not exists(select * from master.dbo.sysdatabases where name = 'db_test')
create database db_test
go
use db_test
go
你就直接写 他也是按照这个顺序执行的。
SQLCenter 2010-10-27
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 yuxh81 的回复:]

先谢谢大家了!

我说明一下我的用意:

在创建数据库前,检查数据库是否存在,不存在则创建之,并建立表、存储过程、初始化数据等
这些是一大段sql语句,要求执行有先后顺序(前面语句执行完了才执行后面的,就是go的功能)
[/Quote]

没事的,exec('...')完了,就相当于有“go”,exec里面不能用 GO, 因为EXEC本身就被当成一个批处理。
dawugui 2010-10-27
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 yuxh81 的回复:]
先谢谢大家了!

我说明一下我的用意:

在创建数据库前,检查数据库是否存在,不存在则创建之,并建立表、存储过程、初始化数据等
这些是一大段sql语句,要求执行有先后顺序(前面语句执行完了才执行后面的,就是go的功能)
[/Quote]
如果是动态执行。
把相关顺序写好即可,不需要使用go
SQLCenter 2010-10-27
  • 打赏
  • 举报
回复
而且你在 exec 里面 use db_test 对于 exec 外面是没有用的:

exec ('use tempdb')
yuxh81 2010-10-27
  • 打赏
  • 举报
回复
先谢谢大家了!

我说明一下我的用意:

在创建数据库前,检查数据库是否存在,不存在则创建之,并建立表、存储过程、初始化数据等
这些是一大段sql语句,要求执行有先后顺序(前面语句执行完了才执行后面的,就是go的功能)
dawugui 2010-10-27
  • 打赏
  • 举报
回复
exec中不需要使用go
if not exists(select 1 from master.dbo.sysdatabases where name = 'db_test')
begin
exec ('create database db_test')
use db_test
end
heymal 2010-10-27
  • 打赏
  • 举报
回复
连接字符串中去掉 go
SQLCenter 2010-10-27
  • 打赏
  • 举报
回复
exec 里面不用用 go

if not exists(select * from master.dbo.sysdatabases where name = 'db_test')
begin
exec
('create database db_test');
end
go

use db_test
xjsujin 2010-10-27
  • 打赏
  • 举报
回复
'create database db_test '+
'go'+
'use db_test'+
);
这样试试?

34,587

社区成员

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

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