MS
Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as
select encrypt(
select pwdencrypt(
select pwdcompare(
select pwdencrypt(
select pwdcompare(
declare @list varchar(
select @list=@list+
set @sql=
exec (@sql)
EXEC master
if (select checksum_agg(binary_checksum(*)) from A)
=
(select checksum_agg(binary_checksum(*)) from B)
print
else
print
DECLARE hcforeach CURSOR GLOBAL FOR SELECT
WHERE program_name IN(
EXEC sp_msforeach_worker
開頭到N條記錄
Select Top N * From 表
N到M條記錄(要有主索引ID)
Select Top M
N到結尾記錄
Select Top N * From 表 Order by ID Desc
sp_renamedb
select Name from sysobjects where xtype=
select name from syscolumns where id=object_id(
select a
select name as 存儲過程名稱 from sysobjects where xtype=
select * from master
或者
select dbid
select column_name
where table_name =
[n]
Select * From TableName Order By CustomerName
[n]
一
select top
二
if exists(select
drop function fgetscript
go
create function fgetscript(
@servername varchar(
) returns varchar(
as
begin
declare @re varchar(
declare @srvid int
declare @dbid int
declare @err int
exec @err=sp_oacreate
if @err<>
if isnull(@userid
begin
exec @err=sp_oasetproperty @srvid
if @err<>
exec @err=sp_oamethod @srvid
end
else
exec @err=sp_oamethod @srvid
if @err<>
exec @err=sp_oagetproperty @srvid
if @err<>
exec @err=sp_oamethod @dbsid
if @err<>
exec @err=sp_oamethod @dbid
if @err<>
exec @err=sp_oamethod @tbid
if @err<>
return(@re)
lberr:
exec sp_oageterrorinfo NULL
declare @errb varbinary(
set @errb=cast(@err as varbinary(
exec master
set @re=
+char(
+char(
return(@re)
end
go
用法如下
print dbo
declare @name varchar(
declare #aa cursor for
select name from sysobjects where xtype not in(
open #aa
fetch next from #aa into @name
while @@fetch_status=
begin
print dbo
fetch next from #aa into @name
end
close #aa
deallocate #aa
三
如果有一個用逗號分割開的字符串
create function getstrarrlength (@str varchar(
returns int
as
begin
declare @int_return int
declare @start int
declare @next int
declare @location int
select @str =
select @str=replace(@str
select @start =
select @next =
select @location = charindex(
while (@location <>
begin
select @start = @location +
select @location = charindex(
select @next =@next +
end
select @int_return = @next
return @int_return
end
create function getstrofindex (@str varchar(
returns varchar(
as
begin
declare @str_return varchar(
declare @start int
declare @next int
declare @location int
select @start =
select @next =
select @location = charindex(
while (@location <>
begin
select @start = @location +
select @location = charindex(
select @next =@next +
end
if @location =
select @str_return = substring(@str
if (@index <> @next ) select @str_return =
return @str_return
end
SELECT [dbo]
SELECT [dbo]
四
我要在一條語句裡操作不同的服務器上的不同的數據庫裡的不同的表
第一種方法:
select * from OPENDATASOURCE(
第二種方法:
先使用聯結服務器:
EXEC sp_addlinkedserver
exec sp_addlinkedsrvlogin @rmtsrvname=
GO
然後你就可以如下
select * from 別名
insert 庫名
select * into 庫名
go
五
蛙蛙推薦
先創建一個視圖
Create view fielddesc
as
select o
length
from syscolumns c
join systypes t on c
join sysobjects o on o
left join sysproperties p on p
where o
查詢時:
Select * from fielddesc where table_name =
還有個更強的語句
SELECT
(case when lorder=
lorder N
a
(case when COLUMNPROPERTY( a
(case when (SELECT count(*)
FROM sysobjects
WHERE (name in
(SELECT name
FROM sysindexes
WHERE (id = a
(SELECT indid
FROM sysindexkeys
WHERE (id = a
(SELECT colid
FROM syscolumns
WHERE (id = a
(xtype =
b
a
COLUMNPROPERTY(a
isnull(COLUMNPROPERTY(a
(case when a
isnull(e
isnull(g
FROM syscolumns a left join systypes b
on a
inner join sysobjects d
on a
left join syscomments e
on a
left join sysproperties g
on a
order by object_name(a
六
因為新開發的軟件需要用一些舊軟件生成的一些數據
UPDATE lvshi
SET shengri =
WHERE (zhiyezheng =
SELECT SUBSTRING(shengri
SUBSTRING(shengri
FROM lvshi
WHERE (zhiyezheng =
UPDATE lvshi
SET shenling = CONVERT(varchar(
+
month(shenling)) ELSE CONVERT(varchar(
END +
day(shenling)) ELSE CONVERT(varchar(
WHERE (zhiyezheng =
七
分區視圖是提高查詢性能的一個很好的辦法
create table tempdb
id int primary key check(id between
create table pubs
id int primary key check(id between
create table northwind
id int primary key check(id between
go
create view v_t
as
select * from tempdb
union all
select * from pubs
union all
select * from northwind
go
insert v_t select
union all select
union all select
union all select
union all select
union all select
update v_t set name=name+
delete from v_t where right(id
select * from v_t
go
drop table northwind
drop view v_t
/**//**//**//*
id name
(所影響的行數為
==*/
八
create table [tb]([id] int identity(
insert [tb] select
union all select
union all select
union all select
union all select
union all select
union all select
union all select
union all select
union all select
union all select
union all select
go
create function f_cid(
@id int
)returns @re table([id] int
as
begin
declare @l int
set @l=
insert @re select @id
while @@rowcount>
begin
set @l=@l+
insert @re select a
from [tb] a
where a
end
/**//**//**//**//**//**//**//*
delete a from @re a
where exists(
select
return
end
go
select a
go
drop table [tb]
drop function f_cid
go
九
CREATE TABLE [t] (
[id] [int] IDENTITY (
[GUID] [uniqueidentifier] NULL
) ON [PRIMARY]
GO
下面這句執行
insert t values (newid())
查看執行結果
select * from t
select * from t
order by case id when
when
when
when
when
select * from t order by (id+
select * from t order by charindex(cast(id as varchar)
select * from t
WHERE id between
order by charindex(cast(id as varchar)
select * from t order by case when id >
select * from t order by id /
十
首先id列是int標識類類型
除了
delete from [fujian] where charindex(
還有一種就是
delete from table
十一
下面這個函數獲取
CREATE FUNCTION fn_Get
RETURNS Nvarchar(
AS
BEGIN
DECLARE @LvshiNames varchar(
select @LvshiNames=
DECLARE lvshi_cursor CURSOR FOR
數據庫裡有
SELECT
INSERT
DELETE
UPDATE
CREATE TABLE
DROP TABLE
ALTER TABLE
CREATE VIEW
DROP VIEW
CREATE INDEX
DROP INDEX
CREATE PROCEDURE
DROP PROCEDURE
CREATE TRIGGER
DROP TRIGGER
CREATE SCHEMA
DROP SCHEMA
CREATE DOMAIN
ALTER DOMAIN
DROP DOMAIN
GRANT
DENY
REVOKE
COMMIT
ROLLBACK
SET TRANSACTION
DECLARE
EXPLAN
OPEN
FETCH
CLOSE
PREPARE
EXECUTE
DESCRIBE
declare @id char(
select @id =
declare @x int @y int @z int
select @x =
if @x > @y
print
else if @y > @z
print
else print
use pangu
update employee
set e_wage =
case
when job_level =
when job_level =
when job_level =
else e_wage*
end
declare @x int @y int @c int
select @x =
while @x <
begin
print @x
while @y <
begin
select @c =
print @c
select @y = @y +
end
select @x = @x +
select @y =
end
waitfor delay
select * from employee
waitfor time
SELECT
select *(列名) from table_name(表名) where column_name operator value ex宿主)
select * from stock_information where stockid = str(nid)
stockname =
stockname like
stockname like
stockname like
or stockpath =
or stocknumber <
and stockindex =
not stocksex =
stocknumber between
stocknumber in(
order by stockid desc(asc)
order by
stockname = (select stockname from stock_information where stockid =
select distinct column_name form table_name
select stocknumber
select stockname
having count(*) =
select *
from table
where table
table
select stockname from table
union [all]
select stockname from table
insert
insert into table_name (Stock_name
value (select Stockname
update
update table_name set Stockname =
Stockname = default
Stockname = null
Stocknumber = Stockname +
delete
delete from table_name where Stockid =
truncate table_name
drop table table_name
alter table
alter table database
sp_help table_name
create table table_name (name char(
insert into table_name select
alter table table_name drop constraint Stockname_default
常用函數(function)
轉換函數
convert(數據類型
統計函數
AVG
COUNT
MAX
MIN
SUM
AVG
use pangu
select avg(e_wage) as dept_avgWage
from employee
group by dept_id
MAX
use pangu
select e_name
from employee
where e_wage =
(select max(e_wage)
from employee)
STDEV()
VAR()
VARP()
算術函數
三角函數
SIN(float_expression)
COS(float_expression)
TAN(float_expression)
COT(float_expression)
反三角函數
ASIN(float_expression)
ACOS(float_expression)
ATAN(float_expression)
ATAN
DEGREES(numeric_expression)
RADIANS(numeric_expression)
EXP(float_expression)
LOG(float_expression)
LOG
SQRT(float_expression)
取近似值函數
CEILING(numeric_expression)
FLOOR(numeric_expression)
ROUND(numeric_expression)
ABS(numeric_expression)
SIGN(numeric_expression)
PI()
RAND([integer_expression])
字符串函數
ASCII()
CHAR()
LOWER()
UPPER()
STR()
LTRIM()
RTRIM()
LEFT()
CHARINDEX()
SOUNDEX()
DIFFERENCE()
QUOTENAME()
/**//**//**//*select quotename(
運行結果如下
{
{abc} [abc]*/
REPLICATE()
/**//**//**//*select replicate(
運行結果如下
abcabcabc NULL*/
REVERSE()
REPLACE()
/**//**//**//*select replace(
運行結果如下
abcdefg*/
SPACE()
STUFF()
數據類型轉換函數
CAST() 函數語法如下
CAST() ( AS [ length ])
CONVERT() 函數語法如下
CONVERT() ([ length ]
select cast(
運行結果如下
日期函數
DAY()
MONTH()
YEAR()
DATEADD(
DATEDIFF(
DATENAME(
DATEPART(
GETDATE()
系統函數
APP_NAME()
COALESCE()
COL_LENGTH(<
COL_NAME(
DATALENGTH()
DB_ID([
DB_NAME(database_id)
HOST_ID()
HOST_NAME()
IDENTITY([
/**//**//**//*select identity(int
into newtable
from oldtable*/
ISDATE()
ISNULL(
ISNUMERIC()
NEWID()
NULLIF(
SELECT
INSERT
DELETE
UPDATE
CREATE TABLE
DROP TABLE
ALTER TABLE
CREATE VIEW
DROP VIEW
CREATE INDEX
DROP INDEX
CREATE PROCEDURE
DROP PROCEDURE
CREATE TRIGGER
DROP TRIGGER
CREATE SCHEMA
DROP SCHEMA
CREATE DOMAIN
ALTER DOMAIN
DROP DOMAIN
GRANT
DENY
REVOKE
COMMIT
ROLLBACK
SET TRANSACTION
DECLARE
EXPLAN
OPEN
FETCH
CLOSE
PREPARE
EXECUTE
DESCRIBE
declare @id char(
select @id =
declare @x int @y int @z int
select @x =
if @x > @y
print
else if @y > @z
print
else print
use pangu
update employee
set e_wage =
case
when job_level =
when job_level =
when job_level =
else e_wage*
end
declare @x int @y int @c int
select @x =
while @x <
begin
print @x
while @y <
begin
select @c =
print @c
select @y = @y +
end
select @x = @x +
select @y =
end
waitfor delay
select * from employee
waitfor time
select * from employee
***SELECT***
select *(列名) from table_name(表名) where column_name operator value
ex:(宿主)
select * from stock_information where stockid = str(nid)
stockname =
stockname like
stockname like
stockname like
or stockpath =
or stocknumber <
and stockindex =
not stocksex =
stocknumber between
stocknumber in(
order by stockid desc(asc)
order by
stockname = (select stockname from stock_information where stockid =
select distinct column_name form table_name
select stocknumber
select stockname
having count(*) =
select *
from table
where table
table
select stockname from table
union [all]
select stockname from table
***insert***
insert into table_name (Stock_name
value (select Stockname
***update***
update table_name set Stockname =
Stockname = default
Stockname = null
Stocknumber = Stockname +
***delete***
delete from table_name where Stockid =
truncate table_name
drop table table_name
***alter table***
alter table database
sp_help table_name
create table table_name (name char(
insert into table_name select
alter table table_name drop constraint Stockname_default
***function(/**//*常用函數*/)***
AVG
COUNT
MAX
MIN
SUM
use pangu
select avg(e_wage) as dept_avgWage
from employee
group by dept_id
use pangu
select e_name
from employee
where e_wage =
(select max(e_wage)
from employee)
/**//***三角函數***/
SIN(float_expression)
COS(float_expression)
TAN(float_expression)
COT(float_expression)
/**//***反三角函數***/
ASIN(float_expression)
ACOS(float_expression)
ATAN(float_expression)
ATAN
DEGREES(numeric_expression)
RADIANS(numeric_expression)
EXP(float_expression)
LOG(float_expression)
LOG
SQRT(float_expression)
/**//***取近似值函數***/
CEILING(numeric_expression)
FLOOR(numeric_expression)
ROUND(numeric_expression)
ABS(numeric_expression)
SIGN(numeric_expression)
PI()
RAND([integer_expression])
ASCII()
CHAR()
LOWER()
UPPER()
STR()
LTRIM()
RTRIM()
LEFT()
CHARINDEX()
SOUNDEX()
DIFFERENCE()
QUOTENAME()
/**//*select quotename(
運行結果如下
{abc} [abc]*/
REPLICATE()
/**//*select replicate(
運行結果如下
abcabcabc NULL*/
REVERSE()
REPLACE()
/**//*select replace(
運行結果如下
abcdefg*/
SPACE()
STUFF()
CAST() 函數語法如下
CAST() (<expression> AS <data_ type>[ length ])
CONVERT() 函數語法如下
CONVERT() (<data_ type>[ length ]
select cast(
運行結果如下
DAY()
MONTH()
YEAR()
DATEADD(<datepart>
DATEDIFF(<datepart>
DATENAME(<datepart>
DATEPART(<datepart>
GETDATE()
APP_NAME()
COALESCE()
COL_LENGTH(<
COL_NAME(<table_id>
DATALENGTH()
DB_ID([
DB_NAME(database_id)
HOST_ID()
HOST_NAME()
IDENTITY(<data_type>[
/**//**//**//*select identity(int
into newtable
from oldtable*/
ISDATE()
ISNULL(<check_expression>
ISNUMERIC()
NEWID()
NULLIF(<expression
From:http://tw.wingwit.com/Article/program/SQLServer/201311/22045.html