案例
例如1:一張表有一萬多條記錄,表的第一個字段 RecID 是自增長字段, 寫一個SQL語句, 找出表的第31到第40個記錄。
select top 10 recid from A where recid not in(select top 30 recid from A)
分析:如果這樣寫會產生某些問題,如果recid在表中存在邏輯索引。
select top 10 recid from A where……是從索引中查找,而後面的select top 30 recid from A則在數據表中查找,這樣由於索引中的順序有可能和數據表中的不一致,這樣就導致查詢到的不是本來的欲得到的數據。
解決方案
1, 用order by select top 30 recid from A order by ricid 如果該字段不是自增長,就會出現問題
2, 在那個子查詢中也加條件:select top 30 recid from A where recid>-1
例2:查詢表中的最後以條記錄,並不知道這個表共有多少數據,以及表結構。
set @s = 'select top 1 * from T where pid not in (select top ' + str(@count-1) + ' pid from T)'
print @s exec sp_executesql @s
9:獲取當前數據庫中的所有用戶表
select Name from sysobjects where xtype='u' and status>=0
10:獲取某一個表的所有字段
select name from syscolumns where id=object_id('表名')
select name from syscolumns where id in (select id from sysobjects where type = 'u' and name = '表名')
兩種方式的效果相同
11:查看與某一個表相關的視圖、存儲過程、函數
select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like '%表名%'
12:查看當前數據庫中所有存儲過程
select name as 存儲過程名稱 from sysobjects where xtype='P'
13:查詢用戶創建的所有數據庫
select * from master..sysdatabases D where sid not in(select sid from master..syslogins where name='sa')
或者
select dbid, name AS DB_NAME from master..sysdatabases where sid <> 0x01
14:查詢某一個表的字段和數據類型
select column_name,data_type from information_schema.columns
where table_name = '表名'
15:不同服務器數據庫之間的數據操作
--創建鏈接服務器
exec sp_addlinkedserver 'ITSV ', ' ', 'SQLOLEDB ', '遠程服務器名或ip地址 '
exec sp_addlinkedsrvlogin 'ITSV ', 'false ',null, '用戶名 ', '密碼 '
--查詢示例
select * from ITSV.數據庫名.dbo.表名
--導入示例
select * into 表 from ITSV.數據庫名.dbo.表名
--以後不再使用時刪除鏈接服務器
exec sp_dropserver 'ITSV ', 'droplogins '
--連接遠程/局域網數據(openrowset/openquery/opendatasource)
--1、openrowset
--查詢示例
select * from openrowset( 'SQLOLEDB ', 'sql服務器名 '; '用戶名 '; '密碼 ',數據庫名.dbo.表名)
--生成本地表
select * into 表 from openrowset( 'SQLOLEDB ', 'sql服務器名 '; '用戶名 '; '密碼 ',數據庫名.dbo.表名)
--把本地表導入遠程表
insert openrowset( 'SQLOLEDB ', 'sql服務器名 '; '用戶名 '; '密碼 ',數據庫名.dbo.表名)
select *from 本地表
--更新本地表
update b
set b.列A=a.列A
from openrowset( 'SQLOLEDB ', 'sql服務器名 '; '用戶名 '; '密碼 ',數據庫名.dbo.表名)as a inner join 本地表 b
on a.column1=b.column1
--openquery用法需要創建一個連接
--首先創建一個連接創建鏈接服務器
exec sp_addlinkedserver 'ITSV ', ' ', 'SQLOLEDB ', '遠程服務器名或ip地址 '
--查詢
select *
FROM openquery(ITSV, 'SELECT * FROM 數據庫.dbo.表名 ')
--把本地表導入遠程表
insert openquery(ITSV, 'SELECT * FROM 數據庫.dbo.表名 ')
select * from 本地表
--更新本地表
update b
set b.列B=a.列B
FROM openquery(ITSV, 'SELECT * FROM 數據庫.dbo.表名 ') as a
inner join 本地表 b on a.列A=b.列A
--3、opendatasource/openrowset
SELECT *
FROM opendatasource( 'SQLOLEDB ', 'Data Source=ip/ServerName;User ID=登陸名;Password=密碼 ' ).test.dbo.roy_ta
--把本地表導入遠程表
insert opendatasource( 'SQLOLEDB ', 'Data Source=ip/ServerName;User ID=登陸名;Password=密碼 ').數據庫.dbo.表名
select * from 本地表
SQL Server基本函數
SQL Server基本函數
1.字符串函數 長度與分析用
1,datalength(Char_expr) 返回字符串包含字符數,但不包含後面的空格
2,substring(expression,start,length) 取子串,字符串的下標是從“1”,start爲起始位置,length爲字符串長度,實際應用中以len(expression)取得其長度
3,right(char_expr,int_expr) 返回字符串右邊第int_expr個字符,還用left於之相反
4,isnull( check_expression , replacement_value )如果check_expression為空,則返回replacement_value的值,不為空,就返回check_expression字符操作類
5,Sp_addtype 自定義數據類型
例如:EXEC sp_addtype birthday, datetime, 'NULL'
6,set nocount {on|off}
使返回的結果中不包含有關受 Transact-SQL 語句影響的行數的信息。如果存儲過程中包含的一些語句並不返回許多實際的數據,則該設置由於大量減少了網絡流量,因此可顯著提高性能。SET NOCOUNT 設置是在執行或運行時設置,而不是在分析時設置。
SET NOCOUNT 爲 ON 時,不返回計數(表示受 Transact-SQL 語句影響的行數)。
SET NOCOUNT 爲 OFF 時,返回計數
常識
在SQL查詢中:from後最多可以跟多少張表或視圖:256
在SQL語句中出現 Order by,查詢時,先排序,後取
在SQL中,一個字段的最大容量是8000,而對於nvarchar(4000),由於nvarchar是Unicode碼。
MS SQL基礎
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
安裝SQL SERVER 2005 時遇到的問題
charlie911
2018-08-27 15:19:37
操作SQL數據庫常用語句
gudanyehai
2018-08-26 19:02:49
SQL數據開發-經典
gudanyehai
2018-08-26 19:02:49
SQL語法大全
gudanyehai
2018-08-26 19:02:47
關於日期函數的一些整理
liutiaoer
2018-08-23 19:21:15