sql server 我常用的语句
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 我常用的语句的更多相关文章
- MS SQL SERVER 2000 常用 Tran-SQL 语句
一.创建数据库:create database mydb-创建数据库mydbon primary-在primary文件组中( name = mydb_data1,filename = 'd:\sql ...
- SQL Server中常用的SQL语句(转):
SQL Server中常用的SQL语句 转自:http://www.cnblogs.com/rainman/archive/2013/05/04/3060428.html 1.概述 名词 笛卡尔积.主 ...
- 处理SQL Server 异常常用步骤
处理SQL Server 异常常用步骤 SQL Server常见的问题主要是SQL问题造成,常见的主要是CPU过高和阻塞. 一.CPU过高的问题 1.查询系统动态视图查询执行时间长的sql语句 WIT ...
- PowerDesigner反向数据库时遇到[Microsoft][ODBC SQL Server Driver][SQL Server]无法预定义语句。SQLSTATE = 37错误解决方法
逆向工程中,有时会出现如下错误 ... [Microsoft][ODBC SQL Server Driver][SQL Server]无法预定义语句 SQLSTATE = 37000 解决方案: 1. ...
- SQL Server Profiler监控执行语句
SQL Server Profiler监控执行语句,这个功能主要用在实时的监控对数据库执行了什么操作,从而及时有效的跟踪系统的运行. 常规配置选项,名称.模板.保存到文件(可以复用). 事件选择,可以 ...
- SQL Server FOR XML PATH 语句的应用---列转行
经常在论坛看到高手使用了 for xml path,由于是搜索一下,记录了详细的使用方法.在SQL Server中利用 FOR XML PATH 语句能够把查询的数据生成XML数据,下面是它的一些应用 ...
- SQL Server中的流控制语句
begin···end 该语句定义sql代码块,通常在if和while语句中使用 declare @num int ; ; begin ; print 'hello word' end if···el ...
- 【SQL Server DBA】维护语句:删除并创建外键约束、获取建表语句
原文:[SQL Server DBA]维护语句:删除并创建外键约束.获取建表语句 1.删除外键约束,建立外键约束 先建立3个表: /* drop table tb drop table tb_b dr ...
- SQL Server中常用的SQL语句
1.概述 名词 笛卡尔积.主键.外键 数据完整性 实体完整性:主属性不能为空值,例如选课表中学号和课程号不能为空 参照完整性:表中的外键取值为空或参照表中的主键 用户定义完整性:取值范围或非空限制,例 ...
随机推荐
- k8s build new API
apiserver-builder git hub api conventions storage api arch step by step, we can follow it. api con ...
- 快速阅读《QT5.9 c++开发指南》1
简介:<QT5.9 c++开发指南>的作者是和i三位主要从事地球物理探测仪器设计.数据处理方法研究和软件开发等工作的博士们,这本书以QT Widget为主要内容,比较全面地教授了QT开发桌 ...
- 20145308 《网络对抗》 逆向及BOF基础实践 学习总结
20145308 <网络对抗> 逆向及BOF基础实践 学习总结 实践目的 通过两种方法,实现程序能够运行原本并不会被运行的代码 实践原理 利用foo函数的Bof漏洞,构造一个攻击输入字符串 ...
- 前端基础小标签3 H5新标签
第二部分H5的新标签一.<!-- mark标签 1.用于显示页面中需要重点关注的内容,就像看书需要用荧光笔划重点一样 2.浏览器通常会用黄色显示m ...
- Git和Jenkins日记之没有新提交代码
日期:2017/3/9 今天查看Jenkins运行代码记录的日志时,发现并没有昨天新提交的代码,然后查看了Jenkins的测试项目中所有的自动化测试用例, 并没有看到昨天新提交的测试用例,又查看了gi ...
- Module controller in JMeter
https://qualibrate.com/blog/quality-assurance/jmeter-module-controller/ 通过组合Test Fragments 和Module C ...
- P3975 [TJOI2015]弦论
思路 一眼SAM板子,结果敲了一中午... 我还是太弱了 题目要求求第k小的子串 我们可以把t=0当成t=1的特殊情况,(所有不同位置的相同子串算作一个就是相当于把所有子串的出现位置个数(endpos ...
- Twitter开发2
There are different API families The standard (free) Twitter APIs consist of REST APIs and Streaming ...
- Latex: 减少图与文字之间的空白间隙
参考: Remove space after figure and before text Latex: 减少图与文字之间的空白间隙 论文中图与文字之间的空白间隙过大,导致排版不大美观.解决方法是在\ ...
- JavaScript——执行环境、变量对象、作用域链
前言 这几天在看<javascript高级程序设计>,看到执行环境和作用域链的时候,就有些模糊了.书中还是讲的不够具体.通过上网查资料,特来总结,以备回顾和修正. 目录: EC(执行环境或 ...