All the fields should have default values so nulls are not required. None of the fields have null handling. I try to avoid nulls whenever possible and rely on default values instead since it's a pain to deal with null values in FoxPro.
I thought all the default values are there. It works for session and request log tested. I didn't test the async table though...
+++ Rick ---
Excellent, Rick! I did notice it must have been compiled in when I tried testing your earlier changes, so I was running your revised scripts in SQL.
I see you're still using NOT NULL for the two additional fields so it will still fail for me per my message: http://west-wind.com/wwThreads/Message3OC0Z9B0E.wwt
Michael
Michael,
So I took another look at this and it turns out this is harder to fix than I thought :-)
Problem is that CREATE DEFAULT has to be executed on its own. There can't be any other commands - including conditionals or even a try catch which all doesn't work and which really sucks:
http://msdn.microsoft.com/en-us/library/ms173565.aspx
When you use any other commands you get the error you saw: Unrecognized command DEFAULT...
Soooo, there's no way that I can see check for the default being there and then executing the command (there might be using blocks, but I'm not that familiar with TSQL)...
Anyway. The solution to the problem seems to be to just remove the reference to the default and instead just set the default value of the Cancelled bit field that uses it to 0. This seems to work and have exactly the same effect. The original code had that set this way anyway, but apparently the SQL schema export creates the default for Bit 0 values.
Here's the whole SQL script to create/update the West Wind tables:
SET ANSI_NULLS OFFGOSET QUOTED_IDENTIFIER OFFGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[sp_ww_GetNextEvent]') ANDOBJECTPROPERTY(id,N'IsProcedure') = 1)BEGINEXEC dbo.sp_executesql @statement = N'CREATEPROCEDURE [dbo].[sp_ww_GetNextEvent] @lctype char(15)ASSETTRANSACTIONISOLATIONLEVELREPEATABLEREADBEGINTRANSACTIONDeclare @lcID char(15)SELECTTOP 1 @lcID=ID FROM wwasyncwebrequestWHERE started < ''01/05/2000'' and completed < ''01/05/2000'' and type = @lcType /* ORDER BY submitted DESC */if @@ROWCOUNT> 0beginUPDATE wwasyncwebrequestSET started = GetDate()WHERE ID = @lcIDSELECT * FROM wwasyncwebrequestWHERE ID = @lcIDendelseSELECT * from wwasyncwebrequest where ID = ''xxxxxxx''COMMITTRANSACTION 'ENDGO/****** Object: StoredProcedure [dbo].[sp_ww_IsSession] Script Date: 12/26/2012 7:38:27 PM ******//SET ANSI_NULLS OFFGOSET QUOTED_IDENTIFIER OFFGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[sp_ww_IsSession]') ANDOBJECTPROPERTY(id,N'IsProcedure') = 1)BEGINEXEC dbo.sp_executesql @statement = N'CREATEPROCEDURE [dbo].[sp_ww_IsSession] @lcSessionId char(14), @ldTimeout DateTime, @llIsSession bit OUTPUTASSELECT SessionId FROM wwSession (NOLOCK) WHERE SessionId= @lcSessionId AND LastOn > @ldTimeoutif @@ROWCOUNT> 0Select @llIsSession = 1elseSelect @llIsSession = 0 'ENDGO/****** Object: StoredProcedure [dbo].[sp_ww_LocateSession] Script Date: 12/26/2012 7:38:27 PM ******//SET ANSI_NULLS OFFGOSET QUOTED_IDENTIFIER OFFGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[sp_ww_LocateSession]') ANDOBJECTPROPERTY(id,N'IsProcedure') = 1)BEGINEXEC dbo.sp_executesql @statement = N'CREATEPROCEDURE [dbo].[sp_ww_LocateSession] @lcSessionId char(14), @ldTimeout DateTimeASSELECT * FROM wwSession (NOLOCK) WHERE SessionId= @lcSessionId AND LastOn > @ldTimeout 'ENDGO/****** Object: StoredProcedure [dbo].[sp_ww_NewID] Script Date: 12/26/2012 7:38:27 PM ******//SET ANSI_NULLS OFFGOSET QUOTED_IDENTIFIER OFFGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[sp_ww_NewID]') ANDOBJECTPROPERTY(id,N'IsProcedure') = 1)BEGINEXEC dbo.sp_executesql @statement = N'CREATEPROCEDURE [dbo].[sp_ww_NewID] @cName char(30), @nRetval int OUTPUTASUPDATE wws_Id SET id = id + 1, @nRetval = id + 1WHERE TableName = @cName 'ENDGO/****** Object: Table [dbo].[wwasyncwebrequest] Script Date: 12/26/2012 7:38:27 PM ******//SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[wwasyncwebrequest]') ANDOBJECTPROPERTY(id, N'IsUserTable') = 1)BEGINCREATETABLE [dbo].[wwasyncwebrequest]( [id] [char](15) NOTNULL, [title] [char](60) NOTNULL, [returndata] [text] NOTNULL, [inputdata] [text] NOTNULL, [ctype] [text] NOTNULL, [type] [char](15) NOTNULL, [submitted] [datetime] NOTNULL, [started] [datetime] NOTNULL, [completed] [datetime] NOTNULL, [expire] [int] NOTNULL, [status] [text] NOTNULL, [username] [char](20) NOTNULL, [chkcounter] [int] NOTNULL, [cancelled] [bit] NOTNULL, [properties] [text] NOTNULL, [timestamp_column] [timestamp] NULL,CONSTRAINT [PK_wwasyncwebrequest] PRIMARYKEYCLUSTERED ( [id] ASC ) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]ENDGOSET ANSI_PADDING OFFGO/****** Object: Table [dbo].[wwrequestlog] Script Date: 12/26/2012 7:38:27 PM ******//SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[wwrequestlog]') ANDOBJECTPROPERTY(id, N'IsUserTable') = 1)BEGINCREATETABLE [dbo].[wwrequestlog]( [time] [datetime] NOTNULL, [reqid] [varchar](20) NOTNULL, [script] [varchar](50) NOTNULL, [querystr] [varchar](1024) NOTNULL, [duration] [numeric](7, 3) NOTNULL, [remoteaddr] [varchar](16) NOTNULL, [memused] [char](8) NOTNULL, [error] [bit] NOTNULL, [reqdata] [text] NOTNULL, [browser] [varchar](256) NOTNULL, [referrer] [varchar](256) NOTNULL, [result] [text] NOTNULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]ENDGOSET ANSI_PADDING OFFGO/****** Object: Table [dbo].[wws_id] Script Date: 12/26/2012 7:38:27 PM ******//SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[wws_id]') ANDOBJECTPROPERTY(id, N'IsUserTable') = 1)BEGINCREATETABLE [dbo].[wws_id]( [tablename] [varchar](50) NOTNULL, [id] [int] NOTNULL ) ON [PRIMARY]ENDGOSET ANSI_PADDING OFFGO/****** Object: Table [dbo].[wwsession] Script Date: 12/26/2012 7:38:27 PM ******//SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[wwsession]') ANDOBJECTPROPERTY(id, N'IsUserTable') = 1)BEGINCREATETABLE [dbo].[wwsession]( [SessionID] [char](14) NOTNULL, [UserId] [char](15) NOTNULL, [firston] [datetime] NOTNULL, [laston] [datetime] NOTNULL, [vars] [text] NOTNULL, [browser] [text] NOTNULL, [ip] [text] NULL, [hits] [int] NOTNULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]ENDGOSET ANSI_PADDING OFFGOSET ANSI_PADDING ONGO/****** Object: Index [SessionID] Script Date: 12/26/2012 7:38:27 PM ******//IFNOTEXISTS (SELECT * FROM dbo.sysindexesWHERE id = OBJECT_ID(N'[dbo].[wwsession]') AND name = N'SessionID')CREATECLUSTEREDINDEX [SessionID] ON [dbo].[wwsession] ( [SessionID] ASC ) ON [PRIMARY]GOSET ANSI_PADDING ONGO/****** Object: Index [id] Script Date: 12/26/2012 7:38:27 PM ******//IFNOTEXISTS (SELECT * FROM dbo.sysindexesWHERE id = OBJECT_ID(N'[dbo].[wwasyncwebrequest]') AND name = N'id')CREATENONCLUSTEREDINDEX [id] ON [dbo].[wwasyncwebrequest] ( [id] ASC ) ON [PRIMARY]GO/****** Object: Index [started] Script Date: 12/26/2012 7:38:27 PM ******//IFNOTEXISTS (SELECT * FROM dbo.sysindexesWHERE id = OBJECT_ID(N'[dbo].[wwasyncwebrequest]') AND name = N'started')CREATENONCLUSTEREDINDEX [started] ON [dbo].[wwasyncwebrequest] ( [started] ASC ) ON [PRIMARY]GOSET ANSI_PADDING ONGO/****** Object: Index [type] Script Date: 12/26/2012 7:38:27 PM ******//IFNOTEXISTS (SELECT * FROM dbo.sysindexesWHERE id = OBJECT_ID(N'[dbo].[wwasyncwebrequest]') AND name = N'type')CREATENONCLUSTEREDINDEX [type] ON [dbo].[wwasyncwebrequest] ( [type] ASC ) ON [PRIMARY]GO/****** Object: Index [LastOn] Script Date: 12/26/2012 7:38:27 PM ******//IFNOTEXISTS (SELECT * FROM dbo.sysindexesWHERE id = OBJECT_ID(N'[dbo].[wwsession]') AND name = N'LastOn')CREATENONCLUSTEREDINDEX [LastOn] ON [dbo].[wwsession] ( [laston] ASC ) ON [PRIMARY]GOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwasyncwebrequest_title]') AND type = 'D')BEGINALTERTABLE [dbo].[wwasyncwebrequest] ADDCONSTRAINT [DF_wwasyncwebrequest_title] DEFAULT ('') FOR [title]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwasyncwebrequest_returndata]') AND type = 'D')BEGINALTERTABLE [dbo].[wwasyncwebrequest] ADDCONSTRAINT [DF_wwasyncwebrequest_returndata] DEFAULT ('') FOR [returndata]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwasyncwebrequest_inputdata]') AND type = 'D')BEGINALTERTABLE [dbo].[wwasyncwebrequest] ADDCONSTRAINT [DF_wwasyncwebrequest_inputdata] DEFAULT ('') FOR [inputdata]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwasyncwebrequest_ctype]') AND type = 'D')BEGINALTERTABLE [dbo].[wwasyncwebrequest] ADDCONSTRAINT [DF_wwasyncwebrequest_ctype] DEFAULT ('') FOR [ctype]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwasyncwebrequest_type]') AND type = 'D')BEGINALTERTABLE [dbo].[wwasyncwebrequest] ADDCONSTRAINT [DF_wwasyncwebrequest_type] DEFAULT ('') FOR [type]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwasyncwebrequest_expire]') AND type = 'D')BEGINALTERTABLE [dbo].[wwasyncwebrequest] ADDCONSTRAINT [DF_wwasyncwebrequest_expire] DEFAULT ((900)) FOR [expire]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwasyncwebrequest_status]') AND type = 'D')BEGINALTERTABLE [dbo].[wwasyncwebrequest] ADDCONSTRAINT [DF_wwasyncwebrequest_status] DEFAULT ('STARTED') FOR [status]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwasyncwebrequest_username]') AND type = 'D')BEGINALTERTABLE [dbo].[wwasyncwebrequest] ADDCONSTRAINT [DF_wwasyncwebrequest_username] DEFAULT ('') FOR [username]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwasyncwebrequest_chkcounter]') AND type = 'D')BEGINALTERTABLE [dbo].[wwasyncwebrequest] ADDCONSTRAINT [DF_wwasyncwebrequest_chkcounter] DEFAULT ((0)) FOR [chkcounter]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwasyncwebrequest_cancelled]') AND type = 'D')BEGINALTERTABLE [dbo].[wwasyncwebrequest] ADDCONSTRAINT [DF_wwasyncwebrequest_cancelled] DEFAULT ((0)) FOR [cancelled]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwasyncwebrequest_properties]') AND type = 'D')BEGINALTERTABLE [dbo].[wwasyncwebrequest] ADDCONSTRAINT [DF_wwasyncwebrequest_properties] DEFAULT ('') FOR [properties]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwrequestlog_time]') AND type = 'D')BEGINALTERTABLE [dbo].[wwrequestlog] ADDCONSTRAINT [DF_wwrequestlog_time] DEFAULT (getdate()) FOR [time]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwrequestlog_reqid]') AND type = 'D')BEGINALTERTABLE [dbo].[wwrequestlog] ADDCONSTRAINT [DF_wwrequestlog_reqid] DEFAULT ('') FOR [reqid]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwrequestlog_script]') AND type = 'D')BEGINALTERTABLE [dbo].[wwrequestlog] ADDCONSTRAINT [DF_wwrequestlog_script] DEFAULT ('') FOR [script]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwrequestlog_querystr]') AND type = 'D')BEGINALTERTABLE [dbo].[wwrequestlog] ADDCONSTRAINT [DF_wwrequestlog_querystr] DEFAULT ('') FOR [querystr]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwrequestlog_duration]') AND type = 'D')BEGINALTERTABLE [dbo].[wwrequestlog] ADDCONSTRAINT [DF_wwrequestlog_duration] DEFAULT ((0.00)) FOR [duration]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwrequestlog_remoteaddr]') AND type = 'D')BEGINALTERTABLE [dbo].[wwrequestlog] ADDCONSTRAINT [DF_wwrequestlog_remoteaddr] DEFAULT ('') FOR [remoteaddr]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwrequestlog_memused]') AND type = 'D')BEGINALTERTABLE [dbo].[wwrequestlog] ADDCONSTRAINT [DF_wwrequestlog_memused] DEFAULT ('') FOR [memused]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwrequestlog_error]') AND type = 'D')BEGINALTERTABLE [dbo].[wwrequestlog] ADDCONSTRAINT [DF_wwrequestlog_error] DEFAULT ('') FOR [error]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwrequestlog_reqdata]') AND type = 'D')BEGINALTERTABLE [dbo].[wwrequestlog] ADDCONSTRAINT [DF_wwrequestlog_reqdata] DEFAULT ('') FOR [reqdata]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwrequestlog_browser]') AND type = 'D')BEGINALTERTABLE [dbo].[wwrequestlog] ADDCONSTRAINT [DF_wwrequestlog_browser] DEFAULT ('') FOR [browser]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwrequestlog_referrer]') AND type = 'D')BEGINALTERTABLE [dbo].[wwrequestlog] ADDCONSTRAINT [DF_wwrequestlog_referrer] DEFAULT ('') FOR [referrer]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwrequestlog_result]') AND type = 'D')BEGINALTERTABLE [dbo].[wwrequestlog] ADDCONSTRAINT [DF_wwrequestlog_result] DEFAULT ('') FOR [result]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwsession_SessionID]') AND type = 'D')BEGINALTERTABLE [dbo].[wwsession] ADDCONSTRAINT [DF_wwsession_SessionID] DEFAULT ('') FOR [SessionID]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwsession_UserId]') AND type = 'D')BEGINALTERTABLE [dbo].[wwsession] ADDCONSTRAINT [DF_wwsession_UserId] DEFAULT ('') FOR [UserId]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwsession_firston]') AND type = 'D')BEGINALTERTABLE [dbo].[wwsession] ADDCONSTRAINT [DF_wwsession_firston] DEFAULT (getdate()) FOR [firston]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwsession_laston]') AND type = 'D')BEGINALTERTABLE [dbo].[wwsession] ADDCONSTRAINT [DF_wwsession_laston] DEFAULT (getdate()) FOR [laston]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwsession_vars]') AND type = 'D')BEGINALTERTABLE [dbo].[wwsession] ADDCONSTRAINT [DF_wwsession_vars] DEFAULT ('') FOR [vars]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwsession_browser]') AND type = 'D')BEGINALTERTABLE [dbo].[wwsession] ADDCONSTRAINT [DF_wwsession_browser] DEFAULT ('') FOR [browser]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwsession_ip]') AND type = 'D')BEGINALTERTABLE [dbo].[wwsession] ADDCONSTRAINT [DF_wwsession_ip] DEFAULT ('') FOR [ip]ENDGOIFNOTEXISTS (SELECT * FROM dbo.sysobjectsWHERE id = OBJECT_ID(N'[dbo].[DF_wwsession_hits]') AND type = 'D')BEGINALTERTABLE [dbo].[wwsession] ADDCONSTRAINT [DF_wwsession_hits] DEFAULT ((0)) FOR [hits]ENDGO
Double checked to make sure this all works, and it looks like it does, including updating the database...
Note that you have to run the script separately. Noticed today that the script was actually compiled into CONSOLE.EXE so changing externally did not work. Removed the internal file so in the future the script can be modified and Console.exe will use it (although it does get overwritten in updates)
+++ Rick ---
West Wind Technologies
Making waves on the Web