2016-12-08 51 views
-1

我想用存儲過程創建一個表。我將表格作爲CREATE語句編寫,並將文件內容複製到我的過程中,這很好。除了設置默認值時,它將第一列默認設置爲整個第二行。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] 
') 

執行後,列[fldName]FOR [fldName] ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'' FOR [fldPath]

默認值這是有道理的,它只是不承認在(N「」)的第二個引號,但爲什麼不?我該如何解決它?

UPDATE:

我已經瞭解到,創建表的正確方法是通過閱讀和執行SQL腳本文件。這使數據操作和數據庫操作保持獨立,並且更易於管理。

+0

標籤您正在使用的數據庫管理系統。 (很多產品特定的SQL ...) – jarlh

+0

看起來像SQL服務器 –

+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] 
') 

目前尚不清楚(至少對我來說)你爲什麼做這種方式雖然,而不是隻:

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] 

也不清楚爲什麼要創建內表一個過程,特別是在不檢查它是否已經存在的情況下 - 你能保證這個表在每次你的程序運行時都不會存在嗎?

+0

我在另一個程序中檢查了我的代碼,以及其他4個表。 – beardedmogul

+0

有很多關於你在問什麼,看起來你在做什麼看起來不是很好的做法。即使您在調用此存儲過程之前在其他位置檢查表的存在,但事先可以調用此存儲過程而不先檢查該表是否已存在並不是件好事。如果你要創建一個表,並且它有可能已經存在,你應該在相同的範圍內進行檢查,這樣當表已經存在時,就不可能嘗試創建表。 – 3N1GM4

+0

夠公平的。我很好奇,爲什麼使用存儲過程創建表不是好習慣。 – beardedmogul

0

不是說是做通過存儲過程創建一個表正確的事情,但...你需要逃避你的報價:如果您在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] 
') 
+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] 
')`