当前位置:网站首页>Sqlserver common statements and functions
Sqlserver common statements and functions
2022-06-12 17:39:00 【Pick prosperity】
One 、SqlServer Common sentences
1、 Delete all tables
1.1、 Delete foreign key constraints for all tables ( Prevent table deletion failure due to foreign key constraints )
DECLARE c1 cursor for
select 'alter table ['+ object_name(parent_obj) + '] drop constraint ['+name+']; '
from sysobjects
where xtype = 'F'
open c1
declare @c1 varchar(8000)
fetch next from c1 into @c1
while(@@fetch_status=0)
begin
exec(@c1)
fetch next from c1 into @c1
end
close c1
deallocate c11.2、 Delete all stored procedures
use Database name ( Is the name of the database where the table is to be deleted )
GO
declare @sql varchar(8000)
while (select count(*) from sysobjects where type='P')>0
begin
SELECT @sql='drop Procedure ' + name
FROM sysobjects
WHERE (type = 'P')
ORDER BY 'drop Procedure ' + name
exec(@sql)
end1.3、 Delete all tables
use Database name ( Is the name of the database where the table is to be deleted )
GO
declare @sql varchar(8000)
while (select count(*) from sysobjects where type='U')>0
begin
SELECT @sql='drop table ' + name
FROM sysobjects
WHERE (type = 'U')
ORDER BY 'drop table ' + name
exec(@sql)
endTwo 、SqlServer Common functions
1、concert() function
concert() Function is a general function that converts dates to new data types .concert() Functions can display dates in different formats / Time data .
1.1、 grammar
CONVERT(data_type(length),data_to_be_converted,style)data_type(length) Specify the target data type ( With optional length ).data_to_be_converted Contains values that need to be converted .style Specified date / Time output format .
serviceable style value :
Style ID | Style Format |
|---|---|
100 perhaps 0 | mon dd yyyy hh:miAM ( perhaps PM) |
101 | mm/dd/yy |
102 | yy.mm.dd |
103 | dd/mm/yy |
104 | dd.mm.yy |
105 | dd-mm-yy |
106 | dd mon yy |
107 | Mon dd, yy |
108 | hh:mm:ss |
109 perhaps 9 | mon dd yyyy hh:mi:ss:mmmAM( perhaps PM) |
110 | mm-dd-yy |
111 | yy/mm/dd |
112 | yymmdd |
113 perhaps 13 | dd mon yyyy hh:mm:ss:mmm(24h) |
114 | hh:mi:ss:mmm(24h) |
120 perhaps 20 | yyyy-mm-dd hh:mi:ss(24h) |
121 perhaps 21 | yyyy-mm-dd hh:mi:ss.mmm(24h) |
126 | yyyy-mm-ddThh:mm:ss.mmm( There are no spaces ) |
130 | dd mon yyyy hh:mi:ss:mmmAM |
131 | dd/mm/yy hh:mi:ss:mmmAM |
1.2、 example
The following script uses concert() Function to display different formats . We will use GETDATE() Function to get the current date / Time :
CONVERT(VARCHAR(19),GETDATE())
CONVERT(VARCHAR(10),GETDATE(),110)
CONVERT(VARCHAR(11),GETDATE(),106)
CONVERT(VARCHAR(24),GETDATE(),113)The results are similar :
Dec 29 2008 11:45 PM
12-29-2008
29 Dec 08
29 Dec 2008 16:25:46.6352、count() function
2.1、count(1) and count(*)
When the table has more data , After analyzing the table , Use count(1) It's better than using count( * ) It takes a lot of time ! In terms of execution plan ,count(1) and count( * ) The effect is the same . But after the table has been analyzed ,count(1) than count( * ) We need less time (1w The amount of data ), But not much . If count(1) It's a clustered index ,id, That must be count(1) fast . But the difference is very small . because count( * ), Automatically optimize the assigned field . So there's no need to count(1), use count( * ),sql It will help you optimize therefore :count(1) and count( * ) Basically no difference !
2.2、count(1) and count( Field )
The main difference between the two is (1) count(1) Will count all the records in the table , The containing field is null The record of . (2) count( Field ) Count the number of times the field appears in the table , The ignored field is null The situation of . That is to say, do not count the fields as null The record of .
2.3、count(*) and count(1) and count( Name ) difference
On the effect of execution : count(*) It includes all the columns , Equivalent to the number of lines , When it comes to statistical results , Column values of... Are not ignored NULL count(1) Including ignoring all columns , use 1 Represents the line of code , When it comes to statistical results , Column values of... Are not ignored NULL count( Name ) Only include the column of the list , When it comes to statistical results , Will ignore null column values ( The null here is not just an empty string or 0, It means null) Count of , That is, the value of a field is NULL when , No statistics .
In terms of execution efficiency : The column name is primary key ,count( Name ) than count(1) fast Column name is not primary key ,count(1) than count( Name ) fast If the table has multiple columns and no primary key , be count(1) Is superior to the execution efficiency count( * ) If there is a primary key , be select count( Primary key ) The efficiency of execution is optimal If the table has only one field , be select count(*) The optimal .
3、len() function
len() Function returns the length of the value in the text field .
4、cast() function
4.1、CAST() The argument to the function is an expression , It includes the use of AS Key separated source and target data types . The following example is used to put a text string ’12’ Convert to integer :
SELECT CAST('12' AS int)4.2、 The return value is an integer value 12. If you try to convert a string representing a decimal to an integer value , What will happen ?
SELECT CAST('12.5' AS int) 4.3、CAST() Functions and CONVERT() Functions cannot perform rounding or truncation . because 12.5 Out-of-service int Data type , So a call to this function will cause an error :
Server: Msg 245, Level 16, State 1, Line 1 Syntax error converting the varchar value '12.5' to a column of data type int.4.4、 To return a valid value , You must use a data type that can handle this value . For this example , There are multiple data types available . If you pass CAST() Function converts this value to decimal type , You need to first define decimal The precision of the value and the number of decimal places . In this case , The precision and decimal places are respectively 9 And 2. Precision is the total number of digits , Include the sum of the digits to the left and right of the decimal point . And the number of decimal places is the number to the right of the decimal point . This means that the largest integer value that this example can support is 9999999, And the smallest decimal is 0.01.
SELECT CAST('12.5' AS decimal(9,2))decimal The data type displays significant decimal places in the results grid : 12.50
4.5、 The default values for precision and scale are 18 And 0. If in decimal These two values are not provided in the type ,SQL Server The decimal part of the number will be truncated , Without making mistakes .
SELECT CAST('12.5' AS decimal)The result is an integer value :12
边栏推荐
- Some introduction to FPC flexible circuit board design
- R language arma-garch-copula model and financial time series case
- JDBC几个坑
- Is Huishang futures company reliable in opening accounts and safe in trading?
- 论文《Deep Interest Evolution Network for Click-Through Rate Prediction》
- String s = null ; String s = new String();String s =““ ;String s ;有什么区别?
- 5-5 configuring MySQL replication log point based replication
- Making nearly $90billion, Buffett's latest heavy stock exposure
- LCD参数解释及计算
- I heard that distributed IDS cannot be incremented globally?
猜你喜欢

