1. computed column

alter table tableName add columnName as (cast(aColumn as float) / bColumn * 100) persisted; 

2. unique nullable

create unique nonclustered index[UniqueName] on [tableName]([columnNameA] asc) where ([columnNameA] is not null);

3. foreign key cascade delete

alter table [tableNameA] drop constraint [FKName];
alter table [tableNameA] with check add constraint [FKName] foreign key(foreignColumnName) references [tableNameB]([Id]) on delete cascade -- or on delete set null;

4. reset auto increment

dbcc checkident ('tableName') --check current
dbcc checkident ('tableName', reseed, 0); --reset to 0 , next is 1

5. create index

create nonclustered index [indexName] on [tableName]([columnName] asc);

6. select Ids to string list

declare @string nvarchar(MAX);
select @string = coalesce(@string + ', ', '') + cast(Id as nvarchar(5))
from Products;
select @string;

6.01. delay

declare @delayLength char(8) = '00:00:10';
waitfor delay @DelayLength

7. 大杂烩

use [simple];

drop proc dbo.performance_tuning_createRandomData;
-- create procedure
go
create proc dbo.performance_tuning_createRandomData
(
@name nvarchar(100),
@email nvarchar(100),
@outValue nvarchar(100) output
)
as
begin
set nocount on;
set @outValue = N'output ok';
declare @value nvarchar(max) = N'dada';
set @value = N'super';
--print @value;
insert into dbo.test (name,email) values (@name,@email);
end
go
go
declare @outValue nvarchar(100);
EXEC dbo.performance_tuning_createRandomData N'mk100', N'hengkeat87@gmail.com', @outValue output;
print @outvalue;
go -- create function
drop function dbo.performance_tuning_randomString;
go
create function dbo.performance_tuning_randomString
(
@name nvarchar(100)
)
returns nvarchar(50)
as
begin
return 'value';
end
go
select dbo.performance_tuning_randomString('dada'); -- declare and set value
go
declare @value nvarchar(max) = N'dada';
set @value = N'super';
-- select @value = count(*) from @table;
print @value;
go -- if else, begin end 不一定需要
go
declare @value2 nvarchar(30) = 'keatkeat2';
if(@value2 = 'keatkeat')
begin
print 'yes';
end
else
begin
print 'no';
end
go -- for loop
go
declare @i int = 0;
while(@i < 10)
begin
print @i;
set @i = @i + 1;
end
go -- random str + number and random number
go SELECT CONVERT(INT, 5 * RAND())
SELECT SUBSTRING(CONVERT(varchar(255), NEWID()), 0, 9)
select NEWID();
go -- create temp table and loop it
go
declare @temp table (name nvarchar(max), [index] int identity(1,1));
insert into @temp (name) values ('keatkeat'), ('xinyao'); declare @i int = 0;
declare @length int;
select @length = count(*) from @temp;
while(@i < @length)
begin
declare @value nvarchar(max)
select @value = name from @temp order by [index] offset @i rows fetch next 1 rows only;
print @value;
set @i = @i + 1;
end
go -- copy table to other table (cross database)
-- copy table to temp table
go
insert into test (email,[name]) select email,[name] from performance_tuning.dbo.Products where Id = 1; select Id,[name] into #temp from test order by Id;
declare @i int = 0;
declare @length int;
select @length = count(*) from #temp;
print @length;
while(@i < @length)
begin
declare @value nvarchar(max)
select @value = [name] from #temp order by Id offset @i rows fetch next 1 rows only;
print @value;
set @i = @i + 1;
end
drop table #temp;
go -- random enum
go
declare @values table([value] nvarchar(25));
insert into @values values ('John'),('Sarah'),('George');
select top 1 * from @values order by newid(); --or rand()
go

杂会 2

--dynamic sql
declare @table nvarchar(max) = 'table';
declare @value nvarchar(max) = 'value';
--input
declare @count int;
--output
declare @sql nvarchar(max) = N'select @count = count(*) from ' + @table + ' where column = @value';
exec sp_executesql
@sql,
N'@count INT OUT, @value nvarchar(max)',
@value = @value,
@count = @count output;
print @count; -- create temp table to store data
go
create table #tempTable
(
Id int
);
declare @sql nvarchar(max) = N'insert into #tempTable (Id) select Id from Questions';
EXEC(@sql);
select *
from #tempTable;
drop table #tempTable;
go

dynamic table name

declare @count int;
declare @sql nvarchar(max) = N'select @count = count(*) from ' + @name;
exec sp_executesql @sql, N'@count int out', @count out
print @count;

