我想创建一个新数据库并用一些表填充它。它创建一个新数据库,但这些表出现在另一个数据库中。
@@DbName
包含用户提供的数据库的名称,它创建数据库的事实表明DbName是正确的。执行时,数据库已创建但为空,所有表都将插入另一个数据库中。
我认为错误在于USE [@@DbName]
我做错了什么?
我的SQL语句:
CREATE DATABASE [@@DbName]
ON PRIMARY
( NAME = [@@DbName],
FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\@@DbName.mdf',
SIZE = 5 MB,
MAXSIZE = UNLIMITED,
FILEGROWTH = 1 MB )
LOG ON
( NAME = [@@DbName_log],
FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\@@DbName_log.ldf',
SIZE = 1 MB,
MAXSIZE = 2097152 MB,
FILEGROWTH = 10 % )
COLLATE Latin1_General_CI_AS
GO
CREATE LOGIN [@@DbLogin] WITH PASSWORD=N'@@DbPassword', DEFAULT_DATABASE=[@@DbName], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
GO
USE [@@DbName]
GO
CREATE TABLE [dbo].[Customers]
(
[ID] int IDENTITY(1, 1) NOT NULL,
[CustomerName] varchar(50) COLLATE Latin1_General_CI_AS NOT NULL,
[Address] varchar(50) COLLATE Latin1_General_CI_AS NOT NULL,
[Picture] image NOT NULL,
[Balance] decimal(18, 2) NOT NULL
)
ON [PRIMARY]
TEXTIMAGE_ON [PRIMARY]
GO
CREATE TABLE [dbo].[Employee]
(
[ID] int IDENTITY(1, 1) NOT NULL,
[EmployeeName] varchar(50) COLLATE Latin1_General_CI_AS NOT NULL,
[Address] varchar(50) COLLATE Latin1_General_CI_AS NOT NULL,
[Email] varchar(50) COLLATE Latin1_General_CI_AS NOT NULL,
[Phone] varchar(20) COLLATE Latin1_General_CI_AS NOT NULL
)
ON [PRIMARY]
GO
CREATE TABLE [dbo].[Registers]
(
[ID] int IDENTITY(1, 1) NOT NULL,
[RegisterName] varchar(50) COLLATE Latin1_General_CI_AS NOT NULL,
[Device] varchar(50) COLLATE Latin1_General_CI_AS NOT NULL
)
ON [PRIMARY]
GO
CREATE TABLE [dbo].[Errorlog]
(
[RegisterID] int NOT NULL,
[Timestamp] varchar(50) COLLATE Latin1_General_CI_AS NOT NULL,
[Message] varchar(255) COLLATE Latin1_General_CI_AS NOT NULL,
[StackTrace] ntext COLLATE Latin1_General_CI_AS NOT NULL
)
ON [PRIMARY]
TEXTIMAGE_ON [PRIMARY]
GO
CREATE TABLE [dbo].[Products]
(
[ID] int IDENTITY(1, 1) NOT NULL,
[ProductName] varchar(50) COLLATE Latin1_General_CI_AS NOT NULL,
[Price] decimal(18, 2) NULL
)
ON [PRIMARY]
GO
CREATE TABLE [dbo].[Register_Employee]
(
[RegisterID] int NOT NULL,
[EmployeeID] int NOT NULL,
[From] varchar(20) COLLATE Latin1_General_CI_AS NOT NULL,
[Until] varchar(20) COLLATE Latin1_General_CI_AS NOT NULL
)
ON [PRIMARY]
GO
CREATE TABLE [dbo].[Sales]
(
[ID] int NOT NULL,
[Timestamp] timestamp NOT NULL,
[CustomerID] int NOT NULL,
[RegisterID] int NOT NULL,
[ProductID] int NOT NULL,
[Amount] int NOT NULL,
[TotalPrice] decimal(18, 2) NOT NULL
)
ON [PRIMARY]
GO