t-sql if exists begin end 报错

zhujinpengliang 2013-10-30 05:01:54
use master
go
if exists(select * from sysdatabases where name='ttt')
begin
print '已经存在'
end
else
begin
use master
go
create database ttt
go
use ttt
if exists(select * from sysobjects where name='T_News' and xtype ='U')
begin
print '该表已经存在'
end
else
begin
create table T_News (
ID int not null ,
Title varchar(50) not null,
passwd varchar not null
)
end
end


////////
消息 102,级别 15,状态 1,第 7 行
'master' 附近有语法错误。
消息 102,级别 15,状态 1,第 14 行
'end' 附近有语法错误。
...全文
440 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhujinpengliang 2013-10-30
  • 打赏
  • 举报
回复
多谢各位
發糞塗牆 2013-10-30
  • 打赏
  • 举报
回复
我个人比较偏向这种,可以运行的,不存在才创建,可以反复运行
use master     
go
IF db_id('ttt')IS  NULL
CREATE DATABASE ttt
go
USE ttt
go
IF OBJECT_ID('T_News','u')IS  NULL
CREATE TABLE T_News
(
 ID int not null ,
Title varchar(50) not null,
passwd varchar not null
    ) 
zhujinpengliang 2013-10-30
  • 打赏
  • 举报
回复
引用 3 楼 DBA_Huangzj 的回复:
[quote=引用 2 楼 hdhai9451 的回复:]
use master     
go

if exists(select * from sysdatabases where name='ttt')  
begin
print '已经存在'
end
else
begin
    --use master
    --go
	create database ttt
	--go
	--use ttt
	if exists(select * from sysobjects where name='T_News' and xtype ='U') 
	begin
	   print '该表已经存在'     
	end
	else
	begin
		create table  T_News (
		 ID int not null ,
		Title varchar(50) not null,
		passwd varchar not null
			) 
	end
end
要去掉四行才执行.
但是T_news就创建在master里面了,本身他这个脚本就不合理[/quote] 请教一下要实现我这个逻辑要求该怎么写合理。
-Tracy-McGrady- 2013-10-30
  • 打赏
  • 举报
回复

  if exists(select * from sys.databases where name='ttt') 
    print '数据库已经存在'
  else
    create database ttt
  go
  use ttt
  go
  if exists(select * from sys.objects where name='T_News' and type ='U') 
    print '该表已经存在于ttt数据库' 	    
  else
    create table T_News(ID int not null,Title varchar(50) not null,passwd varchar not null) 
  go
  
發糞塗牆 2013-10-30
  • 打赏
  • 举报
回复
引用 2 楼 hdhai9451 的回复:
use master     
go

if exists(select * from sysdatabases where name='ttt')  
begin
print '已经存在'
end
else
begin
    --use master
    --go
	create database ttt
	--go
	--use ttt
	if exists(select * from sysobjects where name='T_News' and xtype ='U') 
	begin
	   print '该表已经存在'     
	end
	else
	begin
		create table  T_News (
		 ID int not null ,
		Title varchar(50) not null,
		passwd varchar not null
			) 
	end
end
要去掉四行才执行.
但是T_news就创建在master里面了,本身他这个脚本就不合理
Andy__Huang 2013-10-30
  • 打赏
  • 举报
回复
use master     
go

if exists(select * from sysdatabases where name='ttt')  
begin
print '已经存在'
end
else
begin
    --use master
    --go
	create database ttt
	--go
	--use ttt
	if exists(select * from sysobjects where name='T_News' and xtype ='U') 
	begin
	   print '该表已经存在'     
	end
	else
	begin
		create table  T_News (
		 ID int not null ,
		Title varchar(50) not null,
		passwd varchar not null
			) 
	end
end
要去掉四行才执行.
Andy__Huang 2013-10-30
  • 打赏
  • 举报
回复
begin 和 end 之间不能用go执行语句
内容概要:本文档为一份《计算机程序设计语言(C++)》课程的实验报告,涵盖了从基础语法到面向对象编程的多个实验内容。实验内容包括基础语法练习(如银行存款计算、数值比较、科学计算)、控制结构(分支与循环,如闰年判断、素数判定、Kaprekar黑洞数)、数组操作(一维与二维数组的排序、查找、极值计算、杨辉三角)、函数定义与应用(递归、参数传递、函数重载)以及面向对象编程(类与对象的设计、继承、多态性、文件操作)。文档详细展示了每个实验的任务描述、具体C++代码实现、测试结果及实验分析,体现了从简单到复杂、逐步深入的学习路径。; 适合人群:具备C++基础语法知识,正在学习或巩固程序设计课程的在校大学生及初级编程学习者。; 使用场景及目标:①作为C++课程的配套实验指导材料,帮助学生通过实践掌握编程基础知识和核心算法;②用于自学练习,提升对循环、数组、函数和面向对象等核心概念的理解与编码能力。; 阅读建议:学习者应结合文档中的代码实例,动手在编程环境中逐一实现并调试,重点关注代码逻辑、边界条件处理和算法效率,通过对比预期输出与实际输出加深对知识点的掌握。

34,875

社区成员

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

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