首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

SQL 默认值不起作用?该如何解决

2013-06-19 
SQL 默认值不起作用?本帖最后由 feiniaoflyer 于 2012-11-29 05:46:30 编辑create table [dbo].[T_Message

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 行受影响)
*/


[解决办法]
SQL 默认值不起作用?该如何解决
[解决办法]
猜猜就猜猜 莫非有触发器 
[解决办法]
1、没传达参数。
2、触发器,
一般就这两种
[解决办法]
莫非是触发器

元芳,你怎么看?
[解决办法]
仔细检查检查。。默认值肯定没问题的

热点排行