数据库表:tab
id  int
s   nvarchar(10)
t   tinyint
,插入语句:
....
@s nvarchar(10)
@t tinyint 可空
...
insert into tab(s,t) values(@s,@t)
这样只能插入tinyint型数据
外部c#写了个类:
myInsert(string s,byte t)
{
  插入(s,t);
}问题就来了,我调用myInsert,只想插入s,t为空值,我要怎样才能插入空值啊?最好不要重载myInsert

解决方案 »

  1.   

    insert into tab(s,t) values(@s,@t) insert into tab(s,t) values(null,null) 
      

  2.   

    create table tab (id  int ,s  nvarchar(10) ,t  tinyint )
    insert into tab values(1 , '1' , 1)
    insert into tab values(2 , null , null)
    goselect * from tabdrop table tab/*
    id          s          t    
    ----------- ---------- ---- 
    1           1          1
    2           NULL       NULL(所影响的行数为 2 行)
    */
      

  3.   

    那这样我得写两个插入语句?
    insert into tab(s,t) values(@s,@t)insert into tab(s,t) values(@s,null)有没有其它方法?
      

  4.   

    不用.
    如果你的@t为空,直接用第一句即可.create table tab (id  int ,s  nvarchar(10) ,t  tinyint )
    godeclare @t as tinyintset @t = 1insert into tab values(1 , '1' , @t)  --这句和下面一句一样,只是@t的值不同set @t = nullinsert into tab values(1 , '1' , @t)  --这句和上面一句一样,只是@t的值不同select * from tabdrop table tab/*
    id          s          t    
    ----------- ---------- ---- 
    1           1          1
    1           1          NULL(所影响的行数为 2 行)
    */