create table t3
(
a char(10),
b char(10),
c char(10),
d char(10)
)
create table te(a varchar(10) primary key not null,b int)
insert into te values('1',200)
insert into te values('',1)
select * from te
ALTER TABLE te
ADD CONSTRAINT CK_b CHECK(b>=1)
GO
----------------------------------------------
begin transaction
declare @errorSum int
set @errorSum= 0
update te set b=b+200 where a='1'
set @errorSum=@errorSum+ 'select'+ convert(int,+'b')+' from te.b where a='+'2'
update te set b=b-200 where a='2'
print '查看事物結果'
select * from te
if @errorSum >1000
begin
print '交易失敗'
rollback transaction
end
else
begin
print '交易成功'
commit transaction
end
go
create table tb(id int,姓名 varchar(30))
insert into tb values(1,'張三,李四a')
insert into tb values(2,'aaa,bbb')
select * from tb
go
--邏輯拆分字段值,substring()函數和charindex()函數,子查詢
select A.id, 姓名 =substring(A.姓名, B.id,charindex(',', A.姓名 +',',B.id) -B.id)
from tb A,(select number as id from master..spt_values where type ='P') B
where substring(','+A.姓名, B.id, 1)=','
select id,姓名 = CHARINDEX(',',姓名,id) from tb
select A.id, 姓名 =substring(A.姓名, B.id, CHARINDEX(',',A.姓名+',',B.id)-B.id)
from tb A,(select number as id from tb where type='p') B
where substring(','+A.姓名,B.id, 1)=','
from master..spt_values
select id, CHARINDEX(',',姓名,id) from tb
--得出結果是: id 姓名
--2 4 表示從第四個字符開始包含第四個
--1 3
-- ','表示從這個字符開始計算截取 -id 表示從每個為id的姓名字段減1
select id,姓名 =substring(姓名, id, CHARINDEX(',',姓名)-id) from tb
DECLARE @document varchar(64)
SELECT @document = 'Reflectors are vital safety' +
' components of your bicycle.'
SELECT CHARINDEX('bicycle', @document) --‘bicycle’參數表示計算整個字符串的截止的字符串
GO --@document計算的表達式
--Oracle的拆分處理方法將'_'符號拆分出來
--select id,substr(name,1,instr(name,'_')-1),substr(name,instr(name,'_')+1) from tableName
--select substr(to_char(date,'YYYY-MM-DD HH24:mi:ss'),1,10) 日期,
--substr(to_char(date,'YYYY-MM-DD HH24:mi:ss'),12) 時間,
--substr(name,1,2) 字符1,
--substr(name,4,2) 字符2,
--substr(name,7,2) 字符3
--from tablename