首页 技术 正文
技术 2022年11月17日
0 收藏 721 点赞 3,909 浏览 2933 个字

1、

 if exists(select *  from sys.objects where name='test')
drop table test
go
create table test
(
id varchar(),
name varchar()
)
if exists(select * from sys.objects where name='t_insert')
drop trigger t_insert
go
create trigger t_insert on test
instead of insert
as
declare @id varchar(),@id1 int,@head varchar()
select * into #tb from inserted
set @head='uzi'+convert(varchar(),getdate(),)
select @id=max(id) from test
--if @id is null
--set @id1=
--else
--set @id1=cast(substring(@id,,) as int)
if exists(select * from test)
set @id1=cast(substring(@id,,) as int)
else
set @id1=
update #tb set @id1=@id1+,id=@head+right(''+cast(@id1 as varchar),)
insert into test select * from #tb
go
insert into test(name) values('小马')
insert into test(name) values('小明')
insert into test(name) values('小鱼') select * from test

表test

if exists(select * from test)
select ‘0’  ————————————————-
else        — 表中有数据时为: 0 | 表中无数据时为: 1      —
select ‘1’  ————————————————-
if exists(select count(*) from test)
select ‘0’ ————————————————–
else       — 表中有数据时为: 1 | 表中无数据时为: 0        —
select ‘1’ ————————————————–

丄丄丄丄丄丄丄丄丄丄丄丄丄丄丄丄丄丄丄丄丄丄丄丄丄丄丄丄丄丄丄

【判断表中是否有数据】

丅丅丅丅丅丅丅丅丅丅丅丅丅丅丅丅丅丅丅丅丅丅丅丅丅丅丅丅丅丅丅

–使用局部变量

  –select @id=max(id) from test

–if @id is null
          –set @id1=0
      –else
          –set @id1=cast(substring(@id,12,4) as int)

2、一样的代码,就差一个范围,效果不相同

 if exists(select *  from sys.objects where name='test')
drop table test
go
create table test
(
id varchar(),
name varchar()
)
if exists(select * from sys.objects where name='t_insert')
drop trigger t_insert
go
create trigger t_insert on test
instead of insert
as
declare @id varchar(),@id1 int,@head varchar()
select * into #tb from inserted
set @head='uzi'+convert(varchar(),getdate(),)
select @id=max(id) from test
if @id is null
set @id1=
else
set @id1=cast(substring(@id,,) as int)
update #tb set @id1=@id1+,id=@head+right(''+cast(@id1 as varchar),)
insert into test select * from #tb
go
insert into test(name) values('小马')
insert into test(name) values('小明')
insert into test(name) values('小鱼') select * from test
 if exists(select * from sys.objects where name='test')
drop table test
go
create table test
(
id varchar(),
name varchar()
)
--触发器
if exists(select * from sys.objects where name='t_insert')
drop trigger t_insert
go
create trigger t_insert on test
instead of insert
as
declare @id varchar,@id1 int,@head varchar()
select * into #tb from inserted
set @head=convert(varchar(),getdate(),)
select @id=max(id) from test
if @id is null
set @id1=
else
set @id1=cast(substring(@id,,) as int)
update #tb set @id1=@id1+,id=@head+right(''+cast(@id1 as varchar),)
insert into test select * from #tb
go
insert into test(name) values('小马')
insert into test(name) values('小白')
insert into test(name) values('小黑')
select * from test

给变量添加类型后规定范围(第2个16行代码处)

    (a)declare @id varchar,@x int,@head varchar(20)

      效果为:

        201606140001    小黄
        201606140001    小白
        201606140001    小黑

    (b)declare @id varchar(20),@x int,@head varchar(20)

      效果为:

        201606140001    小黄
        201606140002    小白
        201606140003    小黑

    (c)declare @id varchar(20),@x int,@head varchar

      效果为:

        20001    小黄
        20001    小白
        20001    小黑

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,557
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898