我有一个表格,表格里面有很5个字段,他们都是numeric(8, 5)数据类型的,他们的属性都可以为空!
现在我要用SQL语句在页面上插入一条数据。页面上有5个文本输入框,但是这5个文本输入框不一定都有数据,
但是也不确定哪个有数据,哪个没数据,要求是点击添加的时候能够添加成功,切不能给他们设置默认值。
比如:
有可能是这样:insert into TB(A,B,C,D,E) values('2','','','5','2');
也有可能是这样:insert into TB(A,B,C,D,E) values('2','3','','','')我想要的结果就是:怎么样可以让上面的语句插入成功!
或者是是用什么办法可以做到在插入的时候自己会自动检测一下,如果是空值就不插入,
比如insert into TB(A,B,C,D,E) values('2','','','5','2')检测后就改变成
这样:insert into TB(A,D,E) values('2','','','5','2');insert into TB(A,B,C,D,E) values('2','3','','','')检测后就改变成这样
insert into TB(A,B) values('2','3')请各位前辈帮忙!

解决方案 »

  1.   

    用个存储过程插入,或程序判断,没有值时insert默认值
      

  2.   

    if exists (select * from table where primarykey =?) 
    begin
        update table 
    end
    elsebegin
        insert table 
    end
      

  3.   

    没有意义,如果非要这样的,在程序里面写条件语句,判断文本输入框是否有值,来拼接SQL语句!
      

  4.   

    这种做法毫无意义 。
    如果只有A、B有值的时候,完成可以这样,
    insert into 表名 (A,B,C,D,E) values('a','b',null,null,null)这样和insert into 表名 (A,B) values('a','b')在数据库中显示的结果毫无区别。。
      

  5.   

    现在大家不要讨论有没有意义了!总之就是一句话,就上面说的数据结构:
    insert   into   TB(A,B,C,D,E)   values('2','','','5','2');只要这条语句可以执行成功就可以了!
    有什么办法!
      

  6.   

    我想要的结果就是:怎么样可以让上面的语句插入成功! 
    或者是是用什么办法可以做到在插入的时候自己会自动检测一下,如果是空值就不插入, 
    --
    为空?
    insert       into       TB(A,B,C,D,E)       values('2',null,null,'5','2');才是呀
    这样成不?
    insert       into       TB(A,B,C,D,E)       values('2','0','0','5','2');
      

  7.   

    1、纠正下LZ的错误,既能是numeric(8,5)类型,就不能用字符了
    insert   into   TB(A,B,C,D,E)   values('2','3','','','') 语句执行后C,D,E的值为0。
    2、在前端拼接sql,SQL SERVER 并不知道你的前端是填了0还是什么都没填.
      

  8.   

    insert       into       TB(A,B,C,D,E)       values('2','3','','','') 
    改成
    insert       into       TB(A,B,C,D,E)       values('2','3',null,null,null)
    效果也是一样的。
    最好用
    insert       into       TB(A,B,C,D,E)       values(2,3,null,null,null)  
      

  9.   

    又不在前台作判断,又不能用存储过程,
    一定要insert into TB(A,B,C,D,E) values(2,3,,,) 成功,怎么成功啊?
    在你产生这个语句的时候,总是要输入2,3的呀,这个时候你可以把语句改一下啊
      

  10.   

    <%
    sSql = "insert into TB( "
    sStr1 = ""
    sStr2 = ""
    If A<>"" Then
    sStr1 = "A,"
    sStr2 = sStr2 & "'2',"
    End If
    If B<>"" Then
    sStr1 = "B,"
    sStr2 = sStr2 & "'3',"
    End If
    If C<>"" Then
    sStr1 = "C,"
    sStr2 = sStr2 & "'2',"
    End If
    If D<>"" Then
    sStr1 = "D,"
    sStr2 = sStr2 & "'2',"
    End If
    If E<>"" Then
    sStr1 = "E,"
    sStr2 = sStr2 & "'2',"
    End If
    sStr1 = Left(sStr1,Len(sStr1)-1)
    sStr2 = Left(sStr2,Len(sStr1)-1)
    sSql = sSql & " " & sStr1 & " "
    sSql = sSql & " ) values ( "
    sSql = sSql & " " & sStr2 & " "
    sSql = sSql & " )"
    %>
      

  11.   

    if   exists   (select   *   from   table   where   primarykey   =?)   
    begin 
            update   table   
    end 
    else begin 
            insert   table   
    end
      

  12.   

    create table #testtb
    (
    a numeric(8,5),
    b numeric(8,5),
    c numeric(8,5),
    d numeric(8,5),
    e numeric(8,5)
    )declare @a varchar(10)
    declare @b varchar(10)
    declare @c varchar(10)
    declare @d varchar(10)
    declare @e varchar(10)
    set @a = ''
    set @b = '321.32'
    set @c = '345.21'
    set @d = null
    set @e = '454.232'INSERT INTO #testtb
               ([a]
               ,[b]
               ,[c]
       ,[d]
       ,[e]
       )
         VALUES
               (
                case when @a is null or @a='' then null else convert(numeric(8,5),@a) end
               ,case when @b is null or @b='' then null else convert(numeric(8,5),@b) end
               ,case when @c is null or @c='' then null else convert(numeric(8,5),@c) end
       ,case when @d is null or @d='' then null else convert(numeric(8,5),@d) end
               ,case when @e is null or @e='' then null else convert(numeric(8,5),@e) end
               )select * from #testtb
    drop table #testtb运行结果:
     a     b       c        d       e
    NULL 321.32000 345.21000 NULL 454.23200
      

  13.   

    create table tb
    (A numeric(8,5),B numeric(8,5),C numeric(8,5),D numeric(8,5),E numeric(8,5))
    gocreate proc inserttb
    (@a numeric(8,5),
    @b numeric(8,5),
    @c numeric(8,5),
    @d numeric(8,5),
    @e numeric(8,5))
    AS
    insert into tb
    values
    (case when @a is null then null else @a end,
    case when @b is null then null else @b end,
    case when @c is null then null else @c end,
    case when @d is null then null else @d end,
    case when @e is null then null else @e end)
    goexec inserttb @a=null,@b=23.12,@c=null,@d=12.451,@e=nullselect * from tb/*(所影响的行数为 1 行)A          B          C          D          E          
    ---------- ---------- ---------- ---------- ---------- 
    NULL       23.12000   NULL       12.45100   NULL(所影响的行数为 1 行)
    */