批量添加:

DECLARE @GID INT,@UID INT,@Indexs INT
SET @GID=1
SET @UID=37
SET @Indexs=0
WHILE @GID<674 --674要执行插入的次数
BEGIN
INSERT INTO TB_Employee (CompetitionId, UserId,GroupId,GroupSouce,IsAudit,IsFinal ) -- CompetitionUser 为表名
VALUES ( 52, @UID, @GID, 1, 1, 0)
SET @Indexs = @Indexs+1;
SET @UID = @UID+1;
IF(@Indexs=3)
BEGIN
SET @Indexs=0
SET @GID=@GID+1
END
END

条件排序:

select * FROM TB_Employee ORDER BY
case
when Salary = 1200 then 1
when Salary > 1200 then 2
when Salary < 1200 then 3
end,EmployeeId;

select * FROM TB_Employee ORDER BY
case
when Salary in (1200,238) then 1
else 2
end,EmployeeId

存储过程:

USE [People]
GO

create proc Proc_GetIndexNum
@collegeId int,
@userId int,
@temp1 int output,
@temp2 int output,
@temp3 int output,
@temp4 int output

as
-- 数量1
select @temp1=COUNT(1) from TB_Employee a where IsRelease=1 and IsDelete=0 and CollegeId =@collegeId
and ((a.Type=1 and a.State<102) or (a.Type=2 and a.State<202) or (a.Type=3 and a.State<302))
and (select count(1) from TB_Teacher where CompetitionId=a.Id and UserId=@userId) < 1

-- 数量2
select @temp2=COUNT(1) from TB_Employee a where IsRelease=1 and IsDelete=0 and CollegeId =@collegeId
and ((a.Type=1 and a.State=103) or (a.Type=2 and a.State=203) or (a.Type=3 and (a.State=303 or a.State=306)))
and (select count(1) from TB_Teacher where CompetitionId=a.Id and UserId=@userId and IsAudit=1)>0

--数量3
select @temp3=COUNT(1) from TB_Employee where IsRelease=2 and IsDelete=0 and CollegeId =@collegeId

-- 数量4
select @temp4=COUNT(*) from Account a inner join UserInfo b
on a.UserId = b.Id and b.Status=2 and b.CollegeId=@collegeId

-- 执行测试

--SET STATISTICS TIME ON

--DECLARE @CID INT,@UID INT,@NumA INT,@NumB INT,@NumC INT,@NumD INT
--set @CID=1
--set @UID=1
--exec Proc_GetIndexNum @CID,@UID,@NumA out,@NumB out,@NumC out,@NumD out
--SET STATISTICS TIME OFF

更改原字段内容:

update UserInfo set LoginName=(LoginName+'(已删除)') where LoginName=@LoginName

[通过sql在目标电脑创建文件路径]:

EXEC sp_configure 'show advanced options', 1

GO

RECONFIGURE

GO

EXEC sp_configure 'xp_cmdshell', 1

RECONFIGURE

GO

ExEc xp_cmdshell 'mkdir E:\Aroject' --调用DOS命令创建project文件夹

[获取目标电脑数据库文件存放位置,并在此创建数据库]:

declare  @path  varchar(200)

select  @path  =  filename  from  master.dbo.sysfiles

set  @path  =  ltrim(REVERSE(@path))