sql server 我常用的语句的更多相关文章

  1. MS SQL SERVER 2000 常用 Tran-SQL 语句

    一.创建数据库:create database mydb-创建数据库mydbon primary-在primary文件组中( name = mydb_data1,filename = 'd:\sql ...

  2. SQL Server中常用的SQL语句(转):

    SQL Server中常用的SQL语句 转自:http://www.cnblogs.com/rainman/archive/2013/05/04/3060428.html 1.概述 名词 笛卡尔积.主 ...

  3. 处理SQL Server 异常常用步骤

    处理SQL Server 异常常用步骤 SQL Server常见的问题主要是SQL问题造成,常见的主要是CPU过高和阻塞. 一.CPU过高的问题 1.查询系统动态视图查询执行时间长的sql语句 WIT ...

  4. PowerDesigner反向数据库时遇到[Microsoft][ODBC SQL Server Driver][SQL Server]无法预定义语句。SQLSTATE = 37错误解决方法

    逆向工程中,有时会出现如下错误 ... [Microsoft][ODBC SQL Server Driver][SQL Server]无法预定义语句 SQLSTATE = 37000 解决方案: 1. ...

  5. SQL Server Profiler监控执行语句

    SQL Server Profiler监控执行语句,这个功能主要用在实时的监控对数据库执行了什么操作,从而及时有效的跟踪系统的运行. 常规配置选项,名称.模板.保存到文件(可以复用). 事件选择,可以 ...

  6. SQL Server FOR XML PATH 语句的应用---列转行

    经常在论坛看到高手使用了 for xml path,由于是搜索一下,记录了详细的使用方法.在SQL Server中利用 FOR XML PATH 语句能够把查询的数据生成XML数据,下面是它的一些应用 ...

  7. SQL Server中的流控制语句

    begin···end 该语句定义sql代码块,通常在if和while语句中使用 declare @num int ; ; begin ; print 'hello word' end if···el ...

  8. 【SQL Server DBA】维护语句:删除并创建外键约束、获取建表语句

    原文:[SQL Server DBA]维护语句:删除并创建外键约束.获取建表语句 1.删除外键约束,建立外键约束 先建立3个表: /* drop table tb drop table tb_b dr ...

  9. SQL Server中常用的SQL语句

    1.概述 名词 笛卡尔积.主键.外键 数据完整性 实体完整性:主属性不能为空值,例如选课表中学号和课程号不能为空 参照完整性:表中的外键取值为空或参照表中的主键 用户定义完整性:取值范围或非空限制,例 ...

随机推荐

  1. 应使用sqlplus代替tnsping进行oracle连通性测试

    一直以来,都习惯于tnsping alias测试确定使用了那个sqlnet.ora,并测试连通性.最近在制作系统的安装包,为了轻量级以及提高实施效率,全部客户端使用oracle instant cli ...

  2. 教你用Visual Studio Code做PHP开发 - 微软官方工具,IDE中的黑马

    转载于:http://bbs.wfun.com/thread-902655-1-1.html,仅供自己备忘 本文为我在智机网的原创  ] 关于Visual Studio Code,可能有的开发者很陌生 ...

  3. 热扩容LVM形式的/(根)分区(无损增大、缩小LVM分区)

    警告! 本文为虚拟机环境,生产环境请务必在操作前优先备份重要数据! 再有,请确保所需扩充的分区为非进程占用分区 实验背景:当时规划系统分区时/(根)目录分配过小 实验目的 : 无损增大/(根)分区容量 ...

  4. Linux基础笔记—— 走进Linux

    走进Linux 操作系统 操作系统是计算机中必不可少的基础系统软件,他的作用是管理和控制计算机系统中的硬件和软件资源,合理有效的组织系统的工作流程,在计算机系统(硬件)与使用者之间提供接口作用. 操作 ...

  5. (4运行例子)自己动手,编写神经网络程序,解决Mnist问题,并网络化部署

    ​1.联通ColaB 2.运行最基础mnist例子,并且打印图表结果  # https://pypi.python.org/pypi/pydot#!apt-get -qq install -y gra ...

  6. NOIP 2016 天天爱跑步 (luogu 1600 & uoj 261) - 线段树

    题目传送门 传送点I 传送点II 题目大意 (此题目不需要大意,我认为它已经很简洁了) 显然线段树合并(我也不知道哪来这么多显然) 考虑将每条路径拆成两条路径 s -> lca 和 t -> ...

  7. easyui中datagrid常见功能

    1.数据加载,需要拼接成标准json格式{}.如果是jsonarray格式[{},{}],无法识别. 2.后端将list拼接成datagrid能识别的json格式,需要首先new JSONObject ...

  8. 内置函数之sorted,filter,map

    # 4,用map来处理字符串列表,把列表中所有人都变成sb,比方alex_sb # name=['oldboy','alex','wusir'] # print(list(map(lambda i:i ...

  9. C# 尝试读取或写入受保护的内存。这通常指示其他内存已损坏

    用管理员身份运行CMD,输入netsh winsock reset并回车(注意,必须是已管理员身份运行,这个重置LSP连接)运行后提示要重启生效,结果没重启就OK了(重启不重启看最终效果).

  10. linux内核中的linuxPPS是什么?

    答: linux每秒脉冲数(linux pulse per second),LinuxPPS 在系统中提供一个编程接口(API)去定义几个PPS源; 一个PPS源就是一个每秒能提供高精度信号的设备,以 ...