1. computed column

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

2. unique nullable

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

3. foreign key cascade delete

  1. alter table [tableNameA] drop constraint [FKName];
  2. 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

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

5. create index

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

6. select Ids to string list

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

6.01. delay

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

7. 大杂烩

  1. use [simple];
  2.  
  3. drop proc dbo.performance_tuning_createRandomData;
  4. -- create procedure
  5. go
  6. create proc dbo.performance_tuning_createRandomData
  7. (
  8. @name nvarchar(100),
  9. @email nvarchar(100),
  10. @outValue nvarchar(100) output
  11. )
  12. as
  13. begin
  14. set nocount on;
  15. set @outValue = N'output ok';
  16. declare @value nvarchar(max) = N'dada';
  17. set @value = N'super';
  18. --print @value;
  19. insert into dbo.test (name,email) values (@name,@email);
  20. end
  21. go
  22. go
  23. declare @outValue nvarchar(100);
  24. EXEC dbo.performance_tuning_createRandomData N'mk100', N'hengkeat87@gmail.com', @outValue output;
  25. print @outvalue;
  26. go
  27.  
  28. -- create function
  29. drop function dbo.performance_tuning_randomString;
  30. go
  31. create function dbo.performance_tuning_randomString
  32. (
  33. @name nvarchar(100)
  34. )
  35. returns nvarchar(50)
  36. as
  37. begin
  38. return 'value';
  39. end
  40. go
  41. select dbo.performance_tuning_randomString('dada');
  42.  
  43. -- declare and set value
  44. go
  45. declare @value nvarchar(max) = N'dada';
  46. set @value = N'super';
  47. -- select @value = count(*) from @table;
  48. print @value;
  49. go
  50.  
  51. -- if else, begin end 不一定需要
  52. go
  53. declare @value2 nvarchar(30) = 'keatkeat2';
  54. if(@value2 = 'keatkeat')
  55. begin
  56. print 'yes';
  57. end
  58. else
  59. begin
  60. print 'no';
  61. end
  62. go
  63.  
  64. -- for loop
  65. go
  66. declare @i int = 0;
  67. while(@i < 10)
  68. begin
  69. print @i;
  70. set @i = @i + 1;
  71. end
  72. go
  73.  
  74. -- random str + number and random number
  75. go
  76.  
  77. SELECT CONVERT(INT, 5 * RAND())
  78. SELECT SUBSTRING(CONVERT(varchar(255), NEWID()), 0, 9)
  79. select NEWID();
  80. go
  81.  
  82. -- create temp table and loop it
  83. go
  84. declare @temp table (name nvarchar(max), [index] int identity(1,1));
  85. insert into @temp (name) values ('keatkeat'), ('xinyao');
  86.  
  87. declare @i int = 0;
  88. declare @length int;
  89. select @length = count(*) from @temp;
  90. while(@i < @length)
  91. begin
  92. declare @value nvarchar(max)
  93. select @value = name from @temp order by [index] offset @i rows fetch next 1 rows only;
  94. print @value;
  95. set @i = @i + 1;
  96. end
  97. go
  98.  
  99. -- copy table to other table (cross database)
  100. -- copy table to temp table
  101. go
  102. insert into test (email,[name]) select email,[name] from performance_tuning.dbo.Products where Id = 1;
  103.  
  104. select Id,[name] into #temp from test order by Id;
  105. declare @i int = 0;
  106. declare @length int;
  107. select @length = count(*) from #temp;
  108. print @length;
  109. while(@i < @length)
  110. begin
  111. declare @value nvarchar(max)
  112. select @value = [name] from #temp order by Id offset @i rows fetch next 1 rows only;
  113. print @value;
  114. set @i = @i + 1;
  115. end
  116. drop table #temp;
  117. go
  118.  
  119. -- random enum
  120. go
  121. declare @values table([value] nvarchar(25));
  122. insert into @values values ('John'),('Sarah'),('George');
  123. select top 1 * from @values order by newid(); --or rand()
  124. go

杂会 2

  1. --dynamic sql
  2. declare @table nvarchar(max) = 'table';
  3. declare @value nvarchar(max) = 'value';
  4. --input
  5. declare @count int;
  6. --output
  7. declare @sql nvarchar(max) = N'select @count = count(*) from ' + @table + ' where column = @value';
  8. exec sp_executesql
  9. @sql,
  10. N'@count INT OUT, @value nvarchar(max)',
  11. @value = @value,
  12. @count = @count output;
  13. print @count;
  14.  
  15. -- create temp table to store data
  16. go
  17. create table #tempTable
  18. (
  19. Id int
  20. );
  21. declare @sql nvarchar(max) = N'insert into #tempTable (Id) select Id from Questions';
  22. EXEC(@sql);
  23. select *
  24. from #tempTable;
  25. drop table #tempTable;
  26. go

dynamic table name

  1. declare @count int;
  2. declare @sql nvarchar(max) = N'select @count = count(*) from ' + @name;
  3. exec sp_executesql @sql, N'@count int out', @count out
  4. 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. Hibernate properties文件

    ###################### ### Query Language ### ###################### ## define query language consta ...

  2. 选择排序法、冒泡排序法、插入排序法、系统提供的底层sort方法排序之毫秒级比较

    我的代码: package PlaneGame;/** * 选择排序法.冒泡排序法.插入排序法.系统提供的底层sort方法排序之毫秒级比较 * @author Administrator */impo ...

  3. thinkphp5 tp5 与 nginx 搭配在根目录和子目录中如何设置伪静态

    配置文件参考一下: location /public/ { if (!-e $request_filename){ rewrite ^/public/(.*)$ /public/index.php?s ...

  4. Thinkphp5 分页带参数

    原文链接:http://www.zhaisui.com/article/51.html

  5. Codeforces Round #439 (Div. 2) Problem A (Codeforces 869A) - 暴力

    Rock... Paper! After Karen have found the deterministic winning (losing?) strategy for rock-paper-sc ...

  6. fastjson常用方法

    fastjson是一款alibaba公司开发的json工具包.json经常被使用在数据传输方面,因此特意对它的一些常用方法做备注,欢迎看客在评论区补充或指出问题. 首先定义一个实体类,用于我们进行对象 ...

  7. HTML DOM open() 方法

    HTML DOM Window 对象 定义和用法 open() 方法用于打开一个新的浏览器窗口或查找一个已命名的窗口. 语法 window.open(URL,name,features,replace ...

  8. linux内核中的vgaarb是什么?

    答: vga仲裁器(vga arbiter),是内核中的一个模块

  9. nowcoder 合并回文子串

    链接:https://www.nowcoder.com/acm/contest/6/C来源:牛客网题目输入两个字符串A和B,合并成一个串C,属于A和B的字符在C中顺序保持不变.如"abc&q ...

  10. (转载)WinformGDI+入门级实例——扫雷游戏(附源码)

    本文将作为一个入门级的.结合源码的文章,旨在为刚刚接触GDI+编程或对相关知识感兴趣的读者做一个入门讲解.游戏尚且未完善,但基本功能都有,完整源码在文章结尾的附件中. 整体思路: 扫雷的游戏界面让我从 ...