sql查詢相關

2021-10-18 14:46:08 字數 1891 閱讀 1435

– 查詢指定列所在表

select a.name,b.name as cname

from syscolumns a

left

join sysobjects b

on a.cdefault=b.id

where a.id=object_id(

'dt001_proc-4abafc26-5a8e-49d3-8af5-f5b228c87858'

)and a.name=

'dp_fvoucherno'

– 分頁

select

top10

*from store_store a where

notexists

(select

1from

(select

top30 ss_id from store_store order

by ss_id)a1 where a1.ss_id=a.ss_id)

order

by ss_id

– 分頁2 2012 以上

mysql limit *,*

rownumber()

select

*from store_store

order

by ss_id

offset1*

2rows

fetch

next

3rows only

– 分組 按條件首行查詢

select

top10

*from

(select row_number(

)over

(partition

by 分組條件1

,分組條件2

order

by 順序條件 desc

)as rownum, 查詢結果字段

from 查詢表

) twhere t.rownum =

1

sql行轉列

declare

@sqll

varchar

(1000)=

''select

@sqll

=@sqll

+'['

+a.wu_state+

'],'

from

(select

distinct wx001_users.wu_state

from wx001_users/*列的值*/

) aset

@sqll

=substring(

@sqll,0

,len

(@sqll))

-- select substring(@sqll,0,len(@sqll))

declare

@rzcsql

varchar

(max)=''

set@rzcsql='

select * from (select wu_id,wu_gender,wu_state

from wx001_users

group by wu_state,wu_id,wu_gender

) /*資料來源*/

as p

pivot

( count(p.wu_id/*行轉列後 列的值*/)

for

p.wu_state/*需要行轉列的列*/

in ('

+@sqll+'

)) as t'

-- select @rzcsql

exec

(@rzcsql

)

sql查詢相關

select from 表名 a where a.日期字段 to date yyyy mm dd and a.日期字段 to date yyyy mm dd 和 為前端傳參,string型別的,需要先to date進行轉換格式 yyyy mm dd 是匹配資料庫中 date 欄位存的格式 是用來代替...

SQL相關路徑查詢指令碼

1.查詢機器名 select servername as 機器名稱 查詢已安裝的sql例項名 select from sys.servers 2.查詢sql安裝路徑 declare installpath varchar 200 exec master.dbo.xp regread hkey loc...

SQL相關路徑查詢指令碼

1.查詢機器名 select servername as 機器名稱 查詢已安裝的sql例項名 select from sys.servers 2.查詢sql安裝路徑 declare installpath varchar 200 exec master.dbo.xp regread hkey loc...