日期:2014-05-18  浏览次数:20538 次

这个sql 语句怎么写?
我要插入bit 型数据

前台接受到的是 true 和false

我用insert into 语句插入
怎么判段 如果为true 则插入 1 ,

flase 则插入0

------解决方案--------------------
SQL code

--这个程序里插入前判断下就行了吧。。。

------解决方案--------------------
SQL code
insert into tb select case col when 'ture' then 1 when 'flase' then 0 end from ...

------解决方案--------------------
这要看前台与语句的接口是什么了.
如果用存储过程,设置parameter参数为逻辑型的话,就不用变化.
如果前台用的是插入语句,那在前台直接改为1,0不是什么难事.
如果....

貌似没有如果了.前台要传数据来,总不能愚蠢到传一个 insert into tb(bitcol)values('true')进来吧!
------解决方案--------------------
探讨
SQL code

insert into tb select case col when 'ture' then 1 when 'flase' then 0 end from ...

------解决方案--------------------
SQL code
declare @tb table(istrue bit)
insert into @tb select 'true'
select * from @tb
/*
(1 行受影响)
istrue
------
1

------解决方案--------------------
前台接受到的是 true 和false

你前端变量是布尔型的直接判断插入呗!SQL05中,字段是bit型,写入数据你打开表看还是会显示 true 或 false,这没什么可纠结的。
------解决方案--------------------
bit类型本身就是true和false,直管insert就行了
------解决方案--------------------
case when 即可。
------解决方案--------------------
探讨
引用:
SQL code

insert into tb select case col when 'ture' then 1 when 'flase' then 0 end from ...


试问,这句语句写在哪儿呢?

------解决方案--------------------
探讨
bit类型本身就是true和false,直管insert就行了

------解决方案--------------------
SQL code

if object_id('tb','U') is not null
   drop table tb
go
create table tb
(
 id int identity(1,1),
 name varchar(10),
 sex bit
)
go
--字符串值 TRUE 和 FALSE 可以转换为以下 bit 值:TRUE 转换为 1,FALSE 转换为 0。 
insert into tb(name,sex)
select '张三','true' union all
select '李四','true' union all
select '王五','false'
go
select * from tb
/*
id          name       sex
----------- ---------- -----
1           张三         1
2           李四         1
3           王五         0

(3 行受影响)
*/

------解决方案--------------------
学习了,很好的例子
------解决方案--------------------
不知所云
------解决方案--------------------
+1
探讨
SQL code
insert into tb select case col when 'ture' then 1 when 'flase' then 0 end from ...