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    小黑

数据库 sql server的更多相关文章

  1. 数据库SQL Server与C#中数据类型的对应关系

    ylbtech- .NET-Basic:数据库SQL Server与C#中数据类型的对应关系 数据库SQL SServer与C#中数据类型的对应关系 1.A,返回顶部 数据库 C#程序 int int ...

  2. 数据库管理工具神器-DataGrip,可同时管理多个主流数据库[SQL Server,MySQL,Oracle等]连接

    前言 DataGrip:Jet Brains出品的一款数据库管理工具(没错,是Jet Brains出品,必属精品).DataGrip整合集成了当前主流数据库(如:SQL Server, MySQL, ...

  3. sql server ------创建本地数据库 SQL Server 排序规则

    sql server完整复制数据库 sql server导入导出方法 SQL Server 排序规则

  4. 数据库 SQL Server 到 MySQL 迁移方法总结

    最近接手一起老项目数据库 SQL Server 到 MySQL 的迁移.因此迁移前进行了一些调查和总结.下面是一些 SQL Server 到 MySQL 的迁移方法. 1. 使用 SQLyog 迁移 ...

  5. [数据库]SQL Server 用户NT AUTHORITY\IUSR 登录失败

    今天打开网站时,突然报这个错误,平时都好好的 Cannot open database "JMECC" requested by the login. The login fail ...

  6. 数据库SQL Server DAC 导入导出数据到SQL Azure问题

    对Export data-tier application报错的处理 Error:SQL71564 这个问题是数据库中一些对象如MS_Description,MS_DiagramPane1不支持DAC ...

  7. 如何分离数据库 (SQL Server Management Studio)

    在 SQL Server Management Studio 对象资源管理器中,连接到 SQL Server 数据库引擎的实例上,再展开该实例. 展开“数据库”,并选择要分离的用户数据库的名称. 分离 ...

  8. 批量还原数据库 SQL Server 2008

    1.如果你够懒,不想一步一步点路径,一步一步选择 2.如果你连单个备份数据库的存储过程都不想多执行,一般每还原一个需要修改数据库名 下面的脚本适合你: /*********************** ...

  9. 基础知识(C#语法、数据库SQL Server)回顾与总结

    前言 已经有大概一个多月没有更新博客,可能是开始变得有点懒散了吧,有时候想写,但是又需要额外投入更多的时间去学习,感觉精力完全不够用啊,所以为了弥补这一个多月的潜水,决定写一篇,衔接9月未写博客的空缺 ...

  10. 数据库 --> SQL Server 和 Oracle 以及 MySQL 区别

    SQL Server 和 Oracle 以及 MySQL 区别 三者是目前市场占有率最高(依安装量而非收入)的关系数据库,而且很有代表性.排行第四的DB2(属IBM公司),与Oracle的定位和架构非 ...

随机推荐

  1. WP8.1 RT 生命周期详解‏‏‏‏‏‏‏‏‏‏‏‏‏

                应用状态关系转换图 根据此“应用状态关系转换图”.有以下: 三个状态: NotRunning:未运行    Running:运行中   Suspended:挂起 三个事件: A ...

  2. PHP mysql与mysqli事务详解

    官方对PHP连接到MySQL数据库服务器的三种主要的API简介如下: http://php.net/manual/zh/mysqli.overview.php PHP mysql与mysqli事务详解 ...

  3. Linux下提示 bash: xxx command not found

    今天在虚拟机上安装了CentOS5.5,发现运行一些很正常的诸如:init,shutdown,fdisk 等命令时,悍然提示: bash: xxx command not found. 那么,首先就要 ...

  4. 练习题(登陆-进度条-微信接口判断qq-微信接口判断列车时刻表-)

    1.写一个用户的登陆注册的界面,用户的密码用hashlib加密存在文件中,登陆时候,用户的密码要和文件中的密码一致才行 def sha(password): #加密函数 passwd = hashli ...

  5. 【转】AWK 简明教程

    本文转自:http://coolshell.cn/articles/9070.html 有一些网友看了前两天的<Linux下应该知道的技巧>希望我能教教他们用awk和sed,所以,出现了这 ...

  6. spring mvc redis消息队列

    通常情况下,为了提高系统开发的灵活性和可维护度,我们会采用消息队列队系统进行解耦.下面是一个采用spring redis实现的消息队列实例,但此实例会由于网络延迟和阻塞等情况导致消息处理的延时,因而不 ...

  7. mysql状态取反(0变1,1变0)

    如果要改变status状态 update   table_name   status=ABS(status-1);   //取绝对值 则0-->1   1-->0

  8. Web负载均衡的几种实现方式

    Web负载均衡的几种实现方式摘要:负载均衡(Load Balance)是集群技术(Cluster)的一种应用.负载均衡可以将工作任务分摊到多个处理单元,从而提高并发处理能力.目前最常见的负载均衡应用是 ...

  9. CSC321 神经网络语言模型 RNN-LSTM

    主要两个方面 Probabilistic modeling 概率建模,神经网络模型尝试去预测一个概率分布 Cross-entropy作为误差函数使得我们可以对于观测到的数据 给予较高的概率值 同时可以 ...

  10. Spring中文文档-第一部分

    一. Spring 框架概述 Spring是为了构建企业应用的轻量级框架.然而,Spring是模块化的,允许你只是使用其中的一部分,不需要引入其他的.你可以在任何web框架上使用IoC容器,也可以只使 ...