1

I have a sql script that creates tables for each db in my sql server. However it fails on one create table script. Eg the

CREATE TABLE [dbo].[Mx_Poll_Tags] 

command. What is wrong with this? I don't see it.

The weird part is when I one by one run the scripts in a query window they all work fine. Only is this particular proc it fails.

The error states:

Msg 173, Level 15, State 1, Line 71
The definition for column 'Unit' must include a data type.

The create script is made by SQL Server itself by scripting an existing Mx_Poll_Tags table as CreateTo.

Does anyone see what the error is?

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
6
  • You do have the drop table statement commented out, though I would've expected a different error message... Commented Apr 19, 2016 at 15:43
  • Yeah, I commented it out because the table doesn't exist yet and else the drop gives an error. So that has nothing to do with the error in my question.. Commented Apr 19, 2016 at 15:44
  • Based on the error message I would have expected there to be a text row like this [Unit] NOT NULL, where the data type was missing. Are you sure that the generated error message does not correspond to a query where this is indeed left off/omitted? Could happen as a copy/past error or maybe editing the query, something like that. Commented Apr 19, 2016 at 15:50
  • You could also take a look at the result of the print @proc. I can't really imagine you loose your datatype there, but it's an easy test. Commented Apr 19, 2016 at 15:57
  • Is unit a keyword by any chance? Something which the compiler sees differently in this case? Try units Commented Apr 19, 2016 at 16:06

1 Answer 1

4

Can you try it with just creating that table?

Also, can you eliminate the "begin" and "end"? Based on this thread: http://www.sqlservercentral.com/Forums/Topic808714-8-1.aspx

I don't have enough reputation to make this a comment.

EDIT:

By default, sys.sp_MSforeachdb @command1 has a parameter length of nvarchar(2000). Even though you're passing in a varchar(max), anything over 2000 is being truncated.

Sign up to request clarification or add additional context in comments.

6 Comments

That doesn't help either. The weird part is when I copy the create script directly in de query window it does work. Only not when it's in that loop. I shall add that to the description of my problem.
I think it could be the length of your @proc variable. Can you reduce the proc down to just the exec sp_msforeach with your table. I believe by default, sp_msforeachdb has a max character length.
You are right, except the query would be truncated somewhere in the CREATE TABLE [dbo].[MX_Poll_QA] statement. So the error is in the wrong statement, it seems...
When I paste the query into word it is 2,184 characters. The 2000th character is after Comments in the Mx_Poll_Tags Create Table statement. Seems like this answer is valid.
@ArthurDaniels; you're right. That was the issue. I've split it up and now it works as expected. Lesson learned :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.