批量添加:

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. jmeter(八)-JDBC请求(sqlserver)

    做JDBC请求,首先要了解这个JDBC对象是什么,然后寻找响应的数据库连接URL和数据库驱动. 数据库URL:jdbc:sqlserver://200.99.197.190:1433;database ...

  2. TypeScript -- 面向对象特性

    .class关键字和类名就可以定义一个类 . 类的访问控制符--有三个,.] = ] = ] = ;.声明参数 .用接口声明方法 .理解模块--一个文件就是一个模块,就是这么个意思 ,不用想的多么高大 ...

  3. [SOJ] Babelfish

    Description You have just moved from Waterloo to a big city. The people here speak an incomprehensib ...

  4. Lintcode解题报告

    1. Num.196 寻找缺失的数 给出一个包含 0 .. N 中 N 个数的序列,找出0 .. N 中没有出现在序列中的那个数. 注意事项 可以改变序列中数的位置. 您在真实的面试中是否遇到过这个题 ...

  5. 上锁 - leetcode

    158. Read N Characters Given Read4 II - Call multiple times 题目: The API: int read4(char *buf) reads ...

  6. Html5移动端页面自适应百分比布局

    按百分比布局,精度肯定不会有rem精确 <!DOCTYPE html> <html lang="en"> <head> <meta cha ...

  7. 视频编辑类sdk--lansoeditor--更新啦, 完全免费,欢迎下载

    当前版本是20160506 beta, 增加了兼容高通的64位硬件编码器增加MediaInfo类,您可以轻松获取多媒体中的各种信息.增加了演示的15个ffmpeg处理方法,您可以用这些方法实现秒拍图像 ...

  8. Eclipse 使用简记

    Eclipse 使用简记 本文针对 Eclipse Neon (4.6)版本进行说明,具体而言是 Eclipse IDE for Java EE Developers . 下载 Eclipse ecl ...

  9. python 主机宝

    需求:开发一个主机批量管理系统,要求按saltstack方式执行命令 #!/usr/bin/env python3. # -*- coding:utf8 -*- import os,sys,pickl ...

  10. 疯狂java讲义--笔记

    第一章.Java语言概述与开发环境 什么是软件:一系列按照特定顺序组织的计算机数据和指令的集合: 交互方式:两种 GUI(Graphical User Interface) 图像界面 .CLI (Co ...