SQL 默认值不起作用?
本帖最后由 feiniaoflyer 于 2012-11-29 05:46:30 编辑 create table [dbo].[T_Message]
(
[MaskId] uniqueidentifier default newid() not null,
[ProductMaskId] uniqueidentifier,
[Name] nvarchar(50),
[Email] nvarchar(50),
[Theme] nvarchar(255),
[Content] nvarchar(4000),
[AddTime] datetime default getdate()
)
SqlParameter[] paramArray ={
new SqlParameter("@ProductMaskId",P_ID),
new SqlParameter("@Name",TextBox1.Text),
new SqlParameter("@Email",TextBox2.Text),
new SqlParameter("@Theme",TextBox3.Text),
new SqlParameter("@Content",TextBox4.Text),
new SqlParameter("@AddTime",DateTime.UtcNow.ToString())
};
DB.ExecuteCommand("insert into T_Message([ProductMaskId],[Name],[Email],[Theme],[Content],[AddTime]) values(@ProductMaskId,@Name,@Email,@Theme,@Content,@AddTime);", paramArray)
报错:
不能将值 NULL 插入列 'MaskId',表 'dbo.T_Message';列不允许有空值。INSERT 失败。
语句已终止。
ALTER TABLE T_Message
ALTER COLUMN MaskId uniqueidentifier NULL
此时能Insert但MaskId是null值
ALTER TABLE T_Message
ALTER COLUMN MaskId set default newid()
关键字 'set' 附近有语法错误。
求教为什么不能插入数据?
[解决办法]
没出现lz说的问题。 测试版本为2008 R2
create table [dbo].[T_Message]
(
[MaskId] uniqueidentifier default newid() not null,
[ProductMaskId] uniqueidentifier,
[Name] nvarchar(50),
[Email] nvarchar(50),
[Theme] nvarchar(255),
[Content] nvarchar(4000),
[AddTime] datetime default getdate()
)
INSERT INTO t_message(ProductMaskId,name,email)
SELECT NEWID(),'a','elele'
SELECT * FROM T_Message
/*
MaskId ProductMaskId Name Email Theme Content AddTime
------------------------------------ ------------------------------------ -------------------------------------------------- -------------------------------------------------- --------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------- -----------------------
EA37B1ED-6DCB-4A9B-AC4C-6E59F36885C2 34B5738F-EE36-461F-B1F3-9CE14FC9C561 a elele NULL NULL 2012-11-29 07:56:59.183
(1 行受影响)
*/