set  @path  =  REVERSE(substring(@path,CHARINDEX('\',@path),len(@path)))

exec('CREATE DATABASE xxxx ON PRIMARY (NAME = "xxxx", FILENAME = "' + @path + 'xxxx.mdf", SIZE = 2304KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB) LOG ON (NAME = "xxxx_log", FILENAME = "' + @path + 'xxxx.ldf", SIZE = 832KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)')

[如果数据库已存在,关闭数据库连接,并删除数据库]:

declare @i int declare cur cursor for select spid from sysprocesses where db_name(dbid)= 'DB_Test' open cur fetch next from cur into @i while @@fetch_status=0 begin exec('kill '+@i) fetch next from cur into @i end close cur deallocate cur

IF  EXISTS (SELECT name FROM sys.databases WHERE name = N'DB_Test')

DROP DATABASE [DB_Test]

---------------------------------------------------
-- desc: 通用分页存储过程
---------------------------------------------------

create PROCEDURE [dbo].[Proc_CommonPagingStoredProcedure]
@Tables nvarchar(1000), --表名,多表请使用 tableA a inner join tableB b On a.AID = b.AID
@PK nvarchar(100), --主键,可以带表头 a.AID
@Sort nvarchar(200) = '', --排序字段
@PageNumber int = 1, --开始页码
@PageSize int = 10, --页大小
@Fields nvarchar(1000) = '*', --读取字段
@Filter nvarchar(1000) = NULL, --Where条件
@isCount bit = 0 , --1 --是否获得总记录数
@Total int output
AS

DECLARE @strFilter nvarchar(2000)
declare @sql Nvarchar(max)
IF @Filter IS NOT NULL AND @Filter != ''
BEGIN
SET @strFilter = ' WHERE 1=1 ' + @Filter + ' '
END
ELSE
BEGIN
SET @strFilter = ' '
END
if @isCount = 1 --获得记录条数
begin
Declare @CountSql Nvarchar(max)
Set @CountSql = 'SELECT @TotalCount= Count(1) FROM ' + @Tables + @strFilter
Execute sp_executesql @CountSql,N'@TotalCount int output',@TotalCount= @Total Output
end

if @Sort is null or @Sort = ''''
set @Sort = @PK + ' DESC '

IF @PageNumber < 1
SET @PageNumber = 1

if @PageNumber = 1 --第一页提高性能
begin
set @sql = 'select top ' + str(@PageSize) +' '+@Fields+ ' from ' + @Tables + ' ' + @strFilter + ' ORDER BY '+ @Sort
end
else
begin
DECLARE @START_ID varchar(50)
DECLARE @END_ID varchar(50)

SET @START_ID = convert(varchar(50),(@PageNumber - 1) * @PageSize + 1)
SET @END_ID = convert(varchar(50),@PageNumber * @PageSize)
set @sql = ' SELECT * '+
'FROM (SELECT ROW_NUMBER() OVER(ORDER BY '+@Sort+') AS rownum,
'+@Fields+ '
FROM '+@Tables+ @strFilter +' ) AS D
Where rownum >= '+@START_ID+' AND rownum <=' +@END_ID +' ORDER BY '+substring(@Sort,charindex('.',@Sort)+1,len(@Sort)-charindex('.',@Sort))
END

EXEC(@sql)

【为字段添加唯一约束】:

alter table [GTA_FPBT_Training_V1.5].dbo.AssessmentResults
add constraint [user_match_id] unique (UserId,CompetitionId,TrainExamId)

MSSQL记录的更多相关文章

  1. [MSSQL]SQL疑难杂症实战记录-巧妙利用PARTITION分组排名递增特性解决合并连续相同数据行

    问题提出 先造一些测试数据以说明题目: DECLARE @TestData TABLE(ID INT,Col1 VARCHAR(20),Col2 VARCHAR(20)) INSERT INTO @T ...

  2. [MSSQL]如何高效查询表的总记录数

    如何高效查询表的总记录数?[总结-整理-马克] 首先想到的自然是在表主键上应用COUNT函数来查询了,这个是目前使用最多的方法,没有之一 ) ROWS FROM product 这里再给出一些其它方法 ...

  3. 【公开课】《奥威Power-BI基于微软示例库(MSSQL)快速制作管理驾驶舱》文字记录与反馈

        本期分享的内容: <奥威Power-BI基于微软示例库(MSSQL)快速制作管理驾驶舱> 时间:2016年11月02日 课程主讲人:叶锡文 从事商业智能行业,有丰富的实施经验,擅长 ...

  4. 记录一次php连接mssql的配置

    记录一次php连接mssql的配置 在现有php环境中,php连接mssql数据库失败,tsql 连接正常. 确认问题在php环境上. 网上有个同仁总结的很好,https://blog.csdn.ne ...

  5. Access,MSSQL:随机读取N条记录

    今天试着将一个网站使用的mssql转换为Access,但网站首页有一段代码是随机读取n条记录: SQL Server:Select TOP N * From TABLE Order By NewID( ...

  6. Atitit.mssql 数据库表记录数and 表体积大小统计

    Atitit.mssql 数据库表记录数and 表体积大小统计 1. EXEC   sp_MSforeachtable   "EXECUTE   sp_spaceused   '?'&quo ...

  7. MSSQL—按照某一列分组后取前N条记录

    以前在开发的时候遇到过一个需求,就是要按照某一列进行分组后取前几条数据,今天又有同事碰到了,帮解决了之后顺便写一篇博客记录一下. 首先先建一个基础数据表,代码如下: IF OBJECT_ID(N'Te ...

  8. MSSQL—列记录合并

    在项目开发中,有时会碰到将列记录合并为一行的情况,例如根据地区将人员姓名合并,或根据拼音首字母合并城市等,下面就以根据地区将人员姓名合并为例,详细讲一下合并的方法. 首先,先建一个表,并添加一些数据, ...

  9. MSSQL 查询分组前N条记录

    sql语句中,查询分组中前n条记录的sql语句如下 第一种方法 select * from consultingfeebill awhere n>(select count(*) from co ...

随机推荐

  1. Ubuntu安装Python机器学习包

    1.安装pip $ mkdir ~/.pip $ vi ~/.pip/pip.conf [global] trusted-host=mirrors.aliyun.com index-url=http: ...

  2. Jquery 操作DropDownList 根据条件选中

    $("#<%=DDLCounty.ClientID%> option").each(function () { if ($(this).text() == $(&quo ...

  3. python3 流程控制

    表达式if ... else >>> if 3 > 4: ... print('False') ... else: ... print('True') ... True 表达式 ...

  4. Canvas裁剪和Region、RegionIterator

    主要是看这边文章学习:http://blog.csdn.net/lonelyroamer/article/details/8349601 Region.op参数 DIFFERENCE(0), //最终 ...

  5. C#隐藏tabcontrol

    //tabControl1.SizeMode = TabSizeMode.Fixed; //tabControl1.ItemSize = new Size(0, 1);

  6. 用python做中文自然语言预处理

    这篇博客根据中文自然语言预处理的步骤分成几个板块.以做LDA实验为例,在处理数据之前,会写一个类似于实验报告的东西,用来指导做实验,OK,举例: 一,实验数据预处理(python,结巴分词)1.对于爬 ...

  7. Spring Security(17)——基于方法的权限控制

    目录 1.1     intercept-methods定义方法权限控制 1.2     使用pointcut定义方法权限控制 1.3     使用注解定义方法权限控制 1.3.1    JSR-25 ...

  8. spring-事务实现原理

    spring事务的实现原理:aop. aop的两种实现方式: 1.动态代理: 事务方法与调用方法不能在同一个类中,否则事务不生效.解决方案,自己注入自己(实质注入的是代理类). 实现方式:jdk动态代 ...

  9. [河南省ACM省赛-第三届] 房间安排 (nyoj 168)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=168 分析:找到一天中需要最多的房间即可 #include<iostream> ...

  10. cocos2d-html5 之重要概念

    1,CCDirector:导演: 2,CCCamera: 摄像机:细到每个节点都要用摄像机,例如节点发生放大,缩小,旋转的时候,要继承摄像机,让其重新渲染: 3,CCScene:场景,拍电影时的一段剪 ...