`
7wolfs
  • 浏览: 177872 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

SQL of SQL Server

阅读更多
物化视图
ALTER VIEW view_name WITH SCHEMABINDING AS  
SELECT ***
必须使视图使用SCHEMABINDING选项


CREATE UNIQUE CLUSTERED INDEX ivCustomerOrders 
ON view_name(col1,col2); 



update and select
UPDATE tblSearchOutput  SET  tblSearchOutput.outputName = tblEntityField.Description 
FROM tblEntityField  
join  tblSearchOutput  on tblSearchOutput.EntityFieldId = tblEntityField.EntityFieldId 
WHERE tblEntityField.entityId = 1 


1. 查询表中的字段定义
SELECT name FROM syscolumns WHERE (id = (SELECT id FROM sysobjects WHERE (name = '这里写你的表名')))


2. SQL Server 区分大小写
---查看数据库的大小写设置
exec sp_server_info

查看属性“COLLATION_SEQ”的值“charset=cp936 collation=Chinese_PRC_CI_AS”


---改变SQL Server的大小写
a) 修改列的属性
ALTER   TABLE   tableA  
ALTER   COLUMN   cola   nvarchar(100)   COLLATE   Chinese_PRC_CS_AS


select * from tableA where cola = 'test'



b) 修改查询SQL
select * from tableA where cola   collate   Chinese_PRC_CS_AS_WS   =  'test'


c) 修改数据库属性
alter database 数据库 COLLATE Chinese_PRC_CS_AS


d) 创建如下用户自定义函数(UDF)
CREATE FUNCTION StrComp(@Str1 VARCHAR(50),@Str2 VARCHAR(50))
--ALTER FUNCTION StrComp(@Str1 VARCHAR(50),@Str2 VARCHAR(50))
RETURNS INTEGER
AS
BEGIN
  DECLARE @i INTEGER
  --DECLARE @Str1 VARCHAR(50)
  --DECLARE @Str2 VARCHAR(50)
  DECLARE @y INT
  --SET @Str1='a'
  --SET @Str2='A'
  SET  @i=0
  --SELECT ASCII(SUBSTRING(@Str1,@i+1,1))
  SET @y=1
  DECLARE @iLen INT
  SET @iLen = LEN(LTRIM(RTRIM(@Str1)))
  IF LEN(LTRIM(RTRIM(@Str1))) < LEN(LTRIM(RTRIM(@Str2))) --THEN
     SET @iLen = LEN(LTRIM(RTRIM(@Str2)))
  WHILE (@i < @iLen)
    BEGIN
      IF (ASCII(SUBSTRING(@Str1,@i+1,1))=ASCII(SUBSTRING(@Str2,@i+1,1))) --THEN
         SET @i = @i +1
      ELSE
         BEGIN
           SET @y=0
           BREAK
         END
      END
     RETURN @y
END

测试:
select * from Table1 Where dbo.StrComp(Field1,'aAbB') =1


---语法
引用

理解collate Chinese_PRC_CI_AS NULL
 

 

    我们在create table时经常会碰到这样的语句,例如:

password nvarchar(10)collate chinese_prc_ci_as null,

    那它到底是什么意思呢?不妨看看下面:

    首先,collate是一个子句,可应用于数据库定义或列定义以定义排序规则,或应用于字符串表达式以应用排序规则投影。语法是:

collate collation_name

collation_name ::={windows_collation_name}|{sql_collation_name}

    参数collate_name是应用于表达式、列定义或数据库定义的排序规则的名称。collation_name 可以只是指定的 Windows_collation_name 或SQL_collation_name。

    Windows_collation_name 是 Windows 排序规则的排序规则名称。参见 Windows 排序规则名称。
    SQL_collation_name 是 SQL 排序规则的排序规则名称。参见 SQL 排序规则名称。

    下面简单介绍一下排序规则:

    什么叫排序规则呢?MS是这样描述的:"在 Microsoft SQL Server 2000 中,字符串的物理存储由排序规则控制。排序规则指定表示每个字符的位模式以及存储和比较字符所使用的规则。"
  在查询分析器内执行下面语句,可以得到SQL SERVER支持的所有排序规则。

    select * from ::fn_helpcollations()

    排序规则名称由两部份构成,前半部份是指本排序规则所支持的字符集。如:
  Chinese_PRC_CS_AI_WS
    前半部份:指UNICODE字符集,Chinese_PRC_指针对大陆简体字UNICODE的排序规则。
    排序规则的后半部份即后缀 含义:
  _BIN 二进制排序
  _CI(CS) 是否区分大小写,CI不区分,CS区分
  _AI(AS) 是否区分重音,AI不区分,AS区分   
  _KI(KS) 是否区分假名类型,KI不区分,KS区分 
    _WI(WS) 是否区分宽度 WI不区分,WS区分 


    区分大小写:如果想让比较将大写字母和小写字母视为不等,请选择该选项。
    区分重音:如果想让比较将重音和非重音字母视为不等,请选择该选项。如果选择该选项,比较还将重音不同的字母视为不等。
    区分假名:如果想让比较将片假名和平假名日语音节视为不等,请选择该选项。
    区分宽度:如果想让比较将半角字符和全角字符视为不等,请选择该选项 



---SQL Server中创建视图注意
引用

1. 不能再查询定义中使用order by,除非定义中包含TOP或FOR XML运算符.
(注:
i.TOP不是ANSI的标准语句;
ii.视图是虚拟表,作为一个逻辑实体,表中的行没有顺序,可以在外部查询中对行排序)
2. 所有的结果列必须有名称.因此最好不使用"SELECT *"这种语句
3. 所有结果列的名称必须是唯一的

基本表的架构更改时,要及时刷新视图的元数据信息,使用sp_refreshview可实现此功能:

exec sp_refreshview 'view_name'


--operation on View
create view 
Alter view 
drop view


  • coalesce函数


SqlServer数据库中coalesce函数用法详解,在SqlServer2005中有了新的函数,它非常的实用,它就是coalesce函数,此函数可以返回参数中的第一个非空表达式,当你要在n个字段中选取某一个非空值可以用它,下面来看一个小小的示例:帮助你理解此函数

示例如下:
 select Coalesce(null,null,1,2,null)union  

select Coalesce(null,11,12,13,null)union  

select Coalesce(111,112,113,114,null)  


输出结果:

1  

11  

111  

如果此函数为诞生前可以用case when嵌套完成此功能,但是要写大量的代码,如果用此函数,代码比较的简单清晰。


分享到:
评论

相关推荐

    [SQL Server] Microsoft SQL Server 2012 技术内幕 (英文版)

    Explore the core engine of Microsoft SQL Server 2012—and put that practical knowledge to work. Led by a team of SQL Server experts, you’ll learn the skills you need to exploit key architectural ...

    Microsoft SQL Server Native Client (SQL Native Client)

    SQL Native Client This redistributable installer for the installation to run the client components needed to take advantage of SQL Server 2005' s new features, it also can be installed using the SQL ...

    sqlserver常用sql

    死锁的诊断和定位 查询阻塞的语句 查询执行较慢的语句 查询正在执行的语句1 查询正在执行的语句2 查询所有表的主键 查询所有索引 查询表结构 ...修改SQLServer最大内存 用DAC连接到SQL Server 其它SQL DBCC

    Data Science with SQL Server

    (RDBMS), and one of the most widely used RDBMS is Microsoft SQL Server. Much more than a database server, it’s a rich ecostructure with advanced analytic capabilities. Microsoft SQL Server R Services...

    SQL Server for C# Developers Succinctly

    Developers of C# applications with a SQL Server database can learn to connect to a database using classic ADO.NET and look at different methods of developing databases using the Entity Framework....

    Beginning Transact-SQL with SQL Server 2000 and 2005 (4 of 5)

    Beginning Transact-SQL with SQL Server 2000 and 2005 byPaul TurleyandDan Wood Wrox Press 2006 (594 pages) ISBN:076457955X Prepare for the ever-increasing demands of programming....

    Microsoft SQL Server 2012 Bible

    Microsoft SQL Server 2012 is the most significant update to this product since 2005, and it may change how database administrators and developers perform many aspects of their jobs. If you're a ...

    Fundamentals of SQL Server 2012 Replication

    SQL Server Replication入门,主要讲的是Transaction Replication。

    SQL SERVER T-SQL

    By working through 10-minute lessons, you’ll learn everything you need to know to take advantage of Microsoft SQL Server’s T-SQL language. This handy pocket guide starts with simple data ...

    php的serverSQL 2008的带PDO扩展支持

    Today at DrupalCon SF 2010, we are reaching an important milestone by releasing a Community Technology Preview (CTP) of the new SQL Server Driver for PHP 2.0, which includes support for ...

    Pro.SQL.Server.Internals

    New in this second edition is coverage of SQL Server 2016 Internals, including In-Memory OLTP, columnstore enhancements, Operational Analytics support, Query Store, JSON, temporal tables, stretch ...

    Microsoft SQL Server 2008 - T-SQL Querying March 2009

    defi nition, design, and implementation of the Query Processing engine of SQL Server for over a decade, and I deeply respect his insight. They make an outstanding team of guides who can help you ...

    Techfocus SQL Server 2008 Migration: Evaluate the Value of a SQL Server Upgrade

    SQL Server 11-officially named SQL Server 2008 R2-is due out in the first half of 2010. The next step is to consider your reasons for an upgrade. Different industries will have different motivations...

    SSMA6.0(oracle迁移到sqlserver)

    oracle资料库资料迁移到MS-sql(sqlserver)工具,版本为6.0 如果连oracle资料库提示以下错误时, Unable to find specified provider. Error occurred while establishing connection to Oracle server. You might ...

    Beginning T-SQL with Microsoft SQL Server 2005 and 2008

    Beyond the basics of database object management and security concepts, we recommend Beginning SQL Server 2005 Administration and Beginning SQL Server 2008 Administration from Wrox, co-authored in ...

    Pro.SQL.Server.Wait.Statistics.1484211405

    or already familiar with them, this book will help you gain a deeper understanding of how wait statistics are generated and what they can mean for your SQL Server’s performance. Besides the most ...

    Dynamic SQL: Applications, Performance, and Security in SQL Server 2nd Edition

    Take a deep dive into the many uses of dynamic SQL in Microsoft SQL Server. This edition has been updated to use the newest features in SQL Server 2016 and SQL Server 2017 as well as incorporating the...

    Murach's SQL Server 2016 for Developers

    explanation of the many new features in SQLServer 2016. Some best practices are covered as well. Chapter 3, SQL Server Tools, helps you understand the changes in the release management of SQL Server ...

    SQLServer2005考试题及答案

    SQLServer考试题及答案 选择题: 1、在MS SQL Server中,用来显示数据库信息的系统存储过程是( ) A sp_ dbhelp B sp_ db C sp_ help D sp_ helpdb 2、SQL语言中,删除一个表的命令是( ) A DELETE B DROP C ...

    [SQL工具] dbForge Data Studio for SQL Server 2.0.48 破解版

    ☆ 资料说明:☆ 压缩包内包含安装程序和破解...Data Studio collects approved functionality to provide advanced data management in SQL Server databases and assists in every task with any amount of data.

Global site tag (gtag.js) - Google Analytics