我正在尝试使用存储过程创建一个表。我将表编写为CREATE语句并将文件内容复制到我的过程中,这非常有用。除了设置默认值时,它将第一列默认设置为整个第二行。
这是程序:
CREATE PROCEDURE [dbo].[spCreatetblLocation]
AS
EXEC
('
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
CREATE TABLE [dbo].[tblLocation](
[pkLocationID] [int] IDENTITY(1,1) NOT NULL,
[fldName] [nvarchar](100) NOT NULL,
[fldPath] [nvarchar](1000) NOT NULL,
[fkYearID] [int] NOT NULL,
CONSTRAINT [PK_LocationID] PRIMARY KEY CLUSTERED
(
[pkLocationID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'' FOR [fldName]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'' FOR [fldPath]
')
执行后,列[fldName]
的默认值为FOR [fldName] ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'' FOR [fldPath]
有意义的是,它不会识别(N'')中的第二个引号,但为什么不呢?我该如何解决?
更新
我了解到创建表的正确方法是通过读取和执行sql脚本文件。这使数据操作和数据库操作分离,并且更易于管理。
答案 0 :(得分:1)
如果SQL中的字符串中有撇号,则需要使用附加的撇号来转义它。尝试:
CREATE PROCEDURE [dbo].[spCreatetblLocation]
AS
EXEC
('
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
CREATE TABLE [dbo].[tblLocation](
[pkLocationID] [int] IDENTITY(1,1) NOT NULL,
[fldName] [nvarchar](100) NOT NULL,
[fldPath] [nvarchar](1000) NOT NULL,
[fkYearID] [int] NOT NULL,
CONSTRAINT [PK_LocationID] PRIMARY KEY CLUSTERED
(
[pkLocationID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'''' FOR [fldName]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'''' FOR [fldPath]
')
我不清楚(至少对我来说)为什么你这样做,而不仅仅是:
CREATE PROCEDURE [dbo].[spCreatetblLocation]
AS
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
CREATE TABLE [dbo].[tblLocation](
[pkLocationID] [int] IDENTITY(1,1) NOT NULL,
[fldName] [nvarchar](100) NOT NULL,
[fldPath] [nvarchar](1000) NOT NULL,
[fkYearID] [int] NOT NULL,
CONSTRAINT [PK_LocationID] PRIMARY KEY CLUSTERED
(
[pkLocationID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'' FOR [fldName]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'' FOR [fldPath]
还不清楚为什么要在程序中创建一个表,特别是不检查它是否已经存在 - 你能保证每次运行程序时该表都不存在吗?
答案 1 :(得分:1)
在字符串中必须加倍引号:
CREATE PROCEDURE [dbo].[spCreatetblLocation]
AS
EXEC ('
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
CREATE TABLE [dbo].[tblLocation](
[pkLocationID] [int] IDENTITY(1,1) NOT NULL,
[fldName] [nvarchar](100) NOT NULL,
[fldPath] [nvarchar](1000) NOT NULL,
[fkYearID] [int] NOT NULL,
CONSTRAINT [PK_LocationID] PRIMARY KEY CLUSTERED
(
[pkLocationID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'''' FOR [fldName]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'''' FOR [fldPath]
')`
答案 2 :(得分:0)
不是说通过存储过程创建表是正确的,但是......你需要转义引号:
CREATE PROCEDURE [dbo].[spCreatetblLocation]
AS
EXEC
('
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
CREATE TABLE [dbo].[tblLocation](
[pkLocationID] [int] IDENTITY(1,1) NOT NULL,
[fldName] [nvarchar](100) NOT NULL,
[fldPath] [nvarchar](1000) NOT NULL,
[fkYearID] [int] NOT NULL,
CONSTRAINT [PK_LocationID] PRIMARY KEY CLUSTERED
(
[pkLocationID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'''' FOR [fldName]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'''' FOR [fldPath]
')