创建表在循环中失败(SQL Server)

时间:2016-04-19 15:39:24

标签: sql sql-server

我有一个sql脚本,可以为我的sql server中的每个db创建表。但是它在一个create table脚本上失败了。例如

CREATE TABLE [dbo].[Mx_Poll_Tags] 

命令。这有什么问题?我没有看到它。

奇怪的是,当我逐个在查询窗口中运行脚本时,它们都可以正常工作。只有这个特殊的过程失败了。

错误说明:

  

Msg 173,Level 15,State 1,Line 71
  “单元”列的定义必须包含数据类型。

创建脚本由SQL Server自己编写,将现有Mx_Poll_Tags表编写为CreateTo

有人看到错误是什么吗?

BEGIN
declare @proc nvarchar(max)
set @proc='if ''?'' like ''Client_%''

begin 
    use [?]
    print ''?''

    DROP TABLE [dbo].[ManualMetersInput]
    DROP TABLE [dbo].[ManualMeterActions]
    DROP TABLE [dbo].[ManualMeters]

        DROP TABLE [dbo].[MX_Poll]
    --DROP TABLE [dbo].[Mx_Poll_Tags]
    DROP TABLE [dbo].[MX_Poll_Info]
    DROP TABLE [dbo].[MX_Poll_Logs]
    DROP TABLE [dbo].[MX_Poll_QA]
    --DROP TABLE [dbo].[MX_Poll_Vars]

    CREATE TABLE [dbo].[ManualMeters]
    (
        [Id] [int] IDENTITY(1,1) NOT NULL,
        [Ean] [varchar](max) NULL,
        [Period] [int] NULL,       
        [TagTable] [varchar](max) NULL,
        [TagTableId] [varchar](max) NOT NULL,
        [Overflow] [int] NULL,
        [TZ] [varchar](max) NULL,
    ) ON [PRIMARY] 

    CREATE TABLE [dbo].[ManualMetersInput]
    (
        [Id] [int] IDENTITY(1,1) NOT NULL,
        [Timestamp] [datetime2](7) NOT NULL,
        [ManualMeterId] [int] NOT NULL,
        [Value] [decimal](18, 3) NOT NULL,
        [IsOverflow] [bit] NOT NULL,
        [ImportDate] [datetime2](7) NOT NULL,
    ) ON [PRIMARY] 

    CREATE TABLE [dbo].[ManualMeterActions]
    (
        [Id] [int] IDENTITY(1,1) NOT NULL,
        [ManualMeterId] [int] NOT NULL,
        [UserId] [int] NOT NULL,
        [Type] [nvarchar](max) NOT NULL,
    ) ON [PRIMARY]

    CREATE TABLE [dbo].[MX_Poll]
    (
        [timestamp] [datetime2](7) NOT NULL,
        [localtimestamp] [datetime2](7) NOT NULL,
    ) ON [PRIMARY] 

    CREATE TABLE [dbo].[MX_Poll_Info]
    (
        [timestamp] [datetime2](7) NOT NULL,
        [info] [nvarchar](max) NOT NULL,
    ) ON [PRIMARY]

    CREATE TABLE [dbo].[MX_Poll_Logs]
    (
        [Id] [int] IDENTITY(1,1) NOT NULL,
        [timestamp] [datetime2](7) NOT NULL,
        [Message] [nvarchar](max) NULL,
    ) ON [PRIMARY]

    CREATE TABLE [dbo].[MX_Poll_QA]
    (
        [Id] [int] IDENTITY(1,1) NOT NULL,
        [timestamp] [datetime2](7) NOT NULL,
        [tag] [nvarchar](max) NULL,
        [QA] [int] NULL
    ) ON [PRIMARY] 

    -- error here    
    CREATE TABLE [dbo].[Mx_Poll_Tags]
    (
        [Id] [int] IDENTITY(1,1) NOT NULL,
        [Name] [nvarchar](800) NOT NULL,
        [Unit] [int] NOT NULL,
        [FieldName] [nvarchar](100) NULL,
        [ScaleFromMin] [decimal](18, 3) NULL,
        [ScaleFromMax] [decimal](18, 3) NULL,
        [ScaleToMin] [decimal](18, 3) NULL,
        [ScaleToMax] [decimal](18, 3) NULL,
        [DeltaOfKwhCounter_Id] [int] NULL,
        [Visible] [int] NULL,
        [Type] [nvarchar](50) NULL,
        [Enable] [int] NULL,
        [Content] [int] NULL,
        [Quantity] [int] NULL,
        [Signal] [int] NULL,
        [SignalDescription] [nvarchar](max) NULL,
        [Connection] [nvarchar](max) NULL,
        [Cable] [nvarchar](max) NULL,
        [Comments] [nvarchar](max) NULL,
        [UsedForPrediction_0] [bit] NOT NULL,
        [RelatedToPrediction_0] [bit] NOT NULL,
        [CalculatedByPredictionNo] [int] NULL,
    ) ON [PRIMARY] 

end';

--print @proc;
exec sp_MSForEachDB @proc

END
GO

1 个答案:

答案 0 :(得分:4)

你可以尝试只创建那张桌子吗?

另外,你可以消除“开始”和“结束”吗?基于这个帖子:http://www.sqlservercentral.com/Forums/Topic808714-8-1.aspx

我没有足够的声誉来发表评论。

修改

默认情况下,sys.sp_MSforeachdb @ command1的参数长度为nvarchar(2000)。即使你传入一个varchar(max),超过2000的任何东西都会被截断。