内核中断整体流程图

Arm64棧回溯

Volcano engine held a video cloud technology force summit and released a new experience oriented video cloud product matrix

Compilation optimization of performance optimization

Detailed explanation of shardingjdbc database and table

Exclusive interview with oppo find X5 Product Manager: deeply cultivate self-developed chips to create the ultimate flagship experience with the highest standards

The significance of writing technology blog

"Upgrade strategy" of two new committers

Application case of smart micro 32-bit MCU for server application cooling control

Interesting LD_ PRELOAD
随机推荐
Cicada mother talks to rainbow couple: 1.3 billion goods a year, from e-commerce beginners to super goods anchor
R语言使用epiDisplay包的tabpct函数生成二维列联表并使用马赛克图可视化列联表(二维列联表、边际频数、以及按行、按列的比例)、自定义设置ylab参数设置Y轴的轴标签文本(y axis)
借助SpotBugs将程序错误扼杀在摇篮中
分辨率与行场同步信号的关系 场消隐
Lambda - 1
73. 矩阵置零(标记法)
1723. minimum time to complete all work
Risc-v ide mounriver studio v1.60 update point introduction
R语言使用pdf函数将可视化图像结果保存到pdf文件中、使用pdf函数打开图像设备、使用dev.off函数关闭图像设备、自定义width参数和height参数指定图像的宽度和高度
Vulnhub[DC3]
Kali2022安装armitage
字节飞书人力资源套件三面
566. 重塑矩阵
认识函数原创
Sizepolicy policy in layout management
String s = null ; String s = new String();String s =““ ;String s ;有什么区别?
LCD参数解释及计算
How to win the "Olympic Games" in retail technology for jd.com, the learning tyrant of the "regular examination"?
Use of split method of string
MySQL transaction introduction and transaction isolation level